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-map-object

v0.0.5

Published

The utility for deep mapping object's keys and values

Downloads

6,402

Readme

deep-map-object v0.0.5

The utility for deep mapping object keys and values

Why deep-map-object?

The util help facilitating the deep transform from one object to another by given helpers function.

Install:

npm i deep-map-object

Usage:

ES6

import deepMap from 'deep-map-object';

const options = function(value) {
    return value + 1;
};

const data = {
    x: 1,
    y: {
        z: [2, 3],
        t: 4
    }
};

const result = deepMap(options)(data);

console.log(result);
/*
{
    x: 2,
    y: {
        z: [3, 4],
        t: 5
    }
}
*/

ES5

var deepMap = require('deep-map-object');

var options = function(value) {
    return value + 1;
};

var data = {
    x: 1,
    y: {
        z: [2, 3],
        t: 4
    }
};

var result = deepMap(options)(data);

console.log(result);
/*
{
    x: 2,
    y: {
        z: [3, 4],
        t: 5
    }
}
*/

Api:

deepMap(options)(obj)

deepMap: function(options: function|object) => (resolve: function(value: any) => result:any)

  • deepMap(options) will create a resolve function which can transform(resolve) object into another format.
  • the resolve function is the util function that help you to loop deeper into input value.
  • options may be a function that take a primitive value and transform it to target value or may take a object with below format:
import deepMap from 'deep-map-object';

const data = { x: 1 };

const result = deepMap(value => value + 1)(data);

console.log(result); // {x : 2}

mapValue: function(value: primitive, resolve: function(value: any))

import deepMap from 'deep-map-object';

const data = { x: 1 };

const result = deepMap({ mapValue: value => value + 1 })(data);

console.log(result); // {x : 2}

mapArray: function(value: array, resolve: function(value: any))

import deepMap from 'deep-map-object';

const data = { a: [1, 2, 3] };

const result = deepMap({ mapArray: values => values.map(value => value + 1) })(data);

console.log(result); // { a: [2, 3, 4] };

mapObject: function(value: object, resolve: function(value: any))

import deepMap from 'deep-map-object';

const data = { a: { num: 0 }, b: { num: 1 } };

const result = deepMap({ 
   mapObject: (obj, resolve) => obj.num > 0 
       ? {...obj, num: obj.num + 1 } 
       : deepMap.mapObject(obj, resolve) 
})(data);

console.log(result); // { a: { num: 0 }, b: { num: 2 } };

mapFn: function(value:any, resolve: function(value: any)) // If mapFn is used, mapValue, mapArray, mapObject options will be disabled

import deepMap from 'deep-map-object';

const data = { a: { num: 0 }, c: { d: [1, 2] } };

const result = deepMap({ 
   mapFn: (val, resolve) => {
     if (Array.isArray(val)) return val.map(resolve);
     if (val instanceof Object) return deepMap.mapObject(val, resolve);
     
     return val + 1;
   } 
})(data);

console.log(result); // { a: { num: 1 }, c: { d: [2, 3] } };
  • Based on the type of variable we want to convert, it may be object, array or primitive value.
  • deepMap internally support you to loop deeply in nested object, so you only need to care about transform business

Static functions

deepMap.mapArray: function(value: array, resolve: function(value: any))

  • default mapArray util which run resolve to every array's items

deepMap.mapObject: function(value: object, resolve: function(value: any))

  • default mapObject util which run resolve to every object's key