pareto.js
An extremely small, intuitive and fast functional utility library for JavaScript
- Only 14 core functions
- Written in TypeScript
- Encourages immutability
- Only pure functions (no side-effects)
- Smaller than lodash
Example
// [1, 2, 3, 4, 5] // [2, 3]
Installation
To install the stable version:
npm install --save paretojs
This assumes that you’re using npm with a module bundler like Webpack
How
ES2015 or TypeScript:
or
CommonJS:
var _ = ;
or
var chunk = chunk;var debounce = debounce;
UMD:
API
chunk
Returns the chunk of an array based on an integer n
; ; // [ [1,2,3], [4,5,6], [7] ]
compose
Gets a composed function
; const toUpperCase = x;const exclaim = x + '!!!'; const angry = ; ; // 'STOP!!!
curry
Gets a curried function
; const add = x + y; ; // 312; // 31 2; // 32; // 3
debounce
Creates and returns a new debounced version of the passed function which will postpone its execution until after wait milliseconds have elapsed since the last time it was invoked.
; let a = 1;const fn = a = 42; const debounce = ;; console; // 1 before 500ms console; // 42 after 500ms
deepCopy
Creates a deep copy of an object
; const object = a: 1 b: 2 c: d: 3 ; ; // { a: 1, b: 2, c: { d: 3} }
flatMap
Generates a flattened array by iterating through a collection and applying a function to each element
; const inc = n + 1;); // [2, 3, 4] const dup = n n;); // [1, 1, 2, 2, 3, 3] const sq = n ** 2;) // [1, 4, 9]
flatten
Flattens (recursively) an array
; ; // [1, 2, 3, 4]
get
Gets the value of an object property based on a string path provided. If the property is not found, the defaultValues is returned
; ); // 1); // "default"); // { b: 2 }); // 2); // undefined
matches
Checks if an objects matches with some properties
; const object1 = a: 1 b: 2 ; ; // true; // true; // false
memoize
Creates a function that memoizes (caches) the result
; let count = 0; const square = { count = count + 1; return x * x;}; const memoSquare = ; count; // 0; // 100; // 100; // 100count; // 1
pipe
Creates and returns a new function that performs a left-to-right function composition.
; const increment = x + 1;const decrement = x - 1; const piped = ;; // 1
prop
Gets the property of an object
; const object = label: 'custom label'; ; // custom label
sort
Sorts a collection based on a property
; const collection = id: 2 id: 1 ; ; // [{ id: 1 }, { id: 2 }]
tail
Gets all, except the first element of an array.
; ; // [2, 3]
Misc
If you want to add a new function, please open an issue and explain why.