@corex/flatten

A zero dependency object merger with typescript support and built in common merge utilities.

Usage no npm install needed!

<script type="module">
  import corexFlatten from 'https://cdn.skypack.dev/@corex/flatten';
</script>

README


title: '@corex/flatten' custom_edit_url: 'https://github.com/iamvishnusankar/corex/edit/master/packages/deepmerge/README.md'

import { flatten, unflatten } from '@corex/flatten'

const input = {
  1: {
    a: 'aaa',
    2: {
      b: 'bbb',
    },
  },
  here: {
    is: 'my',
    object: 'with',
    stuff: 'in it',
  },
  empty: {},
  emptyNested: {
    hello: null,
  },
  emptyArr: [],
  emptyArrNested: {
    hello: [],
    world: [null],
  },
  containing: {
    many: [
      'things',
      'inside',
      {
        nest: {
          hello: 'world',
        },
      },
      undefined,
      null,
      {
        nest: {
          arr: [1, 2, 4],
        },
      },
    ],
  },
}

const result = {
  '1.a': 'aaa',
  '1.2.b': 'bbb',
  'here.is': 'my',
  'here.object': 'with',
  'here.stuff': 'in it',
  'containing.many[0]': 'things',
  'containing.many[1]': 'inside',
  'containing.many[2].nest.hello': 'world',
  'containing.many[3]': undefined,
  'containing.many[4]': null,
  'containing.many[5].nest.arr[0]': 1,
  'containing.many[5].nest.arr[1]': 2,
  'containing.many[5].nest.arr[2]': 4,
  empty: {},
  emptyArr: [],
  'emptyArrNested.hello': [],
  'emptyArrNested.world[0]': null,
  'emptyNested.hello': null,
}

flatten(input) // => result

unflatten(result) // => input