Socket
Socket
Sign inDemoInstall

fp-lite

Package Overview
Dependencies
0
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    fp-lite

Functional style utility functions


Version published
Maintainers
1
Install size
45.1 kB
Created

Changelog

Source

2.1.0

Minor Changes

  • 60430ef: feat: new fallback function

Readme

Source

English · 简体中文

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

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

Keywords

FAQs

Last updated on 06 Jan 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc