npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

deep-clone-map

v1.4.1

Published

Deep clone and map complex nested objects

Downloads

713

Readme

Deep Clone Map

npm version

Install | API | Usage | Tests | TypeScript

Deep Clone Map maps any object or array and transforms its primitive values, always returning a new instance, it can map deeply nested values in complex objects and arrays. Think of it as Array.prototype.map() on steriods, capable to map objects and deeply nested structure.

Differences between other deep map libraries

Most existing libraries do not map values in arrays, and in nested complex structures combining both objects and arrays. Typescript support is also one of the lacking features of most existing libraries. A big advantage of Deep Clone Map is that it has zero dependencies.

Size

Deep Clone Map size is really tiny somewhere between 242 bytes up to 440 bytes minified and gzipped, depending on the algorithm used.

Performance

Deep Clone Map has a performance on par with other popular alternatives, but it doesn't use any dependencies, and in some instances provides more functionality.

Some benchmarks running on MacOS Catalina and Node v12.13.0 using benchmark library:

The code for benchmarks is located under the benchmark folder in the github repository.

Install

npm:

npm install --save deep-clone-map

yarn:

yarn add deep-clone-map

API

deepCloneMap(any, mapFn?)

Returns

Returns a new deeply cloned version of the input argument value, it will maintain the exact same structure as the original object or array. In case if a primitive is provided as the first argument it will map its value to a new one, based on the callback function.

Usage

Import Deep Clone Map package

import deepCloneMap from 'deep-clone-map'

Note: the library code is ES6, if you don't have an environment that supports it, you will gonna have to transpile the module's code yourself using babel. If you are using webpack and babel you will gonna have to ignore the module specifically in your config. Otherwise you can use the already commonjs ready provided es5 module.

For es5 support
import deepCloneMap from 'deep-clone-map/dist/es5'
Browser

A browser ready bundle is provided in the node module at deep-clone-map/dist/browser/index.js

Deeply clone an object:

const obj = {
  a: 1,
  b: 2,
  c: {
    a: 1,
    b: 2,
    c: [1, 2, 3],
  },
}

const newObj = deepCloneMap(obj)
// newObj !== obj && newObj.c !== obj.c && newObj.c.c !== obj.c.c

Deeply clone an array:

const arr = [
  [1, 2, 3],
  [
    {
      a: 1,
      b: 2,
      c: [1, 2, 3],
    },
  ],
]

const newArr = deepCloneMap(arr)
// newArr !== arr && newArr[1] !== arr[1] && newArr[1].c !== arr[1].c

Deeply map an object

const obj = {
  a: 1,
  b: 2,
  c: {
    a: 1,
    b: 2,
    c: [1, 2, 3],
  },
}

const newObj = deepCloneMap(obj, val => val + 1)
/*
    newObj => {
      a: 2,
      b: 3,
      c: {
        a: 2,
        b: 3,
        c: [2, 4, 4]
      }
    }
  */

Deeply custom map an object based on the key

const obj = {
  a: 1,
  b: 2,
  c: {
    a: 1,
    b: 2,
    c: [1, 2, 3],
  },
}

const newObj = deepCloneMap(obj, (val, key) => {
  switch (key) {
    case 'a':
      return 10
    case 'c.b':
      return 20
    case 'c.c[1]':
      return 20
    default:
      return val
  }
})
/*
    newObj => {
      a: 10,
      b: 2,
      c: {
        a: 1,
        b: 20,
        c: [1, 20, 3]
      }
    }
  */

Deeply map a nested array

const arr = [
  [1, 2, 3],
  [
    {
      a: 1,
      b: [1, 2, 3],
      c: [
        {
          a: 1,
          b: [1, 2, 3],
        },
      ],
    },
  ],
]

const newArr = deepCloneMap(arr, val => val + 1)
/*
    newArr => [
      [2, 3, 4],
      [
        {
          a: 2,
          b: [2, 3, 4],
          c: [
            {
              a: 2,
              b: [2, 3, 4]
            }
          ]
        }
      ]
    ]
  */

Tests

In order to run the provided unit tests:

  # yarn
  yarn test

  # npm
  npm test

Typescript

The packages comes with typescript declarations included in the package, you only need to import the module normally.

By default the types are infered from the input argument:

const obj = {
  a: 1,
  b: 2,
}

const newObj = deepCloneMap(obj)
/*
    newObj => {
      a: number
      b: number
    }
  */

In some cases you will need to provide a different type to the deepCloneMap function, for example in instances when you map the primitive values to a different type:

  const obj = {
    a: 1,
    b: 2
  }

  const newObj = deepCloneMap<{ a: string; b: string }>(obj, val => String(val))