Socket
Socket
Sign inDemoInstall

deep-map

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

deep-map

Transforms primitive values of a JSON-like object


Version published
Weekly downloads
21K
decreased by-1.51%
Maintainers
1
Weekly downloads
 
Created
Source

deep-map

Version License Build Coverage Dependencies

Recurses through a JSON-like object and transforms its primitive values. Effectively applies array.map() to each nested array and _.mapValues() to each nested object.

Install

Install via npm:

npm install --save deep-map

Example

Suppose we have an object containing some nested template strings:

const templateObject = {
  name: '<%- name %>',
  email: '<%- email %>',
  keywords: [
    '<%- keyword1 %>',
    '<%- keyword2 %>'
  ],
  hobbies: {
    primary: '<%- hobby1 %>',
    secondary: '<%- hobby2 %>'
  }
};

And we want to fill it with the following data:

const data = {
  name: 'Samuel Johnson',
  email: 'sam.johnson@dictonary.com',
  keyword1: 'dictionary',
  keyword2: 'lexicography',
  hobby1: 'writing',
  hobby2: 'torying',
};

We can use deepMap like so:

const deepMap = require('deep-map');
const template = require('lodash/template');
const fs = require('fs');


let result = deepMap(templateObject, (value) => {
  return template(value)(data);
});


fs.writeFileSync('johnson.json', JSON.stringify(result, null, 2));

And here is the result:

{
  "name": "Samuel Johnson",
  "email": "sam.johnson@dictonary.com",
  "keywords": [
    "dictionary",
    "lexicography"
  ],
  "hobbies": {
    "primary": "writing",
    "secondary": "torying"
}

API

deepMap(object, transformFn, [options])

Performs a transformation on each primitive value in an object or array. Properties and indices are visited recursively, so nested primitives will be transformed. By default, a new object is returned without modifying the original or any of its nested objects.

object

object|array

The object to iterate over. This may be an object or an array, and may contain nested objects and/or arrays.

transformFn

function

The function to call for each primitive value. The return value of the function determines the transformed value. The function is invoked with two arguments:

  • value: The primitive value being transformed.

  • key/index: The name of the key at the present node in the case of an object, or the index value in the case of an array.

options

object (optional)

An options object. The following options are accepted:

  • thisArg: Set the this context within transformFn.

  • inPlace: Mutate the object passed-in rather than returning a new object. Nested objects are also transformed in-place.

Returns

object|array

Returns a new object, unless the inPlace option is set, in which case the old object is returned transformed.

License

Copyright © 2016 Akim McMath. Licensed under the MIT License.

Keywords

FAQs

Package last updated on 10 Feb 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc