1.0.1 • Published 7 years ago
@reasonx7/mirror-keys v1.0.1
mirror-keys
A flexible way to mirror object keys.
Installation
npm i mirror-keysUsage
Importing
Node modules:
const mk = require('mirror-keys');ES6 modules:
import mk from 'mirror-keys';Scenarios
No options
Input:
const output = mk({
a: 'a',
b: null,
nested: {
x: 'x',
y: null,
},
});Output:
{
a: 'a',
b: 'b',
nested: {
x: 'x',
y: 'nested.y',
},
}Prefix
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',
},
}Custom
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.