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

direct-deep-map

v0.2.0

Published

Deep map values in a tree directly on the desired places, with strong TypeScript support.

Downloads

18

Readme

direct-deep-map Build Status install size

Deep map values in a tree directly on the desired places, with strong TypeScript support. Original tree is unchanged.

Install

$ npm install direct-deep-map

Note: if you're using TypeScript, you will need TypeScript 3.7 or above.

Usage

const { directDeepMap } = require('direct-deep-map');

const tree = {
    items: [
        {
            foo: { bar: 4 },
        },
        {
            foo: { bar: 7, baz: true },
        },
        {
            foo: { baz: 'hello' },
        },
    ],
};

const tripleBarsMapper = {
    items: [
        {
            foo: { bar: x => 3 * x },
        },
    ],
};

const newTree = directDeepMap(tree, tripleBarsMapper);

console.log(newTree);
//=> {
//   items: [
//     {
//       foo: { bar: 12 }
//     },
//     {
//       foo: { bar: 21, baz: true }
//     },
//     {
//       foo: { baz: 'hello' }
//     }
//   ]
// };

console.log(tree !== newTree);
//=> true

API

directDeepMap(tree, deepMapper)

tree

Type: Anything

If it is indeed a tree (i.e. an array or plain object), it will be deep-cloned and modified with the mapper. Otherwise, it will be returned unchanged.

The provided tree is unchanged.

deepMapper

Type: array or plain object

A tree-structure resembling the tree to be modified, but whose leaves are the mapping functions to apply.

The original tree will be traversed in BFS order and the corresponding mapper function within deepMapper (if present) will be used to transform the values.

TypeScript support

This module supports TypeScript by default. The return type of the directDeepMap method is properly constructed. Example:

import { directDeepMap } from 'direct-deep-map';

type OriginalTreeType = {
    items: Array<{
        foo: {
            bar?: number[];
            baz?: boolean;
        };
    }>;
};

const tree: OriginalTreeType = {
    items: [
        {
            foo: { bar: [1, 2, 3] },
        },
        {
            foo: { bar: [4, 5], baz: true },
        },
        {
            foo: { baz: false },
        },
    ],
};

const newTree = directDeepMap(tree, {
    items: [
        {
            foo: {
                // Type of `x` is automatically inferred to be `number[] | undefined` here
                bar: x => x!.join('-'),
            },
        },
    ],
});
type NewTreeType = typeof newTree;
//=> {
//   items: Array<{
//     foo: {
//       bar?: string;
//       baz?: boolean;
//     };
//   }>;
// }

Note that, in the example above, the mapper was defined as an object inline with the function call to directDeepMap. If you instead wish to define it separately, like in the first example in this readme, you will need to use as const when defining it:

const tree: OriginalTreeType = {
    items: [
        {
            foo: { bar: [1, 2, 3] },
        },
        {
            foo: { bar: [4, 5], baz: true },
        },
        {
            foo: { baz: false },
        },
    ],
};

const mapper = {
    items: [
        {
            foo: {
                bar: (x?: number[]) => x!.join('-'),
            },
        },
    ],
} as const;

const newTree = directDeepMap(tree, mapper);

Known issues & roadmap for v1.0.0

  • Support circular references (currently, the call will hang)
  • Support Set
  • Support Map
  • Improve documentation adding more examples
  • Support modifying the tree in-place instead of deep-cloning via an option (false by default)
  • Provide element index as second argument to the mapper function when mapping over elements in an array

Related

  • deep-map: Map over all primitive leaf values in a tree (instead of specifying specific positions since the beginning) - more flexible but less precise TypeScript typings.
  • tree-shortcut: Simplify an object tree with a shortcut. TypeScript supported.

License

MIT © Pedro Augusto de Paula Barbosa