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

dsc

v1.0.0

Published

A 0.3kB JavaScript library for creating DOM structures

Downloads

6

Readme

dsc

NPM Version Build Status Support Chat

dsc is a JavaScript function for defining DOM structures. It takes an element, properties or attributes, children, and returns a DOM structure. It will add up to 358 bytes to your project.

dsc(element, attributes, ...children)

Usage

Add dsc to your page.

<script src="https://unpkg.com/dsc"></script>
<body>
  <script>
  dsc(document.body, null,
    // append <h3>Hello, <strong title="Earthly Planet">World</strong>! This is generated content!</h3>
    dsc('h3', null,
      'Hello, ', dsc('strong', { title: 'Earthly Planet' },
        'World'
      ), '! This is generated content!'
    ),

    // append a hidden svg symbol
    dsc('svg', { width: 0, height: 0 },
      dsc('symbol', { id: 'foo', viewBox: '0 0 32 32' },
        dsc('path', { d: 'M0 0h12L8 4l6 6-4 4-6-6-4 4M32 0H20l4 4-6 6 4 4 6-6 4 4M0 32V20l4 4 6-6 4 4-6 6 4 4m20 0V20l-4 4-6-6-4 4 6 6-4 4' })
      )
    ),

    // append a visible svg using the previous symbol
    dsc('svg', { width: 32, height: 32 },
      dsc('use', { href: '#foo' })
    )
  );
  </script>
</body>

Alternatively, add dsc to your project:

npm install dsc
import getdsc from 'dsc';

const dsc = getdsc(window);

// append <h3>Hello, <strong title="Earthly Planet">World</strong>!</h3>
dsc(document.body, null,
  dsc('h3', null,
    'Hello, ', dsc('strong', { title: 'Earthly Planet' },
      'World'
    ), '! This is generated content!')
);

When document is an assumed global, use the browser version.

import dsc from 'dsc/browser';

// append <h3>Hello, <strong title="Earthly Planet">World</strong>!</h3>
dsc(document.body, null,
  dsc('h3', null,
    'Hello, ', dsc('strong', { title: 'Earthly Planet' },
      'World'
    ), '! This is generated content!')
);

When using dsc alongside jsdom, initialize dsc with the appropriate window object.

import jsdom from 'jsdom';
import getdsc from 'dsc';

const dom = new jsdom.JSDOM(`YOUR HTML`);
const dsc = getdsc(dom.window);

// append <h3>Hello, <strong title="Earthly Planet">World</strong>!</h3>
dsc(dom.window.document.body, null,
  dsc('h3', null,
    'Hello, ', dsc('strong', { title: 'Earthly Planet' },
      'World'
    ), '! This is generated content!')
);

When converting JSX to JS, dsc can be used to generate DOM Elements.

/** @jsx dsc */

<h3>Hello, <strong title="Earthly Planet">World</strong>! This is generated content!</h3>;

/* becomes */

dsc('h3', null,
  'Hello, ', dsc('strong', { title: 'Earthly Planet' },
    'World'
  ), '! This is generated content!'
);

Read the @babel/plugin-transform-react-jsx documentation for more information about transforming JSX.

Arguments

element

The first argument represents the Element being referenced or created. String arguments create new Elements using the string as the tag name.

// create <h3> using the "h3" string
dsc('h3');
// use the created <h3>
dsc(document.createElement('h3'));

attributes

The second argument represents the properties or attributes being assigned to the element. When a name exists on the element as a property then the property is assigned. Otherwise, the attribute is assigned. Attributes with a null value are removed from the element.

// create <h3 class="foo"> using the "className" property
dsc('h3', { className: 'foo' });
// create <h3 class="foo"> using the "class" attribute
dsc('h3', { class: 'foo' });
// create <h3> with a click event using the "onclick" property
dsc('h3', { onclick(event) {} });

children

The third argument and all arguments afterward are children to be appended to the element.

// append "Hello World" as a text node to <h3>
dsc('h3', null, 'Hello World');
// append "Hello World" as 3 text nodes to <h3>
dsc('h3', null, 'Hello', ' ', 'World');
// append a new <h3> to the fragment
dsc(document.createDocumentFragment(), null, dsc('h3'));

Return

Create returns the element referenced or created by element.

// h3 is <h3>
const h3 = dsc('h3');

// h3ish3 is true
const ish3h3 = h3 === dsc(h3);