1.2.1 • Published 7 years ago

split-key v1.2.1

Weekly downloads
5
License
MIT
Repository
github
Last release
7 years ago

split-key

npm version Build Status XO code style

Supercharge your JSON objects by mapping multiple keys to one value

Install

npm install --save split-key

Usage

const splitKey = require('split-key');

splitKey({
  'primary, secondary': {
    color: 'red'
  }
});
//=> {
//  primary: {
//    color: 'red'
//  },
//  secondary: {
//    color: 'red'
//  }
// }

Use the included fromArray function if you have flat input of a single array. This function also performs with more speed than fromArray(). The downside is that it is more verbose for anything beyond a single value:

const arr = ['primary', 'secondary', 'tertiary'];
splitKey.fromArray(arr, {
  color: 'purple'
});
//=> {
//  primary: {
//    color: 'red'
//  },
//  secondary: {
//    color: 'red'
//  }
// }

API

splitKey(obj, delim)

Arguments

NameDescriptionTypeDefault
objObject to split value's ofobjectNone
delimSplit delimiterstring/,\s?/

Returns

Type: object


splitKey.fromArray(arr, value)

Arguments

NameDescriptionTypeDefault
arrArray to duplicate keys ofarrayNone
valueValue to setanyNone

Returns

Type: object

Advanced Usage

Set a custom delimiter to use split-key with any other character-separated formats:

splitKey({
  'key1a key1b': 'value1',
  'key2a key2b key2c': 'value2'
}, /\s/);
//=> {
//     key1a: 'value1',
//     key1b: 'value1',
//     key2a: 'value2',
//     key2b: 'value2',
//     key2c: 'value2'
//   };

License

MIT © Dawson Botsford

1.2.1

7 years ago

1.2.0

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago