npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsmile

v0.0.20

Published

A JavaScript Markup Language that plays nice.

Downloads

4

Readme

JSMiLe

J:)

A JavaScript Markup Language that plays nice.

Installation

Node

npm install jsmile

Browser

A fully bundled and transformed implementation, ready for use in browsers including any version of Internet Explorer or Opera Mini, is provided in the dist/jsmile.js file. Including this script will expose the entire package as a global object named jsmile.

<script src="./node_modules/jsmile/dist/jsmile.js"></script>

Usage

Build

The build method outputs an HTML string from JavaScript data. It is an alias for the jsml-davidystephenson package.

jsmile.build('hello world')
// 'hello world'

jsmile.build({})
// '<div></div>'

const jsmile.build({ child: 'hello world' })
// '<div>hello world</div>'

jsmile.build({ tag: 'h1' child: 'Welcome!' })
// '<h1>Welcome!</h1>'

jsmile.build({ tag: 'img', src: 'imgur.com' })
// '<img src='imgur.com'>'

const format = name => [
  { tag: 'h3', class: 'title', child: 'User page' }, 
  { tag: 'p', child: `Welcome ${name}.`}
]
jsmile.build(format('Someone'))
// '<h3 class="title">User page</h3><p>Welcome Someone.</p>'

Browser

The browser method renders JavaScript DOM element from JavaScript data. It is an alias for the jsmile-browser package.

const render = jsmile.browser(window)

const div = render({})
console.log(div.tagName)
// DIV

In the browser, or anywhere else where a DOM window with a valid document is globally available, a jsmile.browser instance will be created and attached as jsmile.render. A shortcut for this function will also be added globally as jsml.

<script src="./node_modules/jsmile/dist/jsmile.js"></script>
<script>
  console.log(jsmile)
  // { build: f, express: f, browser: f, render: f }

  const span = jsmile.render({ tag: 'span', child: 'hello world' })
  console.log(span.outerHTML)
  // '<span>hello world</span>'

  const div = jsml({})
  console.log(div.outerHTML)
  // '<div></div>'
</script>

Express

Initialization

The express method integrates jsmile with express as a view engine. It is an alias for the jsmile-express package.

After setting a views directory, declare a view engine that will read .js files in the directory. Then, initialize the engine by passing the express application itself to jsmile's express method.

const express = require('express')
const jsmile = require('jsmile')

const app = express()

app.set('views', './views')
app.set('view engine', 'js')
app.engine('js', jsmile.express(app))

Rendering

You can then use the express.render method to render view files by name, optionally passing any data the view uses. View files are JavaScript modules that export a function.

The function is called when the view is rendered by your routing logic. The output is parsed by the jsmile.build method and then sent as the request's response. The function is passed two arguments:

  1. The jsmile library.
  2. An options object containing the data passed to the route.

views/index.js

module.exports = (options, jsmile) => {
  const title = options.message.toUpperCase()

  return {
    tag: 'html',
    child: [
      { tag: 'head' },
      { 
        tag: 'body'
        child: {
          tag: 'h1',
          child: title 
        }
      }
    ]
  }
}
app.get('/', (request, response) => response.render('index', { message: 'Hello user!' }))
// <html><head></head><body><h1>HELLO USER!</h2></body></html>

Including

The jsmile library passed to views has an additional method, include.

The include method allows other views to be incorporated. It works similarly to the Express render method. It takes two arguments:

  1. The name of the view.
  2. An optional object with data used in the function.

views/navbar.js

module.exports = () => ({
  class: 'navbar',
  child: [
    {
      tag: 'a',
      href: '/',
      child: 'Home'
    }
  ]
})

views/layout.js

module.exports = (jsmile, options) => ({
  tag: 'html',
  child: [
    { tag: 'head', child: options.head },
    {
      tag: 'body',
      child: [
        jsmile.include('navbar'),
        options.body
      ]
    }
  ]
})

views/title.js

module.exports = (jsmile, options) => ({
  class: 'title',
  tag: 'h1',
  child: options.text
})

views/index.js

module.exports = (jsmile, options) => jsmile.include('layout', {
  body: [
    jsmile.include('title', { text: 'Welcome'}),
    { tag: 'p', child: options.message }
  ]
})
app.get('/', (request, response) => response.render('index', { message: 'Hello World!' }))
///<html><head></head><body><div class="navbar"><a href="/">Home</a></div><h1 class="title">Welcome</h1><p>Hello World!</p></body></html>