split-key
v1.2.1
Published
Supercharge your JSON objects by mapping multiple keys to one value
Downloads
14
Readme
split-key
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
| Name | Description | Type | Default |
| ---- | -------------------------- | ------ | ------- |
| obj | Object to split value's of |object
| None |
| delim| Split delimiter |string
| /,\s?/
|
Returns
Type: object
splitKey.fromArray(arr, value)
Arguments
| Name | Description | Type | Default |
| ---- | -------------------------- | ------ | ------- |
| arr | Array to duplicate keys of |array
| None |
| value| Value to set |any
| None |
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