Skip to content

micro-js/stringify-attrs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published