1.0.0 • Published 8 years ago

to-dataset v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

To Dataset

Object to DOM Dataset

API

  toDataset(hash, {data = {}, allowFalse = true} = {});

hash

The object to convert to a dataset, every key will get the data prefix.

  • Hash values can be of type String, Number, Boolean, Function and Object.
  • Strings, Numbers and Booleans will be parsed as string values.
  • Functions will be called and recursively added to the dataset.
  • Objects will be recursively added to the dataset.

data (optional)

The data object where the dataset will be merged into. Defaults to {}.

allowFalse (optional)

Set falsy dataset attributes, otherwise ignore. Defaults to true.

Example

import toDataset from 'to-dataset';

const data = toDataset({
  component: 'link',
  'is-false': false,
  'is-deep': {
    deep: 1
  },
  'is-method': () => 'hello',
  'is-method-deep': () => ({
    a: 1,
    b: 2
  }),
  no: false
});

data === {
  'data-component': 'link',
  'data-is-false': 'false',
  'data-is-deep-deep': '1',
  'data-is-method': 'hello',
  'data-is-method-deep-a': '1',
  'data-is-method-deep-b': '2',
  'data-no': 'false'
};
1.0.0

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago