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

fp-lite

v2.1.0

Published

Functional style utility functions

Downloads

87

Readme

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` 不是异步函数
})