extract-data-options
Extract options from data attributes:
// Import the modulevar extractDataOptions = // Get the elementvar element = document // Extract the options from `data-carousel-*` attributesvar options =
The function will return:
index: 0 autoPlay: true controls: prev: '.js-prev' next: '.js-next' dots: '.my-dots'
installation
npm install extract-data-options --save
usage
function
extractDataOptions(element: HTMLElement, namespace: String) : Object
If you don't pass a namespace, it will return all data-* options.
camel case
This module will automatically convert the attributes names into camelCase. So, an attribute like data-example-my-option
will be turned into myOption
.
nested properties
You can use nested properties too. Just use a dot (.
) to define the keypath:
data-example-prop.show.example="hello"
, will result into:
prop: show: example: 'hello'
OBS: Don't worry about this notation with dots. It is a valid HTML5 standard.
json
This module will always attempt to parse the values as JSON, otherwise it will set the value as a String.
The following options:
Will produce the result below:
boolean: true number: 314 object: key: 'value' array: 1 2 3 string: 'A simple string'
tests
Install all dependencies:
npm install
Run the tests:
npm test
license
MIT