@f/stringify-attrs

Turn an attributes object into a string

Stats

StarsIssuesVersionUpdatedCreatedSize
@f/stringify-attrs
001.0.55 years ago6 years agoMinified + gzip package size for @f/stringify-attrs in KB

Readme

stringify-attrs

Build status Git tag NPM version Code style

Turn an attributes object into a string

Installation

$ npm install @f/stringify-attrs

Usage

var stringifyAttrs = require('@f/stringify-attrs')

stringifyAttrs({href: 'http://www.google.com', class: 'link'}) === ' href="http://www.google.com" class="link"'

API

stringifyAttrs(attrs, skip)

  • attrs - An object of key/value pairs representing attributes
  • skip - Optional. Map of attribute names to skip. E.g. {innerHTML: true}. This is useful if there are certain attribute names that don't make sense when rendered as strings.

Returns: A string containing the attributes in key=value style. Returns empty string if falsy value passed.

Prepended space

The returned string starts with a prepended space (e.g. ' class="link"'). This makes the code short, simpler, and faster, and also comports better with the most common use-case, which is rendering a DOM element as a string (if you don't have the prepended space, you have to again awkwardly check whether attrs is empty).

License

MIT

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.