Skip to content

tealina/fp-lite

Repository files navigation

This libary is focus on easy to use, only requierd you know a few concect below:

  1. Unary function: (input) => output
  2. Hight order function: (a) => (b) => c

Use Case

//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)

Begin composition

  1. flow accept functions, return a function

    Usage scenario:
    when you have a series of functions only require the return value of the previous function

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 fisrt param is a value, rest params are functions

    Usage scenario:
    Distribution dependencies

const getLength = (x: string) => x.length
const increaseWith = (y: number) => (x: number) => x + y
const result = (input: string, step: number) =>
  pipe('FP', getLength, increaseWith(step)) // result = 3
  1. compose The reverse version of flow, some times it made more readable
import { compose, filter, isEmpty } from 'fp-lite'
const not =
  <T>(x: T) =>
  (fn: (x: T) => boolean) =>
    !fn(x)
// const byFlow = flow(isEmpty, not, filter)
// const normal = (x)=>filter(not(isEmpty(x)))
const fp = compose(filter, not, isEmpty)
const getLength = (x: string) => x.length
const increase = (x: number) => x + 1
const fn = compose(increase, getLength) // fn = (x:string)=>number
  1. asyncFlow accept async functions, return an async funtion
const workFlow = asyncFlow(fetch, r => r.json(), console.log)
workFlow('http://xx.json')
  1. asyncPipe The first param is a Promise, rest params are async functions
const result = await asyncPipe(fetch('http://xx.json'), r => r.json())

Functions work with composition function

Example

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. Array functions (frequently used)
    map | filter | flat | concat | unique | last | first

  2. Functions for Object
    pick | omit

  3. Function for grouping groupBy | separeBy

  4. Functions for condition
    maybe | fallback | notNull | isEmpty | isZero

  5. Other functions
    peek|invoke

Some Functions are just alias

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

Normal Functions

pickFn | omitFn | groupByFn | separeByFn

Not recommended for use in non typescript projects

Pitfall

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

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

const submit = flow(validate, post)

submit({}).catch(e => {
  //never reach, because `validate` is not async function
})