Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
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 via npm:
npm install --save deep-map
TypeScript declarations are also included in the package. Just import the package everything will just work.
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"
}
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|array
The object to transform. This may be an object or an array, and may contain nested objects and/or arrays.
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.
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.
object|array
Returns a new object, unless the inPlace
option is set, in which case the
old object is returned transformed.
Copyright © 2016 Akim McMath. Licensed under the MIT License.
FAQs
Transforms nested values of complex objects
We found that deep-map demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.