expand-obj
📦 NODE.JS - Create a multi-key object with the same/multiple values by passing just a string or array.

Installation
npm install expand-obj --save
or
yarn add expand-obj
Importation
commonjs
const expand = require('expand-obj');
or ES6 (export default)
import expand from 'expand-obj';
or ES6 (named export)
import { expand } from 'expand-obj';
How to use
Basically you just need to enter an object that contains one or more keys separated by some character or an array as a key.
const expand = require('expand-obj');
const foo = expand({
['a, b, c']: 123,
});
console.log(foo);
With options
const foo = expand(
{
['a, b, c']: [1, 2, 3],
},
{ splitValues: true }
);
console.log(foo);
Resolve functions
const foo = await expand({
['a, b, c']: async (val: string) => `test=${val}`,
});
console.log(foo);
React example
const expand = require('expand-obj');
const styled = await expand({
'h1, h2, p, span': { fontSize: '2rem', fontWeight: 'bold' },
'roundedBorder, cardBorder, buttonBorder': { borderRadius: '7px' },
'span': { fontStyle: 'italic' },
});
<span style={styled.span}>Font Size 2rem and Italic</span>
<SomeReactComponent style={{...styled.h1, ...styled.roundedBorder}} />
Options
By default these are the configuration options
export type ExpandOptions = {
separator?: string,
splitValues?: boolean,
deleteRawKey?: boolean,
trimSpaces?: boolean,
tryJoinRepeatedKeys?: boolean,
resolveFuncs?: boolean,
useSubkeyAsParams?: boolean,
};
separator (default = ",")
define the subkey separator
const options = {
separator: '|',
};
const obj = await expand(
{
'a, b, c': 123,
},
options
);
console.log(obj);
splitValues (default = false)
when true, spreads the values if the property value is of type array
const options = {
splitValues: true,
};
const obj = await expand(
{
'a, b, c': 123,
'h, i, j': [4, 5, 6],
'x, y, z': [7, 8],
},
options
);
console.log(obj);
deleteRawKey (default = true)
when false, prevents the raw key from being removed from object entries
const options = {
deleteRawKey: false,
};
const obj = await expand(
{
'a, b, c': 123,
},
options
);
console.log(obj);
trimSpaces (default = true)
when false, keep leading and trailing spaces
const options = {
trimSpaces: false,
};
const obj = await expand(
{
'a, b, c': 123,
},
options
);
console.log(obj);
tryJoinRepeatedKeys (default = true)
when true, if the input object has a key that is equal to a subkey and both values are an array or an object the two values will be merged
const options = {
tryJoinRepeatedKeys: false,
};
const obj = await expand(
{
'a, b, c': 123,
'foo, bar': [4, 5, 6],
foo: [789],
},
options
);
console.log(obj);