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

@mattmorgis/arrasync

v0.1.2

Published

Async array utilities for ES6

Downloads

2

Readme

arrasync

Async array utilities for ES6

Six functions are included in arrasync, all of which are async:

Installation

npm install --save arrasync

Import and usage:

import {map} from 'arrasync';

// returns the squares of the whole numbers from 1 to 5
const getSquares = async () => {
  const array = [1, 2, 3, 4, 5];
  const squares = await map(array, async value => value ** 2);
  return squares;
};

console.log(getSquares());  // [1, 4, 9, 16, 25]

Usage

map(array, func)

Asynchronously apply a function to every element of an array, returning an array of the results.

Arguments:

  • array - the array to be mapped.
  • func(value, index, array) - the function to be called for each value of array. Can be sync or async, and can optionally take the index of each value and the array being mapped as arguments.

Example:

// Map values to their squares

const array = [1, 2, 3, 4, 5];
const squares = await map(array, async value => value ** 2);
console.log(squares); // [1, 4, 9, 16, 25]

filter(array, func)

Asynchronously filter an array based on a true/false test.

Arguments:

  • array - the array to be filtered
  • func(value, index, array) - a function that returns true on a value to be included, and returns false on a value that shouldn't be included. Can be sync or async, and can optionally take the index of each value and the array being mapped as arguments.

Example:

// Filter numbers out of an array

const array = [1, 'a', 'b', 2, 3, 'c', 4, 'd'];
const strings = await filter(array, async value => typeof value !== 'number');
console.log(strings); // ['a', 'b', 'c', 'd']

reduce(array, func, accumulator)

Apply a function on each value in an array (from left to right) using an accumulator to reduce it to a single value.

Arguments:

  • array - the array to be reduced
  • func(accumulator, value, index, array) - The function to execute on each value of the array, which returns the new value of the accumulator at each step. Can be sync or async, but executes in series either way. Optionally can take the index of each value and the array being reduced as arguments.
  • accumulator - the initial value of the accumulator

Example:

// Sum values of an array

const array = [1, 2, 3, 4, 5];
const sum = await reduce(array, async (accumulator, value) => accumulator + value, 0);
console.log(sum); // 15

flatten(array)

Given an arbirarily nested array, returns a flat array with order preserved.

Arguments:

  • array - the nested array to be flattened

Example:

// Flatten a nested array

const array = ['a', [['b', 'c'], 'd', 'e'], 'f', 'g', ['h'], 'i'];
const flattened = await flatten(array);
console.log(flattened) // ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'];

zip(...arrays)

Takes multiple arrays and returns an array of sub-arrays, where the first sub-array holds the first element from each of the input arrays, the second sub-array holds the second element from each of the input arrays, and so on.

Arguments:

  • ...arrays - any number of equal-length arrays

Example:

// Zip together two arrays

const numbers  = [1, 2, 3, 4, 5];
const letters  = ['a', 'b', 'c', 'd', 'e'];
const zipped = await zip(numbers, letters);
console.log(zipped); // [[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd'], [5, 'e']]

unzip(array)

Does the reverse of zip (equivalent to zip(...array)).

Arguments:

  • array - array to be unzipped

Example:

// Unzip an array

const array = [[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd'], [5, 'e']]
const unzipped = await unzip(array);
console.log(unzipped); // [[1, 2, 3, 4, 5], ['a', 'b', 'c', 'd', 'e']]