@f/stringify-attrs
v1.0.5
Published
Turn an attributes object into a string
Downloads
46
Readme
stringify-attrs
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 attributesskip
- 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