2.0.0 • Published 5 years ago

deep-map v2.0.0

Weekly downloads
55,323
License
MIT
Repository
github
Last release
5 years ago

Deep Map

Version License Build Coverage

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's a deep version of Array.prototype.map() that works on all objects rather than just on Arrays. Circular references are supported.

To transform the keys of an object rather than its values, use Deep Map Keys.

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

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 © 20162019 Akim McMath. Licensed under the MIT License.

@bsgreenb/gatsby-source-contentful@lysander6/directusdirectus9-lite@infinitebrahmanuniverse/nolb-dee@everything-registry/sub-chunk-1460@angeloashmore/gatsby-source-wordpress@zilahir/gatsby-source-wordpress@adikari/config-manager@trptcolin/gatsby-source-contentful@sekmet/gatsby-source-wordpress@futureverse/nx-semantic-release-syncflexport-gatsby-source-contentfulgarden-cli@wonism/gatsby-source-wordpress@theunderscorer/nx-semantic-release@theplayerstribune/gatsby-source-wordpress-plus@otakism/gatsby-source-wordpress@martinleejones/gatsby-source-contentful@luudjanssen/gatsby-source-contentfuladobexd-object-utils@aclisp/directus@bradleyg/gatsby-source-wordpressmapbox-materialise-style-languageminimum-viable-editoroprahi3-statusmv-data-coremv-data-core-alpha-buildsynqueit@krvajal/gatsby-remark-relative-images@kennethormandy/gatsby-source-wordpress@kepi0809/gatsby-source-wordpress@kall/gatsby-source-wordpress@jonas.tang/mgcc-dms-api@madetech/gatsby-source-wordpress@fourpost/gatsby-source-eventbrite@triply/ratt@skuhnow/directus@splitmedialabs/gatsby-source-wordpress@sbp-datapull/json-configeysi-garden-test@sp-tools/kloud-config@sp-tools/kloud-stack@theanamhossain/gatsby-source-wordpressgatsby-source-apigatsby-source-eventbrite-multipleorgsgatsby-source-eventbritegatsby-source-levergatsby-source-lever-eugatsby-remark-component-imagesgatsby-remark-relative-body-imagesgatsby-remark-relative-images-v2gatsby-remark-relative-sourcegatsby-source-contentful-fastgatsby-source-contentful-richtext-safe@tkz/gatsby-source-contentful@xorifelse/gatsby-source-eventbritegatsby-source-wordpress-plus@ragg/gatsby-remark-relative-images@nearform/gatsby-source-contentful@pintob/gatsby-source-contentful@ptim/gatsby-source-wordpress@mariolopjr/gatsby-remark-relative-images@clue/gatsby-source-contentful@dekdekbaloo/gatsby-source-wordpress@datapull/json-config@cynova/test1@drubin/garden-cli@essilor-ditac/gatsby-source-contentful@dylanvann/gatsby-source-wordpress
2.0.0

5 years ago

1.5.0

7 years ago

1.4.2

8 years ago

1.4.1

8 years ago

1.4.0

8 years ago

1.3.2

8 years ago

1.3.1

8 years ago

1.3.0

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago