map-sift
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

mapSift

Perform map and filter operations in the same iteration

Install

npm install map-sift

Usage

import mapSift from 'map-sift'

type Person = { name: string, height?: number }

const people: Person[] = [
    { name: 'Alice', height: 180 },
    { name: 'Bob', height: 168 },
    { name: 'David', height: undefined },
    { name: 'Emma', height: 160 },
    { name: 'Frank' },
    { name: 'Grace', height: 155 },
]

// Without mapSift:
const heightsMapFilter = people
    .map(person => person.height)
    .filter(Boolean)
console.log(heightsMapFilter) // [180, 168, 160, 155]

// With mapSift:
const heightsMapSift = people
    .flatMap(mapSift(person => person.height))
console.log(heightsMapSift) // [180, 168, 160, 155]

Package Sidebar

Install

npm i map-sift

Weekly Downloads

5

Version

1.0.3

License

MIT

Unpacked Size

4.54 kB

Total Files

7

Last publish

Collaborators

  • mpoc