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 🙏

© 2025 – Pkg Stats / Ryan Hefner

domator

v2.0.0

Published

Minimal & Fast DOM creation.

Readme

Domator

Simple DOM elements creation on Javascript, using something like a CSS selector syntax, with only 180 LOC. Also works on server-side.

Install

npm install domator --save

Table of Contents

Usage

With CommonJS

var d = require('domator')
var el = d('p.the-class Hello!') // <p class="the-class">Hello!</p>

document.body.appendChild(el)

Browser

<script src="dist/domator.min.js"></script>
<script>
  ;(function() {
    var el = domator('p.the-class Hello!') // <p class="the-class">Hello!</p>
    
    document.body.appendChild(el)
  })()
</script>

Examples

To create a simple div:

var el = d('div') // <div></div>

To create a div with 3 span childs:

var el = d('div', ['span', 'span', 'span'])

/* Produces:
<div>
  <span></span>
  <span></span>
  <span></span>
</div>
*/

Can be nested indefinitely:

var el = d(
  'div', [
    'div', [
      'div' [
        'span',
        'span',
        'span'
      ]
    ]
  ]
)

/* Produces:
<div>
  <div>
    <div>
      <span></span>
      <span></span>
      <span></span>
    </div>
  </div>
</div>
*/

With custom attributes, text, and className:

var el = d('div', {
  'data-custom': 'the-custom-value',
  text: 'Hello!',
  className: 'the-class', // "class" and "className" are the same
  id: 'the-id'
})

/* Produces:
<div class="the-class" id="the-id" data-custom="the-custom-value">
  Hello!
</div>
*/

The same as the last one, but, with a slick syntax:

var el = d('#the-id.the-class[data-custom="the-custom-value"] Hello!')

/* Produces:
<div class="the-class" id="the-id" data-custom="the-custom-value">
  Hello!
</div>
*/

If you dont have a single root element, it creates a DocumentFragment with all of them:

var el = d('.one', '.two', '.three')

document.body.appendChild(el)

/* Leaves the <body> with:
<body>
  <div class="one"></div>
  <div class="two"></div>
  <div class="three"></div>
</body>
*/

To keep a reference to a child, you can pre-create an element and pass it on to the constructor:

var span = d('span.some-child')
var el = d('div.the-parent', [span])

/* Produces:
<div class="the-parent">
  <span class="some-child"></span>
</div>
*/

API

domator()

Using the domator() function you can create any amount of DOM Nodes, with and infinite amount of children.

domator(el, attrs, [children...] [, el, attrs, [children...]]...)

el

Domator Selector

The domator selector is used to be able to create an element with default values super easy, all the values are optional. A complete example would be:

tag#id.class-name[attr="value"] Hello world!
  • tag
    • Default: div
    • Could be any value composed only with letters, numbers, or hyphens (-).
    • Optional: in the case you define an id, class, or attr.
      • e.g.: .the-class produces <div class="the-class"></div>
  • #id
    • id= of the element.
    • Can only be one #id defined.
  • .class-name
    • class="class-name" value to be setted to the element.
    • Multiple values allowed.
      • e.g.: .one.two creates <div class="one two"></div>
  • [attr="value"]
    • Custom attribute to set to the element.
    • Multiple values allowed.
    • "value" is optional.
      • e.g.: input[required] creates <input required>
  • Hello world!
    • Everything after the whitespace will be added as text content on the element.
    • e.g.: div Hello world! creates <div>Hello world!</div>

attrs

  • Optional
  • Type: object

Attributes of the element, e.g.:

domator('div', {"data-something": "hello"}) // Produces <div data-something="hello"></div>

children

  • Optional
  • Type: Array

Array with children elements to be added inside the element. The array receives the same arguments as the domator(...) function. e.g.:

domator('div.parent', ['span.child']) // Produces: <div class="parent"><span class="child"></span></div>

domator.setDocument(document)

Takes a valid Document element. It's needed when used on other environments than the browser, where is no default window.document.

domator.toString(node)

Helper function to convert existing node elements to string.

domator.create(element, attributes)

Helper function used internally to create elements, takes a Domator Selector and optionally an attributes object.

Server side usage

To use it on server side, you must set a valid document element first:

var jsdom = require('jsdom')
var d = require('domator')

d.setDocument(jsdom.jsdom())

var el = d('p.the-class Hello!') // <p class="the-class">Hello!</p>

Tests

npm run test

Licence

MIT

JS Styles

js-standard-style