https://github.com/kenberkeley/key-path-mirror
Similar to keymirror but supports nested objects, built with TypeScript
https://github.com/kenberkeley/key-path-mirror
key-mirror keymirror mirror nested nested-objects
Last synced: 10 months ago
JSON representation
Similar to keymirror but supports nested objects, built with TypeScript
- Host: GitHub
- URL: https://github.com/kenberkeley/key-path-mirror
- Owner: kenberkeley
- Created: 2020-04-30T22:58:04.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T04:47:31.000Z (about 3 years ago)
- Last Synced: 2025-04-15T04:12:52.624Z (10 months ago)
- Topics: key-mirror, keymirror, mirror, nested, nested-objects
- Language: TypeScript
- Size: 1.43 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# key-path-mirror
> Similar to [keymirror](https://www.npmjs.com/package/keymirror) but supports nested objects, built with TypeScript.
## § Installation
```sh
$ npm i key-path-mirror
# or
$ yarn add key-path-mirror
```
## § Usage
```ts
import { keyPathMirror } from 'key-path-mirror'
keyPathMirror(obj: object, prefix?: string)
```
## § Examples
> 👉 [REPL online example](https://repl.it/repls/BrightBothDevices)
```ts
const nestedObject = {
a: 123,
b: {
c: 'hello',
d: {
e: 'world'
}
},
f: {
g: {
h: {
i: () => { console.log('hello world') }
},
j: 123
},
k: undefined
},
l: new Date()
}
const expectedKeyPathMirroredObject = {
a: 'a',
b: {
c: 'b.c',
d: {
e: 'b.d.e'
}
},
f: {
g: {
h: {
i: 'f.g.h.i'
},
j: 'f.g.j'
},
k: 'f.k'
},
l: 'l'
}
console.assert(
JSON.stringify(keyPathMirror(nestedObject)) ===
JSON.stringify(expectedKeyPathMirroredObject)
) // no errors :)
```
```ts
const prefix = 'foobar:'
const nestedObject = {
a: 123,
b: {
c: 'hello',
d: {
e: null
}
}
}
const expectedPrefixedKeyPathMirroredObject = {
a: 'foobar:a',
b: {
c: 'foobar:b.c',
d: {
e: 'foobar:b.d.e'
}
}
}
console.assert(
JSON.stringify(keyPathMirror(nestedObject, prefix)) ===
JSON.stringify(expectedPrefixedKeyPathMirroredObject)
) // no errors :)
```
## § Alternatives
* https://github.com/tkqubo/deep-key-mirror
* https://github.com/apolkingg8/KeyMirrorNested
* https://github.com/werk85/pathmirror
* https://github.com/venkyjs/KeyMirrorPlus
* https://github.com/filenwind/keymirror-nested