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

dfrag

v1.0.1

Published

A minimalist functional abstraction to create DOM fragments.

Downloads

9

Readme

dfrag

A minimalist functional abstraction to create DOM fragments.

Why this project?

The two target users for this project are:

  • Developers stuck with an old codebase that creates DOM via string concats (or worse yet, via jQuery).
  • Developers building simple apps that don't merit the overhead of setting up Babel, WebPack, React, etc.

The goal of this project is to not be used at all. The target audience is the sad developer that needs to maintain an old codebase where you create lots of HTML by concatenating strings. If this describes a project you're working on, and refactoring that codebase to use React or something slightly more advanced to simplify the DOM creation is not an option, then this project might be a reasonable alternative.

This single function module exists to simplify creation of DOM fragments in a way that is both terse and clear. When you need to concatenate multiple strings to create a tag with a few attributes, mixing those single quotes and double quotes can get difficult to look at, and easy to mess up. With the API exposed in this module, that issue should be solved completely.

This project is not meant to be a new silver bullet. If you're building a new application, or if you have the option to use a full-fledged view framework/library, by all means, use the other alternative.

API

The API exposed by dfrag is simple:

dfrag(type: string, props: Object, children: string|Array<Element>): Element

If you squint hard enough, you can see React.createElement in there...

  • type is the name of the element being created. Example: 'p' or 'div'
  • props is an optional object of DOM properties. Example: { className: 'btn btn-primary', id: 'main-button', onClick: () => alert('Clicked!'), dataCustomAttribute: 23 }
  • children is an optional parameter. If it is a string or a number, it will be set as the element's textContent. If it is an array, each of the elements will be assumed to be DOM elements, and will be appended to the main element.

Usage

var title = dfrag('h1', { className: 'title', style: 'color: #c00' }, 'DOM Fragments');
var container = dfrag('div', {}, [
   title,
   dfrag('hr'),
   dfrag('ul', {}, [
     dfrag('li', {}, 'Aint no virtual dom'),
     dfrag('li', { style: 'text-decoration: line-through' }, 'Better than Angular'),
     dfrag('li', {}, '...')
   ]),
   dfrag('button', { disabled: true }, 'Submit'),
])

document.body.appendChild(container);