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

@wojtekmaj/async-array-utils

v1.8.1

Published

A collection of array-related async utilities.

Downloads

1,930

Readme

npm downloads CI

Async-Array-Utils

A collection of array-related async utilities.

tl;dr

  • Install by executing npm install @wojtekmaj/async-array-utils or yarn add @wojtekmaj/async-array-utils.
  • Import by adding import * as asyncArrayUtils from '@wojtekmaj/async-array-utils'.
  • Do stuff with it!
    const asyncMappedArr = await asyncMap([1, 2, 3], async (x) => x * 2);

User guide

Table of contents

asyncEvery()

Tests whether all elements in the array pass the test implemented by the provided asynchronous function. It returns a Boolean value.

Note: For optimization purposes, all iterations are ran concurrently. If you rely on any side effects, consider asyncEveryStrict() instead.

Sample usage

import { asyncEvery } from '@wojtekmaj/async-array-utils';

const largerThanZero = await asyncEvery([1, 2, 3], async (el) => el > 0); // true

asyncEveryStrict()

Like asyncEvery(), but runs iterations non-concurrently.

Sample usage

import { asyncEveryStrict } from '@wojtekmaj/async-array-utils';

const indexes = [];
const largerThanZero = await asyncEveryStrict([1, 2, 3], async (el, index) => {
  indexes.push(index);
  return el > 0;
}); // true
console.log(indexes); // [0, 1, 2]

asyncFilter()

Creates a new array with all elements that pass the test implemented by the provided asynchronous function.

Note: For optimization purposes, all iterations are ran concurrently. If you rely on any side effects, consider asyncFilterStrict() instead.

Sample usage

import { asyncFilter } from '@wojtekmaj/async-array-utils';

const asyncFilteredArr = await asyncFilter([1, 2, 3], async (el) => el > 1); // [2, 3]

asyncFilterStrict()

Like asyncFilter(), but runs iterations non-concurrently.

Sample usage

import { asyncFilterStrict } from '@wojtekmaj/async-array-utils';

const indexes = [];
const asyncFilteredArr = await asyncFilterStrict([1, 2, 3], async (el, index) => {
  indexes.push(index);
  return el > 1;
}); // [2, 3]
console.log(indexes); // [0, 1, 2]

asyncFind()

Returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

Sample usage

import { asyncFind } from '@wojtekmaj/async-array-utils';

const asyncFoundEl = await asyncFind([1, 2, 3], async (el) => el > 1); // 2

asyncFindIndex()

Returns the index of the first element in an array that satisfies the provided testing function. If no elements satisfy the testing function, -1 is returned.

Sample usage

import { asyncFindIndex } from '@wojtekmaj/async-array-utils';

const asyncFoundIndex = await asyncFindIndex([1, 2, 3], async (el) => el > 1); // 1

asyncForEach()

Executes a provided asynchronous function once for each array element.

Note: For optimization purposes, all iterations are ran concurrently. If you rely on any side effects, consider asyncForEachStrict() instead.

Sample usage

import { asyncForEach } from '@wojtekmaj/async-array-utils';

await asyncForEach([1, 2, 3], async (el) => {
  console.log(el * 2);
}); // undefined; 3 console.logs

asyncForEachStrict()

Like asyncForEach(), but runs iterations non-concurrently.

Sample usage

import { asyncForEachStrict } from '@wojtekmaj/async-array-utils';

const indexes = [];
await asyncForEachStrict([1, 2, 3], async (el, index) => {
  indexes.push(index);
  console.log(el * 2);
}); // undefined; 3 console.logs
console.log(indexes); // [0, 1, 2]

asyncMap()

Creates a new array populated with the results of calling a provided asynchronous function on every element in the calling array.

Note: For optimization purposes, all iterations are ran concurrently. If you rely on any side effects, consider asyncMapStrict() instead.

Sample usage

import { asyncMap } from '@wojtekmaj/async-array-utils';

const asyncMappedArr = await asyncMap([1, 2, 3], async (el) => el * 2); // [2, 4, 6]

asyncMapStrict()

Like asyncMap(), but runs iterations non-concurrently.

Sample usage

import { asyncMapStrict } from '@wojtekmaj/async-array-utils';

const indexes = [];
const asyncMappedArr = await asyncMapStrict([1, 2, 3], async (el, index) => {
  indexes.push(index);
  return el * 2;
}); // [2, 4, 6]
console.log(indexes); // [0, 1, 2]

asyncReduce()

Executes a reducer asynchronous function (that you provide) on each element of the array, resulting in a single output value.

Sample usage

import { asyncReduce } from '@wojtekmaj/async-array-utils';

const result = await asyncReduce(
  [1, 2, 3],
  async (tmp, cur, idx) => {
    return tmp + cur;
  },
  0,
); // 6

asyncSome()

Tests whether at least one element in the array pass the test implemented by the provided asynchronous function. It returns a Boolean value.

Note: For optimization purposes, all iterations are ran concurrently. If you rely on any side effects or execution order, consider asyncMapStrict() instead.

Sample usage

import { asyncSome } from '@wojtekmaj/async-array-utils';

const largerThanZero = await asyncSome([1, 2, 3], async (el) => el > 0); // true

asyncSomeStrict()

Like asyncSome(), but runs iterations non-concurrently.

Sample usage

import { asyncSomeStrict } from '@wojtekmaj/async-array-utils';

const indexes = [];
const largerThanZero = await asyncSomeStrict([1, 2, 3], async (el, index) => {
  indexes.push(index);
  return el > 0;
}); // true
console.log(indexes); // [0]

License

The MIT License.

Author