A monad for
working with values that may or may not be null or undefined.
Inspired by James Sinclair, Pratica and Eric Elliott.
import { Maybe } from '@appsweet-co/utils';
Return a new monad from inside the first. This is a clean way to work with
a second value that may or may not be null or undefined.
Maybe(6).chain(x => Maybe(null))
// => Maybe(null)
Chain as many monads as you need:
Maybe(6)
.chain(x => Maybe(x + 1)) // => Maybe(7)
.chain(y => Maybe(y + 1)) // => Maybe(8)
// etc...
Return a monad with the fallback value. This is a clean way to guarantee a default value.
Maybe(null).else(6)
// => Maybe(6)
Test the value against one or more predicates. Returns a monad with the value if and only if all predicates return true.
Functions are composed left-to-right.
Maybe(6).filter(x => x % 2 === 0, x < 10)
// => Maybe(6)
Maybe(6).filter(x => x % 2 === 0, x < 5)
// => Maybe(undefined)
Transform the value inside the monad using one or more transformation functions.
Functions are composed left-to-right.
Maybe(6).map(x => x * 2, y => y + 5)
// => Maybe(17)
Send the value to one or more functions and return the original value. This is a clean way to cause side effects.
Functions are composed left-to-right.
Maybe(6)
.pipe(x => x * 2, console.log) // => 12
.map(x => x + 2)
.pipe(console.log) // => 8
Unwrap the value from the monad and returns the value in its raw form.
Maybe(6).value();
// => 6
Adds two numbers. Same as a + b.
Inspired by Ramda.js
import { add } from '@appsweet-co/utils';
add(2, 3);
// => 5
const add2 = add(2);
add2(3);
// => 5
The following are equivalent:
add(a)(b);
add(a, b);
Returns true if and only if all elements of the list match the predicate.
Same as list.every(fn).
Inspired by Ramda.js.
import { all } from '@appsweet-co/utils';
all(x => x === 2, [2, 2, 2]);
// => true
const all2 = all(x => x ===2);
all2([2, 2, 10]);
// => false
The following are equivalent:
all(fn)(list);
all(fn, list);
Returns true if at least one element in the list matches the predicate.
Same as list.some(fn).
Inspired by Ramda.js.
import { any } from '@appsweet-co/utils';
any(x => x === 2, [1, 2, 3]);
// => true
const any2 = any(x => x === 2);
any2([1, 3, 5]);
// => false
The following are equivalent:
any(fn)(list);
any(fn, list);
Binds arguments to any function. Same as fn.bind(null, ...args).
Inspired by Ramda.js.
import { bind } from '@appsweet-co/utils';
const add = (a, b) => a + b;
const add2 = bind(add, 2);
add2(3);
// => 5
Returns a number between min and max values.
Inspired by Ionic.
import { clamp } from '@appsweet-co/utils';
clamp(3, 5, 1);
=> 3
clamp(3, 5, 4);
// => 4
clamp(3, 5, 12);
// => 5
const range = clamp(3, 5);
range(1);
// => 3
range(4);
// => 4
range(12);
// => 5
The following are equivalent:
clamp(min)(max)(val);
clamp(min)(max, val);
clamp(min, max)(val);
clamp(min, max, val);
Returns a deep copy of an object of any depth, if able.
Returns val unchanged if val is not an object.
Inspired by Hey, JavaScript.
import { clone } from '@appsweet-co/utils';
const copy = clone(complexObj);
Transforms a method of any arity into a curried function.
Inspired by William Vincent.
import { curry } from '@appsweet-co/utils';
const add = (a, b, c) => a + b + c;
const add9 = curry(add, 3, 6);
add9(5)
// => 14
curry() accepts an optional generic return type:
const add9 = curry<number>(add, 3, 6);
Decrements a value by one. Same as val - 1.
Inspried by Ramda.js
import { dec } from '@appsweet-co/utils';
dec(10);
// => 9
Divide the first argument by the second. Same as a / b.
Inspired by Ramda.js.
import { divide } from '@appsweet-co/utils';
divide(10, 2);
// => 5
const div10 = divide(10);
div10(2);
// => 5
The following are equivalent:
divide(a)(b);
divide(a, b);
Applies one or more predicates to each item in a list.
Functions are composed left-to-right.
import { filter } from '@appsweet-co/utils';
const evens = filter(x => x !== 0, y => y % 2 ===0);
evens([1, 2, 3, 0, 4, 5]);
// => [2, 4]
This function works very well with pipe.
Returns the first n values in a list.
Inspried by Real World Haskell.
import { first } from '@appsweet-co/utils';
first(3, [1, 2, 3, 0, 4, 5]);
// => [1, 2, 3]
const first3 = first(3)
first3([1, 2, 3, 0, 4, 5]);
// => [1, 2, 3]
The following are equivalent:
first(n)(list);
first(n, list);
Increments a value by one. Same as val + 1.
Inspried by Ramda.js
import { inc } from '@appsweet-co/utils';
inc(10);
// => 11
Returns the last n values in a list.
Inspried by Real World Haskell.
import { last } from '@appsweet-co/utils';
last(3, [1, 2, 3, 0, 4, 5]);
// => [0, 4, 5]
const last3 = last(3)
last3([1, 2, 3, 0, 4, 5]);
// => [0, 4, 5]
The following are equivalent:
last(n)(list);
last(n, list);
Applies one or more transformation functions to each item in a list.
Functions are composed left-to-right.
Inspired by Ramda.js.
import { map } from '@appsweet-co/utils';
const add4 = map(x => x + 1, y => y + 3);
add4([1, 2, 3])
// => [5, 6, 7]
This function works very well with pipe.
Returns the largest of zero or more numbers given as input. Same as Math.max().
Return a curried function by passing only one parameter in the initial call.
import { max } from '@appsweet-co/utils';
max(3, 6, 9);
// => 9
const max9 = max(9);
max9(3, 12);
// => 12
max9(3, 6);
// => 9
The following are equivalent:
max(a)(...more);
max(a, ...more);
Returns the largest of zero or more numbers given as input. Same as Math.min().
Return a curried function by passing only one parameter in the initial call.
import { min } from '@appsweet-co/utils';
min(3, 6, 9);
// => 3
const min9 = min(9);
min9(3, 12);
// => 3
The following are equivalent:
min(a)(...more);
min(a, ...more);
Negates a value or an array of values.
Inspired by Ramda.js.
import { neg } from '@appsweet-co/utils';
neg(9);
// => -9
neg([9, -13]);
// => [-9, 13]
Returns true if the value is null or undefined.
import { nil } from '@appsweet-co/utils';
nil([1, 2, 3, 0, 4, 5]);
// => false
nil(null);
// => true
Returns true if and only if no elements of of the list match the predicate
Same as !list.every(fn).
Inspired by Ramda.js.
import { none } from '@appsweet-co/utils';
none(x => x === 2, [1, 2, 3]);
// => false
const none2 = none(x => x ===2);
none2([1, 3, 5]);
// => true
The following are equivalent:
none(fn)(list);
none(fn, list);
Returns val if and only if:
val is not null or undefined; andval and fallback are of the same type.Returns fallback otherwise.
import { or } from '@appsweet-co/utils';
or([], [1, 2, 3, 0, 4, 5]);
// => [1, 2, 3, 0, 4, 5]
const orArray = or([]);
orArray(true);
// => [];
The following are equivalent:
or(fallback)(val); or(fallback, val);
Parse a JSON string, or return the original.
import { parse } from '@appsweet-co/utils';
parse('foobar');
// => 'foobar'
parse('{"hello": "world"}');
// => { hello: 'world' }
Get the numerical percentage of a value compared against min and max values.
import { percent } from '@appsweet-co/utils';
percent(0, 10, 5);
// => 50
const scale = percentC(0, 10);
scale(5);
// => 50
The following are equivalent:
percent(min)(max)(val);
percent(min)(max, val);
percent(min, max)(val);
percent(min, max, val);
Compose functions left-to-right.
Inspired by Eric Elliott and Ramda.js.
import { pipe } from '@appsweet-co/utils';
const roundedLog = pipe(Math.log, Math.round);
roundedLog(123.45)
// => 5
Returns the value of the supplied property. Same as obj[key].
Inspired by Krzysztof Czernek and Ramda.js.
import { prop } from '@appsweet-co/utils';
prop('bananas', { bananas: 7 });
// => 7
const bananaCount = prop('bananas');
bananaCount({ bananas: 7 });
// => 7
The following are equivalent:
prop(key)(obj);
prop(key, obj);
Get the decimal fraction of a value compared against min and max values.
import { ratio } from '@appsweet-co/utils';
ratio(0, 10, 5);
// => 0.5
const range = ratio(0, 10);
range(5);
// => 0.5
The following are equivalent:
ratio(min)(max)(val);
ratio(min)(max, val);
ratio(min, max)(val);
ratio(min, max, val);
Executes a reducer function on each element of an array and returns a single
output value. Same as list.reduce(fn, init).
Inspired by Mozilla.
import { reduce } from '@appsweet-co/utils';
reduce((acc, cur) => acc + cur, [1, 2, 3]);
// => 6
const sum = reduce((acc, cur) => acc + cur);
sum([1, 2, 3]);
// => 6
The following are equivalent:
reduce(fn)(list);
reduce(fn, list);
Stringify a value, or return the original.
import { stringify } from '@appsweet-co/utils';
stringify('foobar');
// => 'foobar'
stringify(123);
// => '123'
Subtract the first argument from the second. Same as b - a.
Inspired by Ramda.js
import { subtract } from '@appsweet-co/utils';
subtract(5, 9);
// => 4
const sub5 = subtract(5);
sub5(9);
// => 4
The following are equivalent:
subtract(a)(b);
subtract(a, b);
Sets a timer that executes a function once the timer expires.
Same as setTimeout(fn, delay).
import { timeout } from '@appsweet-co/utils';
timeout(3000, () => { console.log('click')});
// => 'click'
const click = timeout(3000);
click(() => { console.log('click')});
// => 'click'
The following are equivalent:
click(delay)(fn);
click(delay, fn);
Compares a value against a given type. Same as typeof val === kind.
Inspried by Ramda.js.
import { type } from '@appsweet-co/utils';
type('string', 'foobar');
// => true
const isNumber = type('number');
isNumber('foobar');
// => false
The following are equivalent:
type(kind)(val);
type(kind, val);
Returns an array of arrays. The first element is a list of items that pass the predicate. The second element is a list of items that fail.
import { unzip } from '@appsweet-co/utils';
unzip(x => x % 2 === 0, [1, 2, 3, 0, 4, 5])
// => [[2, 0, 4], [1, 3, 5]]
const evens = unzip(x => x % 2 === 0)
evens([1, 2, 3, 0, 4, 5])
// => [[2, 0, 4], [1, 3, 5]]
The following are equivalent:
unzip(fn)(list);
unzip(fn, list);
Generated using TypeDoc
A Monad-like function that tests a value against a predicate.
Calls
rightif the test passes orleftif the test fails.import { Either } from '@appsweet-co/utils'; const moreThan6 = Either({ test: x => x > 6, left: x => onFail(x), right: x => onSuccess(x) }); moreThan6(8); // => onSuccess(8) moreThan6(4); // => onFail(4)