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

@mlturner/filter-map

v1.0.0

Published

Filter and map a collection at the same time

Downloads

2

Readme

@mlturner/filter-map

Filter and map a collection (array-like, Map, Set, Generator or plain object) at the same time. This is done simultaneously versus filtering and then mapping. If collection is null or undefined then an empty array is returned so no need to check for these beforehand.

Installation

npm i --save-dev @mlturner/filter-map

yarn add @mlturner/filter-map

pnpm i @mlturner/filter-map

Usage

Array

Note: this also works with TypedArrays (e.g., Int32Array, etc.)

import { filterMap } from '@mlturner/filter-map';

const array = [2, 3, 7, 1, 9, 10, 0, -2];

const results = filterMap(
  array,
  n => n > 3,
  n => n * 2
);
// [14, 18, 20]

Map

import { filterMap } from '@mlturner/filter-map';

const map = new Map();
map.set(Symbol(1), 'kangaroo');
map.set(Symbol(2), 'cat');
map.set(Symbol(3), 'elephant');
map.set(Symbol(4), 'dog');
map.set(Symbol(5), 'parrot');

const results = filterMap(
  map,
  str => str.length > 2,
  str => str[0]
);
// ['k', 'e', 'p']

Plain Object

import { filterMap } from '@mlturner/filter-map';

const obj = {};
obj.a = { x: 9 };
obj.b = { x: 11 };
obj.c = { x: -100 };
obj.d = { y: 100 };

const results = filterMap(
  obj,
  o => o.x >= 9,
  o => o.x
);
// [9, 11]

Set

import { filterMap } from '@mlturner/filter-map';

const set = new Set();
set.add(4);
set.add('fruit');
set.add({ x: 3 });
set.add('fish');

const results = filterMap(
  set,
  v => typeof v === 'string',
  str => `[${str}]`
);
// ['[fruit]', '[fish]']

Generator Function

import { filterMap } from '@mlturner/filter-map';

function* generator(array) {
  for (const x of array) {
    yield x;
  }
}

const results = filterMap(
  generator([3, 0, -18, -100, 5]),
  n => n < 0,
  n => n / 2
);
// [-9, -50]

Type Declarations

export type FilterMapCollection<T> =
  | ArrayLike<T>
  | Map<string, T>
  | Set<T>
  | Record<string, T>
  | Generator<T>;

/**
 * Simultaneously filters and maps a collection
 * @param collection Collection to be filtered and mapped
 * @param predicateFn Predicate function to use for filtering
 * @param mapFn Mapping function used to transform input
 * @returns Filtered and transformed/mapped array
 */
export declare function filterMap<T, O>(
  collection: FilterMapCollection<T> | undefined | null,
  predicateFn: (current: T, keyIndex: number | string | T, collection: FilterMapCollection<T> ) => boolean,
  mapFn: (current: T, keyIndex: number | string | T, collection: FilterMapCollection<T>) => O
): O[];