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 🙏

© 2026 – Pkg Stats / Ryan Hefner

js-function-chain

v1.1.1

Published

链式执行JavaScript函数

Readme

概述:

在接触函数式编程时,我们会遇到怎么更好的链式调用函数;比如:Array.map(...).sort(...).reverse();就是一个很好的函数链式调用的例子。怎么样将这种链式调用推广到一般的函数呢?我参考Rxjs的pipe写了这个函数。全部源码就是下面这个函数:

/**
 * 
 * 该函数将从左到右依次执行给定的函数,第一个参数为原始参数数组。chainFunctions([1,2,3],f,g,h) <=> h(g(f(1,2,3)))。
 * 建议传入函数均为“纯函数”,无副作用,总是有返回值,给定相同参数每次的返回值均相同;当然非“纯函数”也能使用。
 * @param {Array} input 原始参数数组
 * @param  {...any} fns 需要依次执行的函数
 */
function chainFunctions(input: Array<any>, ...fns: Array<Function>){
    return fns.reduce((preResult: any, currentFn: Function, index: number) => {
        return index === 0 ? currentFn(...preResult) : currentFn(preResult);
    }, input);
}

npm 安装:

npm i js-function-chain

import方式:

  • import { chainFunctions } from 'js-function-chain/functionChain.js'; // es6 或者 ts
  • let chainFunctions = require('js-function-chain/functionChainNodejs.js').chainFunctions; // nodejs导入
  • window.chainFunctions; // 传统方式直接使用

Demos

// demo1,普通使用
console.log(chainFunctions([1,2], (x,y) => x+y, (x) => x*2, (x) => x*x));

// demo2,使用柯里化(curry)函数
function createMessage(subject, msg){
    return `${subject} create message '${msg}'`;
}
function sendMessage(msg, object){
    return `${msg} send to ${object}`;
}
function concatMessage(msg1, msg2){
    return `${msg1} and ${msg2}`;
}
console.log(chainFunctions(['alice', 'hello'], createMessage, (msg) => concatMessage(msg,''), (msg) => sendMessage(msg, 'bob')));

// demo3,注意chainFunctions函数第一个参数是参数的数组,所以当参数是数组时,要在外面嵌套一层数组
function findMin3(numberArr){
    return chainFunctions([numberArr],   // 注意这里numberArr外嵌套了一层数组
                (arr) => arr.sort((a,b) => a-b),
                (arr) => arr.slice(0,3));
}
console.log(findMin3([24,42,12,23,52,2,7,31,3,234]));