to-set

1.0.0 • Public • Published

Object to hyphenated set

  toSet({ foo: 'bar', nofoo: false, deep: { foo: bar } })
  // => { foo: 'bar', 'deep-foo': 'bar' }

API

  toSet(hash, {data = {}, prefix = '', allowFalse = true} = {});

hash

The object to convert to a hyphenated set.

  • 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 set.
  • Objects will be recursively added to the set.

data (optional)

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

allowFalse (optional)

Set falsy attributes, otherwise ignore. Defaults to false.

prefix (optional)

Prefixes all objects in hash, except for the optional attributes (in data) when given.

Example

import toSet from 'to-set';

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

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

Example how to create data attributes

import toSet from 'to-set';

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

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

Readme

Keywords

Package Sidebar

Install

npm i to-set

Weekly Downloads

2

Version

1.0.0

License

MIT

Last publish

Collaborators

  • ehtb