A small foot print collection of javascript utility functions written in a functional style without any external dependancies.
FJS-Utils
Table of contents
Installation
npm i fjs-utils
or
yarn add fjs-utils
Usage
installation
To use the library just import it
;
You can import just the pieces you want
;
or
;
Utils
arrays
more
- all - Tests each value with provided functions and returns true if all results are truthy. It's Array.prototype.every renamed
; const array = true true true; console; // => true
- any - Tests each value with provided functions and returns true if any results are truthy. It's Array.prototype.some renamed
; const array = false false true; console; // => true
- bifurcate - Seperates an array by supplied function.
; const array = true 0 'str' false {} ''; console // => [[true, 'str', {}], [0, false, '']]
- chunk - Splits an array into chunks
; const array = 1 2 3 4 5 6 7 8 9 0; console; // => [[1, 2, 3], [4, 5, 6], [7, 8, 9], [0]]
- countOccurences - counts the occurences of a value in an array
; console; //=> 4
- flatten - Flattens an array 1 level
; const array = 1 2 3 4 5 6 7 8 9 0; console; // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
- getProp - Returns an array of values from a specified property in an array of objects
; const array = foo: 'foo' bar: 'bar' foo: 'foo2' bar: 'bar2' ; console; // => ['foo', 'foo2']
- isArray - Returns boolean, true if input is an array
; console; // => true console; // => false
- mean - Calculates the mean of an array of numbers
; console; // => 2
- median - Calculates the median of an array of numbers
; console; // => 2
- sample - Returns random values from an array
; const array = 1 2 3 4; console; // => [2] console; // => [1, 3]
- shuffle - Returns a new array with the order randonmized
; const array = 1 2 3 4 5; console; // => [3, 1, 2, 5, 4];
- sum - Sums an array of numbers
; console // => 6
functions
more
- curry - Accepts a function and returns a curried function
; const add = a + b; const add10 = 10; console; // => 15
- curryRight - Accepts a function and returns a curried function that fills params in from right to left
; const concat = ``; const appendBar = 'bar'; console; // => 'foobar'
- identity - returns the input unchanged
; console; // => 'foo'
- memoize - returns a function that will memoize the arguments
; const fn = ``; const memoizedFn = ; console; // => 'foobar'
- multi - returns a function that will return an array of results from multiple functions
; const maxMin = ; console; // => [500, 0]
numbers
more
- random - Returns a random number between min, max
; console; // => 2 console; // => 2.2398217
objects
more
- convertSnakeKeysToCamel - Converts an object's snake cased keys to camel cased
; const obj = snake_case: 'snek' foo: bar_baz: 'something' lorem_ipsum: 'latin' obj: snake_key_again: 'right_here' ; console; /* { snakeCase: 'snek', foo: [{ barBaz: 'something' }, { loremIpsum: 'latin' }], obj: { snakeKeyAgain: 'right_here' }, } */
- isObject - Returns true if input is an object
; const obj = {}; const array = ; const fn = {}; const str = ''; console; // => true false false false
- omit - Omits keys from an object
; const obj = one: 1 two: 2 three: 3 four: 4 five: 5 ; console; // => { five: 5 }
- removeEmptyStrings - Removes keys with empty strings from an object
; const obj = one: 'one' two: '' three: 'three' four: '' five: 'five' ; console; /* { one: 'one', three: 'three', five: 'five', } */
- removeFromObject - Removes properties from an object based on a comparator
; const obj = one: 1 two: 2 three: 3 four: 4 five: 5 ; const removeGreaterThan3 = val > 3; const removeTwo = key === 'two'; const remove = || ; console; // => { one: 1, three: 3 }
- removeValueFromObject - Removes keys with specified value from an object
; const obj = one: 'foo' two: 'bar' three: 'foo' four: 'bar' five: 'foo' ; console; /* { one: 'foo', three: 'foo', five: 'foo', } */
- transformObjectKeysAndValues - transforms an object's keys and values
; const obj = left1: 'left1' left2: 'left2' ; const keyTrans = `right`; const valTrans = `right`; console; /* { left1right: 'left1right', left2right: 'left2right', } */
strings
more
- append - Appends strings with specified value
; console; // => 'foobar' const appendBar = ; console; // => 'foobar'
- concat - Concatenates two strings
; console; // => 'foobar'
- prepend - Prepends strings with specified value
; console; // => 'foobar' const prependFoo = ; console; // => 'foobar'
- snakeToCamel - Converts a snake cased string to camel cased
; console; // => 'snakeCasedString'
Utils
more
- debounce - debounces a function. Third paramater can be used to allow the function to be invoked immediately
; const fn = console; const debouncedFn = ; ; ; await ; /* 'foo' */
- wait - returns a promise that resolves after n milliseconds
; const fn = async { console; // => 0 await ; console; // => 100 }; ;
FAQ
expand
- How is this different than lodash or ramda?
- For one it's not the same set of functions
- Lodash specifically puts the array or object first which limits the composability
- This libraray has a different goal than ramda. Ramda's goal is to make functional programming easier in JS. While there are functional concepts here, that's only so it can remain external dependency free.