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-series-each

v1.0.2

Published

一个提供了异步遍历功能的工具,可以通过插件拓展工具的遍历器。已经提供的方法包括forEach,every,some,filter,map,sort,reduce。

Downloads

3

Readme

async-each

这是一个提供了异步遍历功能的工具,可以通过插件拓展工具的遍历器。已经提供的方法包括forEach,every,some,filter,map,sort,reduce。

  安装: npm install async-series-each --save-dev

提供的遍历器:

  • forEach(callback[, thisArg])
  调用方法及参数和同步版本forEach(Array.prototype.forEach)一致,功能也一致。
  • every(callback[, thisArg])
  调用方法及参数和同步版本every(Array.prototype.every)一致,功能也一致。
  • some(callback[, thisArg])
  调用方法及参数和同步版本some(Array.prototype.some)一致,功能也一致。
  • filter(callback[, thisArg])
  调用方法及参数和同步版本filter(Array.prototype.filter)一致,功能也一致。
  • map(callback[, thisArg])
  调用方法及参数和同步版本map(Array.prototype.map)一致,功能也一致。
  • reduce(callback[, fir])
  调用方法及参数和同步版本reduce(Array.prototype.reduce)一致,功能也一致。
  • sort(callback)
  调用方法及参数和同步版本sort(Array.prototype.sort)一致,功能也一致。内部采用的是插入排序,海量数据慎用。
  • 你可以通过插件编写需要的遍历器

用法:

  建议安装promise化工具: npm install new-promiseify --save-dev

调用环境应该是在async函数内部或者Promise实例的回调函数内部

   //导入
   const each = require('async-series-each');
   const promiseify = require('new-promiseify');
   const fs = require('fs');

   //forEach
   await each([0, 0, 0, 0]).forEach(async(cur, i, ar) => {
     await promiseify([setTimeout, 0, 0])(5000);
     console.log(`Current index is ${i}`);
   });

   //every
   const vaild = await each([1, 2, 3, 0, 4]).every(async(cur, i, ar) => {
       await promiseify([setTimeout, 0, 0])(5000);
       return console.log(cur) || cur;
   });

   //some
   const vaild = await each([0, 0, 0, false, true, 1]).some(async(cur, i, ar) => {
       await promiseify([setTimeout, 0, 0])(5000);
       return console.log(cur) || cur;
   });

   //filter
   const ar = await each([1, 2, 0, false, true, [], {}, null]).filter(async(cur, i, ar) => {
       await promiseify([setTimeout, 0, 0])(5000);
       return console.log(cur) || !cur;
   });

   //map
   const ctns = await each(['./1.md', './2.md', './3.md']).map(async(cur, i, ar) => {
       const ctn = await promiseify(fs.readFile)(cur, 'utf8');
       return console.log(cur) || ctn;
   });

   //reduce
   const res = await each(['./1.md', './2.md', './3.md']).reduce(async(res, cur, i, ar) => {
     const ctn = await promiseify(fs.readFile)(cur, 'utf8');
     return console.log(cur) || `${res + (i + 1)}. ${ctn}\n`;
   }, 'Read file content:\n');

   //sort
   const ar = await each(['./1.md', './2.md', './3.md']).sort(async(prev, cur) => {
       const rdFile = promiseify(fs.readFile),
       firCtn = await rdFile(prev, 'utf8'),
       secCtn = await rdFile(cur, 'utf8');
       return firCtn > secCtn ? 1 : -1;
   });

一点说明

  为什么不直接在Array.prototype上拓展?

  因为我希望这个拓展是温和的,而不是具有侵入性的。

插件的写法

  以some方法为例
   module.exports = function SomeAsyncPlugin(Series) {
      //Regist some command to prototype of Series
      Series.prototype.some = async function(callback, cur = Series.root) {
          callback = callback.bind(cur);
          const src = this.source,
              len = src.length;
          for (let i = 0; i < len; i++) {
              const cont = await callback(src[i], i, src);
              if (cont) return true;
          }
          return false;
      };
   };