JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 29027
  • Score
    100M100P100Q138913F
  • License MIT

Transforms primitive values of JSON-like objects

Package Exports

  • deep-map

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (deep-map) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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

TypeScript declarations are also included in the package. Just import the package everything will just work.

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@dictionary.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@dictionary.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 transform. 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.