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

dom-gen

v2.3.0

Published

Utility for dom generation, a jquery plugin

Downloads

22

Readme

dom-gen v2.3.0 Circle CI codecov.io

Utility for dom generation, a jquery plugin

Idea

This tool is a shorthand of $('<tagName/>', opts). You can write it like tagName(opts) with this tool such as div({data: {key: 'value'}}) for example.

See the slide "Things You Might Not Know About jQuery" by John Resig. This slide explains how $('<tagName/>', opts) works in details.

Install

npm install dom-gen

Usage

NOTE dom-gen supposes $ is exposed in global namespace. You need to put jquery on global namespace first.

div()

import {div} from 'dom-gen'

div() // This creates an empty div element.

The above code is the same as $('<div/>'). You can chain jquery method calls like the following

div().text('Hello').appendTo('#main')

div(opts)

You can pass generation options as the parameter.

div({ data: { x: 0, y: 1 }, addClass: 'container', appendTo: '#main' })

The above is the same as:

$('<div/>', { data: { x: 0, y: 1 }, addClass: 'container', appendTo: '#main' })

(See the slide "Things You Might Not Know About jQuery" which explains what the above code means in jQuery.)

or:

$('<div/>').data({ x: 0, y: 1 }).addClass('container').appendTo('#main')

Another example

img({ attr: { src: 'path/to/img' }, appendTo: '#some-place' })

is the same as:

$('<img/>').attr('src', 'path/to/img').appendTo('#some-place')

div(opts, param0, [param1, ...])

You can pass additional params to div function and they are appended to the element.

div({addClass: 'main'}, div().text('Hello'), 'world!')

is the same as the follwoing jquery call:

$('<div/>', {addClass: 'main'}).append($('<div/>').text('Hello'), 'world!')

which is equivalent of the following html:

<div class="main"><div>Hello</div>world!</div>

You can even omit first param opts if it's empty.

div(p(span('Hello'), ' ', span('world!')))

is equal to:

<div><p><span>Hello</span> <span>world!</span></p></div>

Supported tags

dom-gen exports following tags by default for now. You can import them directly from dom-gen and use them as generator functions.

a()
abbr()
address()
area()
article()
aside()
audio()
b()
base()
bdi()
bdo()
blockquote()
body()
br()
button()
canvas()
caption()
cite()
code()
col()
colgroup()
data()
datalist()
dd()
del()
details()
dfn()
dialog()
div()
dl()
dt()
em()
embed()
fieldset()
figcaption()
figure()
footer()
form()
h1()
h2()
h3()
h4()
h5()
h6()
head()
header()
hr()
html()
i()
iframe()
img()
input()
ins()
kbd()
keygen()
label()
legend()
li()
link()
main()
map()
mark()
math()
menu()
menuitem()
meta()
meter()
nav()
noscript()
object()
ol()
optgroup()
option()
output()
p()
param()
picture()
pre()
progress()
q()
rb()
rp()
rt()
rtc()
ruby()
s()
samp()
script()
section()
select()
small()
source()
span()
strong()
style()
sub()
summary()
sup()
svg()
table()
tbody()
td()
template()
textarea()
tfoot()
th()
thead()
time()
title()
tr()
track()
u()
ul()
var()
video()
wbr()

Create your own

You can create the generator for your own tag.

import domGen from 'dom-gen'

const xTag = domGen('x-tag')

xTag({addClass: 'foo'}, p('Hello')) // This is equivalent of <x-tag class="foo"><p>Hello</p></x-tag>

Recipes

Mix inline html and dom-gen composition

Inline elements are often better not to use dom-gen for creating.

p('Hello, this is <span class="green">example</span> page!')

is a bit better readable than:

p(
  'Hello, this is ',
  span({addClass: 'green'}, 'example'),
  'page!'
)

Complex construction

import {div, h2, p} from 'dom-gen'

div(
  h2('Hello'),
  div({addClass: 'greeting'},
    p('Hello, this is <span class="green">example</span> page!')
  ),
  hr()
)

is equivalent of the following html:

<div>
  <h2>Hello</h2>
  <div class="greeting">
    <p>Hello, this is <span class="green">example</span> page!</p>
  </div>
  <hr/>
</div>

Use with tagged template string

import {div, h2, p} from 'dom-gen'

div(
  h2`Hello`,
  div({addClass: 'greeting'},
    p`Hello, this is <span class="green">example</span> page!`
  ),
  hr()
)

License

MIT

Similar projects