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

@enkidevs/deepmapper

v2.0.0

Published

Utility for mapping arbitrary structures

Downloads

121

Readme

deepmapper

CircleCI npm version

Map an arbitrary JS structure by altering any portion of it, including itself.

const obj = {
  a: [
    1,
    2,
    {
      value: 3
    },
    {
      child: [
        4,
        {
          value: 5
        }
      ]
    }
  ],
  b: 6,
  c: {
    value: 7
  }
};

const result = deepMapper(obj, x => {
  if (Array.isArray(x)) {
    return x.slice();
  }
  if (typeof x === "number") {
    return 2 ** x;
  }
  return { ...x };
});

// gives
/*
const obj = {
  a: [
    2,
    4,
    {
      value: 8,
    },
    {
      child: [
        16,
        {
          value: 32
        }
      ]
    },
  ],
  b: 64,
  c: {
    value: 128
  }
};
*/

The mapping is done as a pre-order traversal. This means that, if you change the parent by removing some of its children, the removed children won't be mapped.

deepMapper({ child: { value: "x" } }, item => {
 if (isObject(item)) {
   return {
     child: "I am changed"
   };
 }
 return item;
});

// gives
/*
{
 child: 'I am changed'
}
*/

Commonly, you'd want to map values in an immutable fashion:

deepMapper({
  nested: [
    1,
    'test',
    {
      bottom: true
    }
  ]
}, item => {
  if (Array.isArray(item)) {
    return item.slice() // this will do a shallow copy of the nested array but not its values
  }
  if (item && typeof item === 'object') {
    return { ...item }; // this will do a shallow copy of the top object, and the object with the 'bottom' key
  }
  // primitives in JS are immutable so we're fine here
  return item;
})

The above will essentially perform a clone operation on the above object.

Here's how you'd write a deep clone function using deepmapper and shallow-clone to handle all possible values:

const deepMapper = require("@enkidevs/deepmapper");
const shallowClone = require("shallow-clone");

const obj = { a: [{ b: "test" }, 1, /abc/g], c: false, d: new Date(2) };

const cloned = deepMapper(obj, shallowClone);

Features

  • Can map any primitive value
deepMapper(1, n => n + 1) === 2        // true
deepMapper('a', s => s + 'b') === 'ab' // true
deepMapper(true, b => !b) === false    // true
// ...
  • Handles circular references
// circular references get properly mapped
const obj = { a: [1, 2, 3], b: { loop: null } };
obj.b.loop = obj.a;

const result = deepMapper(obj, item => {
  if (Array.isArray(item)) {
    return item.slice();
  }
  if (item && typeof item === 'object') {
    return { ...item };
  }
  return item + 1;
})

// gives
/*
{
  a: [2, 3, 4],
  b: {
    loop: // points to the mapped result.a, not the original obj.a
  },
};
*/
  • Doesn't break on repeated references
const ref = { a: [1, 2, 3] };
const arr = [ref, ref, { b: ref }, { c: { value: ref } }];

const result = deepMapper(arr, item => {
  if (Array.isArray(item)) {
    return item.slice();
  }
  if (item && typeof item === 'object') {
    return { ...item };
  }
  return item + 1;
});

// gives
/*
{ a: [2, 3, 4] },
{ a: [2, 3, 4] },
{ b: { a: [2, 3, 4] } },
{ c: { value: { a: [2, 3, 4] } } },
*/

Practical examples

  • Obfuscate MongoDB object by changing all MongoDB ObjectID _id keys to id:
function cleanMongoId(item) {
  if (Array.isArray(item)) {
    return item.slice();
  }
  if (isObject(item)) {
    if (ObjectId.isValid(item)) {
      return item;
    }
    // only change the _id property if it's a valid ObjectId
    if (ObjectId.isValid(item._id)) {
      const { _id, ...cleanItem } = item;
      if (_id) {
        cleanItem.id = _id;
      }
      return cleanItem;
    }
    return { ...item };
  }
  return item;
}

const id1 = ObjectId();
const id2 = ObjectId();
const doc = [
  { nested1: { _id: id1, whatever: 5 } },
  { nested2: { _id: id2, whatever: 5 } },
];

const result = deepMapper(doc, cleanMongoId);

// gives
/*
[
  { nested1: { id: id1, whatever: 5 } },
  { nested2: { id: id2, whatever: 5 } },
]
*/
  • Changing state structures in a Redux reducer:
// ...
const state = {
  items: [
    {
      id: 'todo-1',
      data: {
        createdAt: Date,
        updatedAt: Date,
        order: Number,
        text: 'yipikaye'
      }
    },
    // ...
  ]
}
// ... somewhere in a reducer for action { type: 'CHANGE_UPDATED_AT' id: 'todo-1', updatedAt: new Date() }
case [CHANGE_UPDATED_AT]:
  return deepMapper(state, chunk => {
    if (Array.isArray(chunk)) {
      return chunk.slice();
    }
    if (chunk && typeof chunk === 'object') {
      if (chunk.id === action.id) {
        return {...chunk, updatedAt: action.updatedAt };
      }
      return {...chunk };
    }
    return chunk;
  })

License

MIT