npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

deep-map

v2.0.0

Published

Transforms nested values of complex objects

Downloads

78,547

Readme

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: '[email protected]',
  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: '[email protected]',
  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 © 2016–2019 Akim McMath. Licensed under the MIT License.