Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
direct-deep-map
Advanced tools
Deep map values in a tree directly on the desired places, with strong TypeScript support.
Deep map values in a tree directly on the desired places, with strong TypeScript support. Original tree is unchanged.
$ npm install direct-deep-map
Note: if you're using TypeScript, you will need TypeScript 3.7 or above.
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
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.
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.
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);
Set
Map
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.FAQs
Deep map values in a tree directly on the desired places, with strong TypeScript support.
The npm package direct-deep-map receives a total of 15 weekly downloads. As such, direct-deep-map popularity was classified as not popular.
We found that direct-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.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.