higherjs
TypeScript icon, indicating that this package has built-in type declarations

0.7.4 • Public • Published

higherjs

版本: 0.7.4

Build Status TypeScript License


简介:提供Javascript常见的高阶函数


安装

    直接引入dist目录下的higherjs.min.js
    或
    使用npm安装,运行

npm install higherjs

使用方法

    直接引入higherjs.min.js文件会将higherjs挂载至全局对象
    通过npm安装的可以使用按需加载

import {curry,Pubsub} from 'higherjs'
也可以直接引入higherjs   
import higherjs from 'higherjs'

API

1. higherjs.curry( fn : function, length? : number|curryOption, limited? : boolean, mode? : string)

    函数柯里化

柯里化(Currying)是把接受多个参数的函数变换成接受一个单一参数的函数,并且返回接受余下的参数且返回结果的新函数的技术

    第一个参数传入需要柯里化的函数

import {curry} from 'higherjs'
function logThreeNums(num1,num2,num3){
  console.log(num1,num2,num3)
}
let curryLog = curry(logThreeNums)
curryLog(1)(2)(3) // 1,2,3

    第二个可选参数,可以显式设置参数的长度限制,默认为函数的参数长度。

let curryLogTwoNums = curry(logThreeNums,2)
curryLogTwoNums(1)(2) // 1,2,undefined

    第三个可选参数,是否限制参数长度,如果为假值则无视第二个参数,并提供finish方法。使用无参调用或finish方法结束调用。

function logNumbers(){
  console.log(...arguments)
}
let curryUnlimitedLog = curry(logNumbers,0,false)
curryUnlimitedLog(1)(2)(3)(4)() // 1,2,3,4
curryUnlimitedLog(4)(3)(2)(1).finish() // 4,3,2,1

    第四个可选参数,可以设置'parallel'来使用并行模式的柯里化,默认使用'pipe'管道流模式

//注意 我们将 curryFunc(1)(2)(3)(4) 这样的调用称为pipe模式,也就是默认的模式
let parallelLog = curry(logNumbers,0,false,'parallel')
parallelLog(1)
parallelLog(2)
parallelLog(3)
parallelLog.finish() // 1,2,3
//注意 在使用无限制'parallel'模式时调用finish后会清除缓存,但是无参调用不会触发这种行为。
parallelLog(1)
parallelLog(2)
parallelLog(3)
parallelLog() // 1,2,3
parallelLog(3) 
parallelLog() // 1,2,3,3

    现在也可以提供一个option对象,传入对象的行为可读性更高 推荐

import {curry} from "higherjs"
 
interface curryOption{
  length ?: number;
  limit ?: boolean; 
  parallel ?: boolean;
}
let func = function(){
  console.log(...arguments)
}
//为了使你的柯里化函数更加可控,当你设置limit:true时你始终应该提供length属性
let option:curryOption = {
  limit : true
  length : 4
  parallel : true
}
let curryFunc = curry(func,option)
//相当于 curry(func,4,true,'parallel')
curryFunc(2)
curryFunc(4)
curryFunc(2)
curryFunc(4) //[2,4,2,4]
 
let curryFunc2 = curry(func,{})
//相当于 curry(func) 或者 curry(func,0,false)
curryFunc2(2)(3)(5)() //[2,3,5]

    pipe模式允许出现在parallel模式

import {curry} from "higherjs"
 
let func = function(){
  console.log(...arguments)
}
//省略limit参数即为不限制参数长度,所以没必要传入length属性
let curryFunc = curry(func,{
  parallel : true
})
curryFunc(2)
curryFunc(4)
curryFunc(6)(8) //pipe模式允许出现在parallel模式
curryFunc()  //[2,4,6,8]

2. higherjs.Pubsub(类)

    发布订阅类
    支持订阅前发布自动缓存
    支持异步订阅

import {Pubsub} from "higherjs"
 
interface PubsubClass{
  subscribe : (key: string, fn: Function, async?:boolean) => boolean|any[];
  publish : (key: string, ...params: any[]) => undefined|any[];
  clear : (key: string) => boolean;
  //现在也可以用on方法替代subscribe,emit方法替代publish。只是alias(别名)
  on : (key: string, fn: Function, async?:boolean) => boolean|any[];
  emit : (key: string, ...params: any[]) => undefined|any[];
}
 
//实例化一个发布订阅对象
let pubsub:PubsubClass = new Pubsub()
//提供一个key并订阅一个函数
pubsub.subscribe('getSum',function(num1,num2){
  return num1 + num2
})
 
//发布key对应的函数队列
pubsub.publish('getSum', 2, 4)  // [6]
 
//清除key对应的函数队列
pubsub.clear('getSum')
 
//同一个key订阅两个函数
function returnOriginalArr(param1, param2) {
  return [param1, param2]
}
function returnStringArr(param1, param2) {
  return [param1.toString(), param2.toString()]
}
pubsub.subscribe('getArr', returnOriginalArr)
pubsub.subscribe('getArr', returnStringArr)
 
let result = pubsub.publish('getArr', 2, 4)
console.log(result) //[ [2,4] , ['2','4'] ]
 
//支持订阅前发布自动缓存 
pubsub.publish('pubBeforeSub', 'this is the first message')
pubsub.publish('pubBeforeSub', 'this is the second message')
let result = pubsub.subscribe('pubBeforeSub', function (msg) {
  return msg
})
//注意订阅完成后,缓存会清零
console.log(result) // ['this is the first message','this is the second message']
 
//支持异步订阅
function addNum(num) {
  num ++
  console.log(num)
  return num
}
//异步订阅函数
pubsub.subsribe( 'addNumAsync', addNum, true)
pubsub.publish( 'addNumAsync', 10)
console.log('before addNum execute')
 
//'before addNum execute'
// 11
 
//注意pubsub.publish( 'addNumAsync', 10)会返回一个Promise(resolved)对象,你可以继续调用then处理你订阅函数返回的数据
let promise = pubsub.publish( 'addNumAsync', 10)[0]
promise.then(res => console.log(res + 1)) // 12
 

3. higherjs.debounce( fn : function, wait? : number, immediate? : boolean)

    函数防抖
    第一个参数是需要节流的函数
    第二个可选参数,设置防抖的缓冲时间,默认1000ms
    第三个可选参数,设置是否立即调用,默认true
    返回防抖后的函数

4. higherjs.throttle( fn : function, wait? : number, immediate? : boolean)

    函数节流
    第一个参数是需要节流的函数
    第二个可选参数,设置节流的缓冲时间,默认1000ms
    第三个可选参数,设置是否立即调用,默认true
    返回节流后的函数

5. higherjs.getSingle(fn : function)

    函数单例(惰性)
    唯一参数,传入需要成为单例的函数
    返回一个单例模式的函数

更新说明

V0.7.0更新
    新的API,可异步执行和订阅前缓存的发布订阅类Pubsub
V0.6.0更新
    curry函数的第二个参数现在可以传入一个option参数
V0.5.0更新
    用TypeScript重写项目,调整了目录结构

output //tsc编译后的文件   
config //存放配置文件   
src //源码   
dist //webpack打包后的js文件  
...

V0.4.0更新

    新增getSingle函数(见API介绍)

    提供了curry函数的第四个参数,可以传入"parallel"使用并行调用

Package Sidebar

Install

npm i higherjs

Weekly Downloads

2

Version

0.7.4

License

ISC

Unpacked Size

47.5 kB

Total Files

28

Last publish

Collaborators

  • attackonryan