Attribute Value
The @bento/to-attribute-value package is a formatter that converts any given
value into a string that can be used as an HTML attribute value.
Installation
npm install --save @bento/to-attribute-value
stringify
The package exposes the stringify function as the default export. The sole
purpose of this function is to format any given value into a string that
closely resembles the value as if it was formatted as an HTML attribute value.
The function accepts the following arguments:
import { stringify } from '@bento/to-attribute-value';
console.log(stringify({ hello: 'world'}));
We automatically format the data based on the type of the value given:
Any unknown object is converted using JSON.stringify. In order for these
custom objects to be converted, they must have a toJSON method that returns a
serializable object.
The value false is automatically converted to undefined to signal React that
the resulting attribute should not end up in the DOM.