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 nested values of complex objects


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

Deep Map

Version License Build Coverage Dependencies

Install | Usage | API | TypeScript | License

Deep Map recurses through an object and transforms its primitive values – including nested values – according to some function. Essentially, it works like Array.prototype.map(), except that it works on all objects rather than just on Arrays.

Install

Install Deep Map via npm.

npm install --save deep-map

Usage

Let's say we have an object like this:

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

And we want to fill it with this data:

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

We can use Deep Map like this:

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

let result = deepMap(info, value => template(value)(data));

And the result looks like this:

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

API

deepMap(object, mapFn, [options])
Parameters
ParamTypeDescription
objectany The object whose values are to be transformed. Typically, this will be a complex object containing other nested objects. This object may be an Array, and may contain nested arrays whose values will be deeply transformed in the same way. In the unlikely use-case in which a primitive value is passed, `mapFn()` will be applied once and the result returned.
mapFnfunction The function used to transform each primitive value. The function is called with two arguments:
  • value <any> The value being transformed
  • key <string | number> The key name or numerical index of the value being transformed
The return value determines the value at the same position on the resulting object.
[options]object An optional options object. The following options are accepted:
  • inPlace <boolean = false> Transform object in-place rather than constructing a new object
  • thisArg <any = undefined> Sets the value of this within mapFn()
Returns

Returns a new object with the same keys as object. If options.inPlace is set to true, the original object is returned mutated.

TypeScript

TypeScript declarations are included in the package. Just import the module, and things will just work.

By default, the compiler will assume that the return value will have the same shape as the input object. In most use cases, this is likely to be true. But in some cases – like the one below – the assumption breaks down.

function isPositive(n: number): boolean {
  return n >= 0;
}

// COMPILER ERROR: number not assignable to boolean :(
let bool: boolean = deepMap({n: 2}, isPositive).n;

Pass a type argument to describe the shape of the return value, and everything will be happy.

let bool: boolean = deepMap<{n: boolean}>({n: 2}, isPositive).n; // :)

License

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

Keywords

FAQs

Package last updated on 16 Jun 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