Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@antoniovdlc/map

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@antoniovdlc/map

Custom array map functions

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

map

version issues downloads license

Custom map functions for arrays.

Installation

This package is distributed via npm:

npm install @antoniovdlc/map

Motivation

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.

Usage

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");

Examples

select

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]

pick

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" } },
]
*/

License

MIT

Keywords

FAQs

Package last updated on 19 Nov 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc