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

async-array-methods

v2.1.0

Published

Async methods to operate on collections

Downloads

134

Readme

async-array-methods

version status coverage dependencies devDependencies

Async methods to manipulate arrays.

Usage

var methods = require('async-array-methods')
var AsyncArray = methods.Array
var filter = methods.filter
var map = methods.map
var forEach = methods.forEach
var reduce = methods.reduce

All methods return a promise.

Methods

Callbacks can be made asynchronous by returning a promise, or accepting a function argument, which will be called when the callback finishes.

Otherwise, the callback is treated as synchronous.

filter

Signature: filter(arr, fn, context)

filter(
  [1, 2, 3, 4],
  function (v, i, a, next) {
    process.nextTick(function () {
      next(null, v % 2)
    })
  }
)
.then(function (res) {
  // [1, 3]
  console.log(res)
})

map

Signature: map(arr, fn, context)

fn is called with each element in parallel.

map(
  [1, 2, 3, 4],
  function (v) {
    return new Promise(function (rs) {
      process.nextTick(function () {
        rs(v << 2)
      })
    })
  },
  { num: 2 }
)
// [4, 8, 12, 16]
.then(console.log.bind(console))

series

This method works like map, except that fn is called with each element in sequence rather than in parallel.

Signature: map(arr, fn, context)

var n = 1
series(
  [1, 2, 3, 4],
  function (v, i, arr, next) {
    var delay = rand()
    console.log('i:', i, 'delay:', delay)
    setTimeout(function() {
      next(null, v << n++)
    }, delay)
  }
)
// i: 0 delay: 10
// i: 1 delay: 50
// i: 2 delay: 0
// i: 3 delay: 80
// [2, 8, 24, 64]
.then(log)

function rand() {
  return Math.floor(Math.random() * 10) * 10
}

forEach

fn is called with elements in sequence.

Signature: forEach(arr, fn, context)

var count = 0

forEach(
  [1, 2, 3, 4],
  function (v, i, _, next) {
    process.nextTick(function () {
      console.log(count++, i)
      next(null, v << 2)
    })
  }
)
.then(function (arr) {
  // [1, 2, 3, 4]
  console.log(arr)
})

reduce

Signature: reduce(arr, fn, initial)

reduce(
  [1, 2, 3, 4],
  function (a, b, i, arr, next) {
    process.nextTick(function () {
      next(null, a + b)
    })
  }
)
// 10
.then(console.log.bind(console))

AsyncArray

Signature: AsyncArray(arr)

Methods:

  • map
  • series
  • filter
  • forEach
  • reduce
var origin = AsyncArray([1, 2, 3, 4, 5, 6])
var odd = origin.filter(isOdd)
var even = origin.filter(isEven)

return odd.then(function (res) {
  // [1, 3, 5]
  console.log(res)
  return even
})
.then(function (res) {
  // [2, 4, 6]
  console.log(res)
})
.then(function () {
  return even.reduce(flagMap, {})
})
.then(function (res) {
  // { 2: true, 4: true, 6: true }
  console.log(res)
})
.then(function () {
  // chain
  return origin.filter(isOdd).reduce(flagMap, {})
})
.then(function (res) {
  // { 1: true, 3: true, 5: true }
  console.log(res)
})

function isOdd(n, i, arr, next) {
  process.nextTick(function () {
    next(null, n % 2)
  })
}

function isEven(n, i, arr, next) {
  return new Promise(function (resolve) {
    process.nextTick(function () {
      resolve((n + 1) % 2)
    })
  })
}

function flagMap(o, v) {
  o[v] = true
  return o
}