1.0.0 • Published 8 years ago

to-set v1.0.0

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

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'
};
*/
1.0.0

8 years ago

0.1.0

8 years ago