0.4.1 • Published 6 years ago

almost-functional v0.4.1

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

Almost Functional

Docs are a work in progress.

Install

npm install almost-functional

API

compose

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

Param
...fns: Array
Return
Function;
Example
const square = (n) => n * n;
const mult = (a, b) => a * b;

const multSquare = compose(
  square,
  mult,
);
multSquare(10, 10);
// => 10000

curry

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.

Param
fn: Function;
Return
Function || any;
Example
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

first

Returns the first element in an array or undefined

Param
arr: Array;
Return
any;
Example
first([1, 2, 3]);
// => 1
first([]);
// => undefined

flatten

Flattens an array one level deep

Param
arr: Array;
Return
Array;
Example
flatten([1, 2, 3, [4, 5, 6]]);
// => [1, 2, 3, 4, 5, 6]

flattenDeep

Recursively flattens an array

Param
arr: Array;
Return
Array;
Example
flattenDeep([1, 2, 3, [4, 5, [6, 7, [8, 9]]]]);
// => [1, 2, 3, 4, 5, 6, 7, 8, 9]

forEach

Iterates over the provided array and invokes the iteratee on each element. The iteratee is provided the value, index and array.

Param
arr: Array;
iteratee: Function;
Return
void
Example
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

fromPairs

Returns an object created from the key:value pairs provided.

Param
pairsArr: Array;
Return
Object;
Example
fromPairs(['a', 1], ['b', 2]);
// => {a: 1, b: 2}

head

Returns the first element in an array or undefined

Param
arr: Array;
Return
any;
Example
head([1, 2, 3]);
// => 1
head([]);
// => undefined

isFunction

Checks if the value is a function

Param
val: any;
Return
boolean;
Example
isFunction(() => {});
// => true
isFunction({});
// => false

isObject

Checks if the value is a object

Param
val: any;
Return
boolean;
Example
isObject({});
// => true
isObject(null);
// => false
isObject(() => {});
// => false

isObjectLike

Checks if the value is object like, which is not null and an object.

Param
val: any;
Return
boolean;
Example
isObjectLike(null);
// => false
isObjectLike([]);
// => true
isObjectLike({});
// => true

isPlainObject

Checks if the value has been created by the Object constructor.

Param
val: any;
Return
boolean;
Example
class A {}
isPlainObject(new A());
// => false
isPlainObject({});
// => true

keys

Returns an array from the key values from the provided object.

Param
obj: Object;
Return
Array;
Example
keys('almost-functional');
// => []
keys({a: 1, b: 2});
// ['a', 'b']

last

Returns the last element of an array or undefined

Param
...args: Array
Return
any;
Example
last([]);
// => undefined
last([1, 2, 3]);
// => 3

merge

Merges the arguments provided.

Param
...args: Array
Return
Object;
Example
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}

mergeSafe

Merges the arguments provided, remove all none objects, null, and arrays.

Param
...args: Array
Return
Object;
Example
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}

pipe

Composes a function that will pipe the result of the invoking function to the next function.

Param
...fns: Array
Return
Function;
Example
const square = (n) => n * n;
const mult = (a, b) => a * b;

const multSquare = pipe(
  mult,
  square,
);
multSquare(10, 10);
// => 10000

pluckDeep

Recursively searches an object for a specified keys and returns it or null.

Param
obj: Object;
Return
any;
Example
const pluckObj = pluckDeep({a: {b: {c: 1, d: {e: 'found'}}}});

pluckObj('f');
// => null
pluckObj('e');
// => 'found'

random

Returns a random number between min and max.

Param
min: number;
max: number;
Return
number;
Example
random(0, 100);
// => 44

remove

Removes all provided elements from the array.

Param
...args: Array
Return
Array;
Example
remove([1, 2, 3, 4], 1, 2, 3);
// => [4]
remove(['a', 2, 'b', 4], 1, 2, 'b');
// => ['a', 4]

removeFalsy

Returns a new array with all falsy (false, 0, '', "", null, undefined, or NaN) values removed.

Param
arr: amy;
Return
Array;
Example
removeFalsy([1, 2, 3, false, 0, '', '', null, undefined, NaN, 'hello']);
// =>  [1 , 2, 3, 'hello']

shuffle

Returns a new array, shuffled.

Param
list: Array;
Return
Array;
Example
shuffle([1, 2, 3, 4, 5]);
// => [2,1,5,3,4]
shuffle([1, 2, 3, 4, 5]);
// => [4,5,1,2,3]

tail

Returns all elements of the array expect for the head

Param
arr: Array;
Return
Array;
Example
tail([1, 2, 3]);
// => [2, 3]

toLower

Converts a string to lower case and removes all non-alpha characters

Param
text: String;
Return
string;
Example
toLower('123ABc!_*&&34:"{}');
// => 'abc'

toUpper

Converts a string to upper case and removes all non-alpha characters

Param
text: String;
Return
string;
Example
toUpper('--foo-bar');
// => FOOBAR
0.4.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago