Docs are a work in progress.
npm install almost-functional
Composes a function that will return the result of invoking the functions in succession where the return value of the previous function is supplied to the next
...fns: Array
Function;
const square = (n) => n * n;
const mult = (a, b) => a * b;
const multSquare = compose(
square,
mult,
);
multSquare(10, 10);
// => 10000
Returns a curried function that is equal to the provided function. Arguments of the curried function do not need to be provided at the same time.
fn: Function;
Function || any;
const sumFive = curry(
(a: number, b: number, c: number, d: number, e: number) => a + b + c + d + e,
);
sumFive(1)(1)(1)(1)(1);
// => 5
sumFive(1, 1, 1, 1, 1);
// => 5
sumFive(1, 1)(1, 1, 1, 1);
// => 5
Returns the first element in an array or undefined
arr: Array;
any;
first([1, 2, 3]);
// => 1
first([]);
// => undefined
Flattens an array one level deep
arr: Array;
Array;
flatten([1, 2, 3, [4, 5, 6]]);
// => [1, 2, 3, 4, 5, 6]
Recursively flattens an array
arr: Array;
Array;
flattenDeep([1, 2, 3, [4, 5, [6, 7, [8, 9]]]]);
// => [1, 2, 3, 4, 5, 6, 7, 8, 9]
Iterates over the provided array and invokes the iteratee on each element. The iteratee is provided the value, index and array.
arr: Array;
iteratee: Function;
void
forEach([1, 2, 3], (val) => {
console.log(val);
});
// => 1
// => 2
// => 3
forEach([1], (val, idx, arr) => {
console.log(`val: ${val} - idx: ${idx} - arr: ${arr}`);
});
// => val: 1 - idx: 0 - arr: 1
Returns an object created from the key:value pairs provided.
pairsArr: Array;
Object;
fromPairs(['a', 1], ['b', 2]);
// => {a: 1, b: 2}
Returns the first element in an array or undefined
arr: Array;
any;
head([1, 2, 3]);
// => 1
head([]);
// => undefined
Checks if the value is a function
val: any;
boolean;
isFunction(() => {});
// => true
isFunction({});
// => false
Checks if the value is a object
val: any;
boolean;
isObject({});
// => true
isObject(null);
// => false
isObject(() => {});
// => false
Checks if the value is object like, which is not null and an object.
val: any;
boolean;
isObjectLike(null);
// => false
isObjectLike([]);
// => true
isObjectLike({});
// => true
Checks if the value has been created by the Object constructor.
val: any;
boolean;
class A {}
isPlainObject(new A());
// => false
isPlainObject({});
// => true
Returns an array from the key values from the provided object.
obj: Object;
Array;
keys('almost-functional');
// => []
keys({a: 1, b: 2});
// ['a', 'b']
Returns the last element of an array or undefined
...args: Array
any;
last([]);
// => undefined
last([1, 2, 3]);
// => 3
Merges the arguments provided.
...args: Array
Object;
merge({a: 1, b: 2}, {c: 3, d: 4}, {e: 5, f: 6});
// => {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6}
Merges the arguments provided, remove all none objects, null, and arrays.
...args: Array
Object;
merge({a: 1, b: 2}, {c: 3, d: 4}, {e: 5, f: 6}, null, []);
// => {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6}
Composes a function that will pipe the result of the invoking function to the next function.
...fns: Array
Function;
const square = (n) => n * n;
const mult = (a, b) => a * b;
const multSquare = pipe(
mult,
square,
);
multSquare(10, 10);
// => 10000
Recursively searches an object for a specified keys and returns it or null.
obj: Object;
any;
const pluckObj = pluckDeep({a: {b: {c: 1, d: {e: 'found'}}}});
pluckObj('f');
// => null
pluckObj('e');
// => 'found'
Returns a random number between min and max.
min: number;
max: number;
number;
random(0, 100);
// => 44
Removes all provided elements from the array.
...args: Array
Array;
remove([1, 2, 3, 4], 1, 2, 3);
// => [4]
remove(['a', 2, 'b', 4], 1, 2, 'b');
// => ['a', 4]
Returns a new array with all falsy (false, 0, '', "", null, undefined, or NaN) values removed.
arr: amy;
Array;
removeFalsy([1, 2, 3, false, 0, '', '', null, undefined, NaN, 'hello']);
// => [1 , 2, 3, 'hello']
Returns a new array, shuffled.
list: Array;
Array;
shuffle([1, 2, 3, 4, 5]);
// => [2,1,5,3,4]
shuffle([1, 2, 3, 4, 5]);
// => [4,5,1,2,3]
Returns all elements of the array expect for the head
arr: Array;
Array;
tail([1, 2, 3]);
// => [2, 3]
Converts a string to lower case and removes all non-alpha characters
text: String;
string;
toLower('123ABc!_*&&34:"{}');
// => 'abc'
Converts a string to upper case and removes all non-alpha characters
text: String;
string;
toUpper('--foo-bar');
// => FOOBAR