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

u-elem

v9.7.1

Published

Configure your own JsonML dialect

Downloads

89

Readme

Elem Build Status Coverage Status

var x = require('u-elem'),
    on = require('u-elem/on');

x('body',
  {draggable: false, className: 'foo'},
  'Hello world!',
  ['span',
    on('click',e => console.log('click!')),
    {style: {color: 'green', textAlign: 'center'}},
    'Hi again!'
  ]
);

console.log(document.body.innerHTML);
// Hello world!<span style="color: green; text-align: center;">Hi again!</span>

console.log(document.body.draggable); // false

Elem is a parser of a custom JsonML variant, based in the DOM rather than the HTML markup. It uses internally u-proto's apply, which means that all of its goodies are directly integrated into Elem. Well, that's its default behavior, you can change it: Elem is both extensible and configurable.

What Elem does is just call the internal [hook] method of provided objects. Well, in fact all it does is call Array.prototype[hook]:

function x(){
  return Array.prototype[hook].call(arguments);
}

The Array hook in turn calls the hook of the first element without arguments, expecting a Node to be returned. This node will be the argument passed to the hooks of the remaining elements, and that's all. But then again, that's just the default Array hook included with Elem, feel free to change it if you want. To illustrate these hook functions, we'll take a look at the default Node hook:

var define = require('u-proto/define'),
    hook = require('../hook.js');

Node.prototype[define](hook,function(parent){
  if(parent) parent.appendChild(this);
  return this;
});

Now imagine you've got an array like this:

[
  document.body,
  document.createTextNode('foo'),
  document.createTextNode('bar')
]

The Array hook would run the hook of document.body without arguments, which would do nothing but return document.body itself. Then, it would run the hook of foo and bar with document.body as the only argument and ignore returned values. In the end, our body would end up looking like this, assuming it was previously empty:

<body>foobar</body>

You may want to take a look at on.js to see how hooks may be used to do interesting things, such as calling addEventListener in the middle of the parsing process. Another cool hook is when:

var x = require('u-elem'),
    when = require('u-elem/when'),
    H = require('y-setter').Hybrid,

    h = new H(),
    d = x('div',
      when(h,['span','Yes!']).else(['span','No!'])
    );

h.value = true;
console.log(d.innerHTML); // <span>Yes!</span>

h.value = false;
console.log(d.innerHTML); // <span>No!</span>

One thing remains: the cleanup. Each node gets assigned a detacher to its require('u-elem/detacher') property. Elem ships with a utility method to detach an element's detacher and all of its children, use it when you're done with a node, like this:

var x = require('u-elem'),
    destroy = require('u-elem/destroy'),
    d = x('div');

//...

d[destroy]();

Or listen to a detacher to do your cleanup:

var x = require('u-elem'),
    detacher = require('u-elem/detacher'),
    d = x('div');

//...

d[detacher].then(function(){
  // do your cleanup
});

And lastly, a note on variable names. You can see here that an x is used as the variable name of Elem. It was originally elem, but it was larger and less cool. One of the beauties of modules is that you can name things however you like without polluting the global namespace. Feel free to name it x, elem, $, _ or even ಠ_ಠ. And for those of you not using modules, please stop doing so. Or, you know, just call it x.