In this documentation, several words are use to groups javascript types together. They are described here.
Native javascript types:
undefined
null
Number
String
Array
Object
Date
RegExp
Error
...
undefined
null
Here in yambda Object
only designate plain javascript object.
String
Array
Every native except void but including custom class. As in javascript, classes are just functions, it designate the class constructor.
Every javascript class created or imported from an another library.
fn
(Function): Function to curry
(Function): the curried function with not a fixed arity
const add = (a, b) => a + b;
const addCurried = curry(add);
const inc = addCurried(1);
const dec = addCurried(-1);
addCurried(1, 2); // 3
addCurried(1)(2); // 3
inc(42); // 43
dec(42); // 41
const sum4 = (a, b, c, d) => a + b + c + d;
const sum4Curried = curry(sum4);
sum4Curried(1, 2, 3, 4); // 10
sum4Curried(1, 2, 3, 4, 5); // 10
sum4Curried(1)(2)(3)(4); // 10
sum4Curried(1)(2, 3)(4); // 10
sum4Curried(1, 2, 3)(4); // 10
sum4Curried(1, 2)(3, 4); // 10
Apply an array of arguments to a function.
fn
(Function): the function to be appliedargs
(Array): the array of arguments to apply
(Any): the return of the fn
function called with the arguments args
.
const add = (a, b) => a + b;
const values = [2, 3];
apply(add, values); // 5
Change the order of a two parameters function.
fn
(Function): the function to be swapb
(Any): the second param of the original functiona
(Any): the first param of the original function
(Any): the same result as fn
const add = a => b => a + b;
const inc = add(1);
const dec = add(-1);
const myArray = [1, 2, 3];
const mapMyArrayOn = swap(map, myArray);
mapMyArrayOn(inc); // [2, 3, 4]
mapMyArrayOn(dec); // [0, 1, 2]
Compare a define if subject
has the type type
or if type
and subject
have the same type.
type
(Any): Could be a type or any value that need to be comparesubject
(Any): The value to be compare
(Boolean): if subject
has the type type
or if type
and subject
have the same type
// class
is(String, 'foo'); // true
is(Boolean, false); // true
is(RegExp, /abc/); // true
is(Date, 42); // false
// values
is(undefined, undefined); // true
is(true, false); // true
is({}, { foo: 'bar' }); // true
is({}, new Date()); // false
is(Function, Boolean); // true
is(String, Boolean); // false
// custom classes
class Foo {};
is(Foo, new Foo()); // true
Get the type of a value
.
value
(Any): The value on which type is desired
(String): the type of value
// working with existing types
typeOf(null); // 'null'
typeOf(42); // 'Number'
typeOf({}); // 'Object'
typeOf(new Date()); // 'Date'
// but also with custom classes
class Foo {};
class Bar extends Foo {};
typeOf(Foo); // 'Function'
typeOf(new Foo()); // 'Foo'
typeOf(new Bar()); // 'Bar'
Same as typeOf but if value is a class, it return the name of the class instead of "Function"
.
Throw an error if subject
has not the type
otherwise return subject
.
type
(Any): Could be a class or any value that need to be comparesubject
(Any): The value to be compare
(Any): subject
const upCase = (str) => mustBe(String, str).toUpperCase();
upCase('foo'); // 'FOO'
upCase(42); // TypeError: 42 is not a(n) String
const num = mustBe(Number);
const add = (a, b) => num(a) + num(b);
add(2, 3); // 5
add('foo', 3) // TypeError: foo is not a(n) Number
Return a fresh copy of subject. (Not a deep copy)
subject
(Any): The value to clone
(Any): subject
cloned
const n = 42;
clone(42); // 42
n === clone(42); // true
const arr = [1, 2];
clone(arr); // [1, 2]
arr === clone(arr); // false
See Functor;
fn
(Function): The function that handle the changesubject
(Any): The value to map over
(Any): the new value
const add = (a) => (b) => a + b;
const inc = map(add(1));
inc(42); // 43
inc([1, 2, 3]); // [2, 3, 4]
inc({ a: 4, b: 5, c: 6 }); // { a: 5, b: 6, c: 7 }
Iterate on subject
and call fn
with:
- result: value that
fn
must return and that has the value ofinitialValue
at the begening - value: current value
- index/key: index or key of the current value
subject
It will return result at the end.
fn
(Function): first ObjectinitialValue
(Any): second Object that will overridea
propertiessubject
(Any): The value to reduce over
(Object): the final result
const add = (a, b) => a + b;
const sum = reduce(add, 0);
sum(42); // 42
sum([1, 2, 3]) // 6
sum({ a: 1, b: 2, c: 3 }); // 6
const products = [
{ id: 1, qty: 5},
{ id: 1, qty: 2},
{ id: 1, qty: 3}
];
const sumQty = reduce((res, { qty }) => res + qty, 0);
const totalQty = sumQty(products); // 10
Merge object b
in object a
with no mutation.
a
(Object): first Objectb
(Object): second Object that will overridea
properties
(Object): the new Object
const a = { foo: 42, bar: 'baz' };
const b = { foo: 5, baz: true };
assign(a, b); // { foo: 5, bar: 'baz', baz: true }
assign(b, a); // { foo: 42, bar: 'baz', baz: true }
Returns an array of keys from the Object obj
.
obj
(Object)
(Array): an array of keys of obj
.
keys({ foo: 1, bar: 2, baz: 3 }); // ['foo', 'bar', 'baz']
keys({}); // []