deep-key-mirror
v0.6.1
Published
Alternative to React keyMirror
Downloads
29
Readme
Deep Key Mirror
Alternative to React's keyMirror which further mirrors properties deep inside the object graph.
Installation
npm install deep-key-mirror
Usage
deepKeyMirror(obj)
Returns a new object that has values equal to its property names in the given object.
Simple example
import deepKeyMirror from 'deep-key-mirror';
deepKeyMirror({ name: '', age: null }); // { name: 'name', age: 'age' }
If the given object has child arrays or objects, they are also "key-mirrored" recursively, with the .
-concatenated
paths from the root object assigned to each of their value.
Nested example
import deepKeyMirror from 'deep-key-mirror';
const breakfast = {
bread: null,
beverage: {
milk: null,
coffee: null,
beer: 'BEER!',
},
fruits: [{ name: 'orange' }, { name: 'apple' }],
};
const mirrored = deepKeyMirror(breakfast);
/*
mirrored === {
bread: 'bread',
beverage: {
milk: 'beverage.milk',
coffee: 'beverage.coffee',
beer: 'beverage.beer'
},
fruits: [
{ name: 'fruits[0].name' },
{ name: 'fruits[1].name' },
]
}
*/
TypeScript
TypeDoc-generated documentation is available here