2.1.0 • Published 5 months ago

fp-lite v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

English · 简体中文

一组函数式风格的工具函数

专注于简单易用, 只需要你了解以下概念:

  1. 一元函数: (input) => output
  2. 高阶函数: (a) => (b) => c

使用场景

//before
const handleSumbit = async formValues => {
  const values = await validate(formValues)
  const actualValues = await transform(values)
  const result = await post(acutalValues)
  return result
}

//after
const handleSumbitFp = asyncFlow(validate, transform, post)

组合函数

  1. flow 参数都是函数, 返回一个函数

    使用场景:\ 当下一个函数只需上一个函数的返回值时

const getLength = (x: string) => x.length
const increase = (x: number) => x + 1
const workFlow = flow(getLength, increase)
const result = workFlow('FP') // result = 3
  1. pipe 第一个参数是值, 后面的参数都是函数

    使用场景:\ 分发依赖

const getLength = (x: string) => x.length
const increase = (x: number) => x + 1
const result = (input: string, step: number) =>
  pipe('FP', getLength, increaseWith(step)) // result = 3
  1. asyncFlow 所有参数都是异步函数(第一个是,后面不是也行), 执行后返回一个异步函数
const workFlow = asyncFlow(fetch, r => r.json(), console.log)
workFlow('http://xx.json')
  1. asyncPipe 第一个参数是一个Promise值, 后面的参数都是函数
const result = await asyncPipe(fetch('http://xx.json'), r => r.json())

下面是跟组合函数一起使用的小函数

案列

const datas = [
  { kind: 'frontend', lib: 'React' },
  { kind: 'backend', lib: 'Express' },
]
const result = pipe(
  datas,
  groupBy(v => v.kind),
  g => g.values(),
  toList,
  flat,
  map(v => v.lib)
)
  1. 处理数组 (常用的) map | filter | flat | concat | unique | last | first

  2. 处理对象 pick | omit

  3. 条件判断 maybe | fallback | notNull | isEmpty | isZero

  4. 其他 peek|invoke

有些函数只是内建函数的别名

waitAll = Promise.all()\ toList = Array.from()\ unique = new Set()

普通函数

pickFn | omitFn | groupByFn | separeByFn

陷阱

const validate = () => {
  throw new Error('invalidate')
}

const post = async data => axios.post('/api', data)

const submit = flow(validate, post)

submit({}).catch(e => {
  //捕捉不到错误, 因为 `validate` 不是异步函数
})
2.1.0

5 months ago

2.0.0

7 months ago

1.2.0

10 months ago

1.0.2

11 months ago

1.1.0

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago

1.0.3

11 months ago

0.3.0

11 months ago

0.3.1

11 months ago

0.2.1

1 year ago

0.1.0

1 year ago

0.2.0

1 year ago

0.1.1

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago