Skip to content

Instantly share code, notes, and snippets.

@Darkle
Last active November 24, 2019 23:34
Show Gist options
  • Select an option

  • Save Darkle/ff53d856587f448fbb0c2bb285b7fac8 to your computer and use it in GitHub Desktop.

Select an option

Save Darkle/ff53d856587f448fbb0c2bb285b7fac8 to your computer and use it in GitHub Desktop.
My TypeScript FP Utility Functions
const noop = (): void => {}
const identity = <T>(param: T): T => param
const pipe = (...fns: Function[]): Function => (param: any) => fns.reduce((result, fn) => fn(result), param)
const compose = (...fns: Function[]): Function => (param: any) => fns.reduceRight((acc, current) => current(acc), param)
const curry = (f: Function): Function => (...a: any[]) => (...b: any[]) => f(...a, ...b)
const curryRight = (f: Function): Function => (...a: any[]) => (...b: any[]) => f(...b, ...a)
const range = (start: number, end: number): number[] => Array.from({ length: (end - start) }, (v, k) => k + start)
//includes end number - useful if you actually want the numbers in the range instead of just the index range
const rangeIncEnd = (start: number, end: number): number[] => Array.from({ length: ((end - start) + 1) }, (v, k) => k + start)
export{
noop,
identity,
pipe,
compose,
curry,
curryRight,
range,
rangeIncEnd,
}
// Also check out https://github.com/ritesh404/kudojs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment