Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1
A collection of array-related async utilities.
License
wojtekmaj/async-array-utils
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
A collection of array-related async utilities.
- Install by executing
npm install @wojtekmaj/async-array-utilsoryarn add @wojtekmaj/async-array-utils. - Import by adding
import * as asyncArrayUtils from '@wojtekmaj/async-array-utils'. - Do stuff with it!
constasyncMappedArr=awaitasyncMap([1,2,3],async(x)=>x*2);
asyncEvery()asyncEveryStrict()asyncFilter()asyncFilterStrict()asyncFind()asyncFindIndex()asyncForEach()asyncForEachStrict()asyncMap()asyncMapStrict()asyncReduce()asyncSome()asyncSomeStrict()
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, considerasyncEveryStrict() instead.
import{asyncEvery}from'@wojtekmaj/async-array-utils';constlargerThanZero=awaitasyncEvery([1,2,3],async(el)=>el>0);// true
LikeasyncEvery(), but runs iterations non-concurrently.
import{asyncEveryStrict}from'@wojtekmaj/async-array-utils';constindexes=[];constlargerThanZero=awaitasyncEveryStrict([1,2,3],async(el,index)=>{indexes.push(index);returnel>0;});// trueconsole.log(indexes);// [0, 1, 2]
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, considerasyncFilterStrict() instead.
import{asyncFilter}from'@wojtekmaj/async-array-utils';constasyncFilteredArr=awaitasyncFilter([1,2,3],async(el)=>el>1);// [2, 3]
LikeasyncFilter(), but runs iterations non-concurrently.
import{asyncFilterStrict}from'@wojtekmaj/async-array-utils';constindexes=[];constasyncFilteredArr=awaitasyncFilterStrict([1,2,3],async(el,index)=>{indexes.push(index);returnel>1;});// [2, 3]console.log(indexes);// [0, 1, 2]
Returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function,undefined is returned.
import{asyncFind}from'@wojtekmaj/async-array-utils';constasyncFoundEl=awaitasyncFind([1,2,3],async(el)=>el>1);// 2
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.
import{asyncFindIndex}from'@wojtekmaj/async-array-utils';constasyncFoundIndex=awaitasyncFindIndex([1,2,3],async(el)=>el>1);// 1
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, considerasyncForEachStrict() instead.
import{asyncForEach}from'@wojtekmaj/async-array-utils';awaitasyncForEach([1,2,3],async(el)=>{console.log(el*2);});// undefined; 3 console.logs
LikeasyncForEach(), but runs iterations non-concurrently.
import{asyncForEachStrict}from'@wojtekmaj/async-array-utils';constindexes=[];awaitasyncForEachStrict([1,2,3],async(el,index)=>{indexes.push(index);console.log(el*2);});// undefined; 3 console.logsconsole.log(indexes);// [0, 1, 2]
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, considerasyncMapStrict() instead.
import{asyncMap}from'@wojtekmaj/async-array-utils';constasyncMappedArr=awaitasyncMap([1,2,3],async(el)=>el*2);// [2, 4, 6]
LikeasyncMap(), but runs iterations non-concurrently.
import{asyncMapStrict}from'@wojtekmaj/async-array-utils';constindexes=[];constasyncMappedArr=awaitasyncMapStrict([1,2,3],async(el,index)=>{indexes.push(index);returnel*2;});// [2, 4, 6]console.log(indexes);// [0, 1, 2]
Executes a reducer asynchronous function (that you provide) on each element of the array, resulting in a single output value.
import{asyncReduce}from'@wojtekmaj/async-array-utils';constresult=awaitasyncReduce([1,2,3],async(tmp,cur,idx)=>{returntmp+cur;},0,);// 6
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, considerasyncMapStrict() instead.
import{asyncSome}from'@wojtekmaj/async-array-utils';constlargerThanZero=awaitasyncSome([1,2,3],async(el)=>el>0);// true
LikeasyncSome(), but runs iterations non-concurrently.
import{asyncSomeStrict}from'@wojtekmaj/async-array-utils';constindexes=[];constlargerThanZero=awaitasyncSomeStrict([1,2,3],async(el,index)=>{indexes.push(index);returnel>0;});// trueconsole.log(indexes);// [0]
The MIT License.
| Wojciech Maj |
About
A collection of array-related async utilities.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors3
Uh oh!
There was an error while loading.Please reload this page.