Skip to content

Similar to keymirror but supports nested objects, built with TypeScript

Notifications You must be signed in to change notification settings

kenberkeley/key-path-mirror

Folders and files

NameName
Last commit message
Last commit date
May 2, 2020
May 3, 2020
May 3, 2020
May 2, 2020
May 1, 2020
May 2, 2020
May 3, 2020
May 2, 2020
May 3, 2020
May 2, 2020
Jul 20, 2020

Repository files navigation

key-path-mirror

Build Status Version License JavaScript Style Guide

Similar to keymirror but supports nested objects, built with TypeScript.

§ Installation

$ npm i key-path-mirror
# or
$ yarn add key-path-mirror

§ Usage

import { keyPathMirror } from 'key-path-mirror'

keyPathMirror(obj: object, prefix?: string)

§ Examples

👉 REPL online example

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 :)
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

About

Similar to keymirror but supports nested objects, built with TypeScript

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published