Skip to content

tkqubo/deep-key-mirror

Repository files navigation

Deep Key Mirror

npm version License CI

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({ null: '', 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

About

Alternative to React's keyMirror

Resources

License

Stars

Watchers

Forks

Packages

No packages published