A flexible way to mirror object keys.
npm i @reasonx7/mirror-keysNode modules:
const mk = require('@reasonx7/mirror-keys');ES6 modules:
import mk from '@reasonx7/mirror-keys';Input:
const output = mk({
a: 'a',
b: null,
nested: {
x: 'x',
y: null,
},
});Output:
{
a: 'a',
b: 'b',
nested: {
x: 'x',
y: 'nested.y',
},
}Input:
const output = mk({
a: 'a',
b: null,
nested: {
x: 'x',
y: null,
},
}, 'PREFIX::');Output:
{
a: 'a',
b: 'PREFIX::b',
nested: {
x: 'x',
y: 'PREFIX::nested.y',
},
}Input:
const output = mk({
a: 'a',
b: null,
nested: {
x: 'x',
y: null,
},
}, mk.flow(mk.joinPath, key => key.replace('.', '-')));Output:
{
a: 'a',
b: 'b',
nested: {
x: 'x',
y: 'nested-y',
},
}Available modifiers:
mk.flow(...funcs): string- combines modifiers into one flow.mk.upper(key: string): string- converts key toUPPERCASE.mk.prefix(prefix: string): function- creates a function that adds a prefix to the key.mk.snake(prefix: string): string- converts key tosnake_case.mk.joinPath(key: string, path: string): string- adds a path to the nested key.