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

hyperscript-normalize-args

v1.0.0

Published

A hyperscript helper to normalize component arguments, for easier creation of reusable components

Downloads

5

Readme

hyperscript-normalize-args

Build status NPM version XO code style

A hyperscript helper to normalize component arguments, for easier creation of reusable components

Why?

We want a flexible API to our users but an easy to use and consistent API internally.

This helper enables your hyperscript component to be called in a lot of ways:

// selector
component('.class#id');
// selector, props
component('.class.class2', {disabled: true});
// selector, props, child/-ren
component('.class', {title: 'Lorem ipsum'}, 'Hello world');
// selector, child/-ren
component('.class', ['Hello', 'world']);
// props, child/-ren
component({checked: true}, otherComponent('Hello world'));
// child/-ren
component([p('Lorem ipsum'), p('Dolor sit')]);

And you can code against a consistent API in your component no matter what!
You'll always get a props object as first argument and a children array as the second argument.

See module usage for examples.

Installation

Install hyperscript-normalize-args using npm:

npm install --save hyperscript-normalize-args

Usage

Module usage

With Cycle.js, virtual-dom or hyperscript

import {button} from '@cycle/dom';
import classNames from 'classnames';
import normalize from 'hyperscript-normalize-args';

const n = normalize(); // no options => DOM mode

// Creating a `myButton` component, which wraps `button` and always add "my-button" as css class
const myButton = n((props, children) => {
	const {
		className,
		...attrs
	} = props;
	return button({...attrs, className: classNames(className, 'my-button')}, children);
});

// myButton can now be used in multiple ways:

myButton('#the-button');
// props = {id: 'the-button'}
// children = []
myButton('#the-button.a-class', {className: 'btn'}, 'Click me');
// props = {id: 'the-button', className: 'a-class btn'}
// children = ['Click me']
myButton('Click me');
// props = {}
// children = ['Click me']

With snabbdom

import h from 'snabbdom/h';
import hh from 'hyperscript-helpers';
import classNames from 'classnames';
import normalize from 'hyperscript-normalize-args';

const {button} = hh(h);
const n = normalize({snabbdom: true}); // snabbdom mode

// Creating a `myButton` component, which wraps `button` and always add "my-button" as css class
const myButton = n((props, children) => {
	const {
		className,
		props: prop,
		...attrs
	} = props;
	return button({...attrs, props: {...prop, className: classNames(className, 'my-button')}}, children);
});

// myButton can now be used in multiple ways:

myButton('#the-button');
// props = {props: {id: 'the-button'}}
// children = []
myButton('#the-button.a-class', {props: {className: 'btn'}}, 'Click me');
// props = {props: {id: 'the-button', className: 'a-class btn'}}
// children = ['Click me']
myButton('Click me');
// props = {}
// children = ['Click me']

With hyperscript-string

import h from 'hyperscript-string';
import hh from 'hyperscript-helpers';
import classNames from 'classnames';
import normalize from 'hyperscript-normalize-args';

const {button} = hh(h);
const n = normalize({dom: false}); // no dom mode, i.e. string mode

// Creating a `myButton` component, which wraps `button` and always add "my-button" as css class
const myButton = n((props, children) => {
	const {
		class: className,
		...attrs
	} = props;
	return button({...attrs, class: classNames(className, 'my-button')}, children);
});

// myButton can now be used in multiple ways:

myButton('#the-button');
// props = {id: 'the-button'}
// children = []
myButton('#the-button.a-class', {class: 'btn'}, 'Click me');
// props = {id: 'the-button', class: 'a-class btn'}
// children = ['Click me']
myButton('Click me');
// props = {}
// children = ['Click me']

Related packages

  • hyperscript - Create HyperText with JavaScript, on client or server.
  • hyperscript-helpers - Terse syntax for hyperscript
  • hyperscript-string - Create HTML strings with JavaScript
  • snabbdom - A virtual DOM library with focus on simplicity, modularity, powerful features and performance.
  • virtual-dom - A batched diff-based DOM rendering strategy
  • @cycle/dom - The standard DOM Driver for Cycle.js, based on virtual-dom, and other helpers

API

normalize([options])

| Name | Type | Description | |------|------|-------------| | options | Object | Normalize options |

Returns: function, a normalize function. See examples above.

options.dom

Type: Boolean
Default: true

The mode to use with Cycle.js, hyperscript or virtual-dom.

When explicitly set to false it is good to use with hyperscript-string.

options.snabbdom

Type: Boolean
Default: false

The mode to use with snabbdom.

License

MIT © Joakim Carlstein