Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
@antoniovdlc/map
Advanced tools
Custom map functions for arrays.
This package is distributed via npm:
npm install @antoniovdlc/map
Mapping over arrays is a common operation in JavaScript, so this library provides some common custom map functions to have a more declarative way of mapping over arrays.
You can use this library either as an ES module or a CommonJS package:
import { select, pick } from "@antoniovdlc/map";
- or -
const { select, pick } = require("@antoniovdlc/map");
The select
map function picks a single value based on a key path in an arry of objects:
import { select } from "@antoniovdlc/map";
const arr = [
{ name: "Bob", age: 23 },
{ name: "Alice", age: 32 },
{ name: "Tom", age: 60 },
{ name: "Candice", age: 45 },
];
arr.map(select("age")); // [23, 32, 60, 45]
If in some objects, the key path doesn't exist, the returned value for that index will be null
:
import { select } from "@antoniovdlc/map";
const arr = [
{ name: "Bob", age: 23 },
{ name: "Alice", age: 32 },
{ name: "Tom", age: 60 },
{ name: "Tim" },
{ name: "Candice", age: 45 },
];
arr.map(select("age")); // [23, 32, 60, null, 45]
The pick
map function is similar to the select
one, but it allows for picking multiple key paths in an array of objects:
const arr = [
{ name: { first: "Bob", last: "Garcia" }, age: 23 },
{ name: { first: "Alice", last: "Doe" }, age: 32 },
{ name: { first: "Tom", last: "Dupont" }, age: 60 },
{ name: { first: "Candice", last: "Smith" }, age: 45 },
];
arr.map(pick("name.first", "age"));
/*
[
{ name: { first: "Bob" }, age: 23 },
{ name: { first: "Alice" }, age: 32 },
{ name: { first: "Tom" }, age: 60 },
{ name: { first: "Candice" }, age: 45 },
];
*/
The pick
map function always returns an array of objects, even if it is only passed a single key path:
const arr = [
{ name: "Bob", age: 23 },
{ name: "Alice", age: 32 },
{ name: "Tom", age: 60 },
{ name: "Candice", age: 45 },
];
arr.map(pick("age"));
// [{ age: 23 }, { age: 32 }, { age: 60 }, { age: 45 }];
Finally, it ignores key paths that don't exist in the objects:
const arr = [
{
name: { first: "Bob", last: "Garcia" },
age: { value: 23, birthday: { year: 1999, month: 3 } },
},
{
name: { first: "Alice", last: "Doe" },
age: { value: 32, date: { year: 1990, month: 1 } },
},
{
name: { last: "Dupont" },
age: { value: 60, date: { year: 1962, month: 10 } },
},
{
name: { first: "Candice", last: "Smith" },
age: { value: 45, birthday: { month: 8 } },
},
];
arr.map(pick("name.first", "age.birthday.year"));
/*
[
{ name: { first: "Bob" }, age: { birthday: { year: 1999 } } },
{ name: { first: "Alice" } },
null,
{ name: { first: "Candice" } },
]
*/
MIT
FAQs
Custom array map functions
We found that @antoniovdlc/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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.