Fetching contributors…
Cannot retrieve contributors at this time
1517 lines (1188 sloc) 34.5 KB

# API

## Function

### apply

• `(f, iterable) => f(...iterable)`
• source
```const args = [10, 20];

### call

• `(f, ...args) => f(...args)`
• source
```add(10, 20); // 30

### calls

• `([(a, b) => c, (a, b) => d, ...], a, b) => [c, d, ...]`
• `([(a, b) => Promise c, (a, b) => Promise d, ...], a, b) => Promise [c, d]`
• `({ k: (a, b) => c, k2: (a, b) => d }, a, b) => { k: c, k2: d }`
• `({ k: (a, b) => Promise c, k2: (a, b) => Promise d }, a, b) => Promise { k: c, k2: d }`
• source
```calls([
a => a + 1,
a => a + 2
], 10);
// [11, 12]

calls({
a: a => a + 1,
b: a => a + 2
}, 10);
// {a: 11, b: 12}

calls([
_ => Promise.resolve(1),
_ => Promise.resolve(2),
_ => Promise.resolve(3)
]).then(log);
// [1, 2, 3]

calls({
a: _ => Promise.resolve(1),
b: _ => Promise.resolve(2),
c: _ => Promise.resolve(3)
}).then(log);
// {a: 1, b: 2, c: 3}```

### constant

• `a => _ => a`
• source
```const a = constant('A');
a(); // A
a(); // A```

### curry

• `((a, b, ...) => e) => a => (b, ...) => e`
• `((a, b, ...) => e) => (a, b, ...) => e`
• source
```const add = curry((a, b) => a + b);

### curryN

```const addAll = (...args) => args.reduce((a, b) => a + b);

### go

• `(a, a => b, b => c, ..., y => z) => z`
• `(Promise a, a => b, b => c, ..., y => z) => Promise z`
• `(a, a => Promise b, b => Promise c, ..., y => z) => Promise z`
• source
```go(0,
a => a + 1,
a => a + 10,
log); // 11

go(0,
a => Promise.resolve(a + 1),
a => a + 10,
log); // 11

const b = go(0,
a => a + 1,
a => a + 10);
log(b); // 11

const pb = go(0,
a => Promise.resolve(a + 1),
a => a + 10);
pb.then(log); // 11```

### juxt

```const compute = juxt(min, max, sum, mean);
log(...compute([1, 2, 3, 4, 5])); // 1, 5, 15, 3```

### negate

• `f => a => !f(a)`
• source
```const a = negate(a => a);
log(a(true)); // false
log(a(false)); // true```

### once

```const f = once(a => a + 10);
log(f(5)); // 15
log(f(5)); // 15```

### pipe

• `((a, b, ...) => d, d => e, ..., y => z) => (a, b, ...) => z`
• `((a, b, ...) => Promise d, d => e, e => Promise f, ..., y => z) => (a, b, ...) => Promise z`
• source
```const f1 = pipe(a => a.toUpperCase(), a => a == 'A');
const b = f1('a');
log(b); // true

const total = f => pipe(
map(f),
reduce((a, b) => a + b));

const totalAge = total(({age}) => age);

go(
fetchUsers(),
totalAge,
log);
// 186

go(
fetchProducts(),
total(({price}) => price),
log);
// 156000```

### tap

• `(g, f, ...) => a => go(a, g, f, ..., _ => a)`
• source
```go(
10,
a => a + 5,
tap(
a => a + 5,
log), // 20
a => a + 10,
log); // 25```

## Strict

• Number => Number => Number
• source
```add(10, 5);
// 15

// 15```

### baseSel

```const sel = baseSel('.');
sel('a.b', { a: { b: 10 }});
// 10

sel('a.b', { b: { c: 20 }});
// undefined

const sel2 = baseSel('>');
sel2('a>b', { a: { b: 10 }});
// 10

sel2('a>b', { b: { c: 20 }});
// undefined```

### chunk

```chunk(2, [1, 2, 3, 4, 5]);
// [[1, 2], [3, 4], [5]]```

### compact

• `Iterable a => [a]`
• `Iterable Promise a => Promise [a]`
• source
```compact([1, 2, 0, false, true, null]);
// [1, 2, true]```

### countBy

• (a => b) => Iterable a => { [b]: n }
• (a => b) => Iterable Promise a => Promise { [b]: n }
• (a => Promise b) => Iterable a => Promise { [b]: n }
• (a => Promise b) => Iterable Promise a => Promise { [b]: n }
• source
```countBy(a => a % 2 ? 'odd' : 'even', [1, 2, 3, 4, 5]);
// { odd: 3, even: 2 }```

### deepFlat

• [[[[a]]]] => [a]
• Iterable Iterable Iterable ... Iterable a => [a]
• [Promise [[Promise a]]] => Promise [a]
• [Promise [[Iterable Promise a]]] => Promise [a]
• source
```deepFlat([[1, 2, [3, [4, 5, [6], [[7]]]]]]);
// [1, 2, 3, 4, 5, 6, 7];```

### defaults

• ({}, {}, ..., {}) => {}
• source
```defaults({flavor: "chocolate"}, {flavor: "vanilla", sprinkles: "lots"});
// {flavor: "chocolate", sprinkles: "lots"}```

### defaultTo

```const obj = {a: 1, c: null, d: NaN};
defaultTo(0, obj.a);
// 1
defaultTo(0, obj.b);
// 0
defaultTo(0, obj.c);
// 0
defaultTo(0, obj.d);
// 0```

### delay

• time => a => Promise a
• (time, a) => Promise a
• source
```go(
'hi',
delay(1000),
log); // After 1 second "hi"```

### difference

```difference([2, 3], [2, 1]);
// [1]
difference([2, 2, 2, 2], [1, 1, 1, 1, 1]);
// [1, 1, 1, 1, 1]
difference([1, 2, 3, 4], [1]);
// []
difference([2], [1, 2, 3, 4]);
// [1, 3, 4]```

### differenceBy

```differenceBy(a => a.x, [{ x: 1 }], [{ x: 2 }, { x: 1 }]);
// [{ x: 2 }]```

### differenceWith

```const cmp = (x, y) => x.a === y.a;
const l1 = [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}];
const l2 = [{a: 3}, {a: 4}];
differenceWith(cmp, l1, l2);
// [{a: 1}, {a: 2}, {a: 5}]```

### drop

```drop([1, 2, 3, 4]);
// [2, 3, 4]```

### dropRight

```dropRight([1, 2, 3, 4]);
// [1, 2, 3]```

### dropUntil

```dropUntil(a => a > 1, [1, 2, 3, 4]);
// [3, 4]```

### dropWhile

```dropWhile(a => a < 3, [1, 2, 3, 4]);
// [3, 4]```

### each

• (a => b) => Iterable a => [a]
• (a => b) => Iterable Promise a => Promise [a]
• (a => Promise b) => Iterable a => Promise [a]
• (a => Promise b) => Iterable Promise a => Promise [a]
• source
```go(
document.querySelectorAll('.post'),
each(el => el.innerHTML = ''),
log); // [div.post, div.post, div.post];```

### entries

```entries({a: 1, b: 2, c: 3});
// [['a', 1], ['b', 2], ['c', 3]]```

### extend

• ({}, {}, ..., {}) => {}
• source
```extend({flavor: "vanilla", sprinkles: "lots"}, {flavor: "chocolate"});
// {flavor: "chocolate", sprinkles: "lots"}```

### filter

• `(a => Boolean) => Iterable a => [a]`
• `(a => Boolean) => Iterable Promise a => Promise [a]`
• `(a => Promise Boolean) => Iterable a => Promise [a]`
• `(a => Promise Boolean) => Iterable Promise a => Promise [a]`
• source
```filter(a => a % 2, [1, 2, 3]);
// [1, 3]

filter(a => a % 2, [
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3)
]).then(log);
// [1, 3]

filter(
a => Promise.resolve(a % 2),
[1, 2, 3]
).then(log);
// [1, 3]

filter(a => Promise.resolve(a % 2), [
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3)
]).then(log);
// [1, 3]```

### find

• find = head . L.filter
• (a => Boolean) => Iterable a => a
• (a => Promise Boolean) => Iterable a => Promise a
• (a => Boolean) => Iterable Promise a => Promise a
• (a => Promise Boolean) => Iterable Promise a => Promise a
• source
```find(a => a > 3, [1, 2, 3, 4, 5]);
// 4

find(({age}) => age == 23, [
{ name: 'a', age: 15, ... },
{ name: 'b', age: 19, ... },
{ name: 'c', age: 23, ... },
{ name: 'd', age: 17, ... },
{ name: 'e', age: 23, ... }
]);
// { name: 'c', age: 23, ... }```

### findWhere

• {k: v} => Iterable {k: v} => {k: v}
• {k: v} => Iterable Promise {k: v} => Promise {k: v}
• source
```findWhere({ age: 23 }, [
{ name: 'a', age: 15, ... },
{ name: 'b', age: 19, ... },
{ name: 'c', age: 23, ... },
{ name: 'd', age: 17, ... },
{ name: 'e', age: 23, ... }
]);
// { name: 'c', age: 23, ... }

findWhere({ name: 'e', age: 23 }, [
{ name: 'a', age: 15, ... },
{ name: 'b', age: 19, ... },
{ name: 'c', age: 23, ... },
{ name: 'd', age: 17, ... },
{ name: 'e', age: 23, ... }
]);
// { name: 'e', age: 23, ... }```

### flat

• (Iterable Iterable a, Number depth) => [a]
• (Iterable Promise Iterable a, Number depth) => Promise [a]
• (Iterable Iterable Promise a, Number depth) => Promise [a]
• source
```flat([[1, 2], [3, 4]]);
// [1, 2, 3, 4]

flat([[1, [2]], [[[3]]]);
// [1, [2], [[3]]];

flat([[1, [2]], [[[3]]], 2);
// [1, 2, [3]];

flat([[1, [2]], [[[3]]], 3);
// [1, 2, 3];

await flat([Promise.resolve([1, 2]), [Promise.resolve(3), 4]]);
// [1, 2, 3, 4]```

### flatMap

• flatMap = flat . mapLazy
• (a => Iterable b) => Iterable a => [b]
• (a => Iterable b) => Iterable Promise a => Promise [b]
• (a => Iterable Promise b) => Iterable a => Promise [b]
• (a => Promise Iterable b) => Iterable Promise a => Promise [b]
• (a => Promise Iterable Promise b) => Iterable a => Promise [b]
• (a => Promise Iterable Promise b) => Iterable Promise a => Promise [b]
• source
```flatMap(a => range(a), [1, 2]);
// [0, 0, 1]

await flatMap(a => Promise.resolve(range(a)), [1, 2]);
// [0, 0, 1]```

### groupBy

```groupBy(a => a % 2 ? 'odd' : 'even', [1, 2, 3, 4, 5]);
// { odd: [1, 3, 5], even: [2, 4] }```

```head([1, 2, 3, 4]);
// 1```

### identity

`const identity = a => a;`

### indexBy

```const products = [{id: 1, price: 100}, {id: 3, price: 100}, {id: 5, price: 100}];
indexBy(p => p.id, products);
// {1: {id: 1, price: 100}, 3: {id: 3, price: 100}, 5: {id: 5, price: 100}}```

### initial

```initial([1, 2, 3]);
// [1, 2]```

### intersection

```intersection([2, 1], [2, 3]);
// [2]
intersection([1, 2, 1, 1, 3], [1, 1, 1, 2, 4]);
// [1, 2]```

### intersectionBy

```intersectionBy(o => o.x, [{ x: 2 }, { x: 1 }], [{ x: 1 }]);
// [{ x: 1 }]```

### intersectionWith

```const cmp = (x, y) => x.a === y.a;
const l1 = [{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}];
const l2 = [{a: 3}, {a: 4}];
intersectionWith(cmp, l1, l2);
// [{a: 3}, {a: 4}]```

### keys

```keys({a: 1, b: 2, c: 3});
// ['a', 'b', 'c']```

### last

```last([1, 2, 3]);
// 3```

### map

• `(a => b) => Iterable a => [b]`
• `(a => b) => Iterable Promise a => Promise [b]`
• `(a => Promise b) => Iterable a => Promise [b]`
• `(a => Promise b) => Iterable Promise a => Promise [b]`
• source
```map(a => a + 10, [1, 2, 3]);
// [11, 12, 13]

map(a => Promise.resolve(a + 10), [1, 2, 3]).then(log);
// [11, 12, 13]

// ["META", "TITLE", "SCRIPT"]

map(a => a + 10, function* () {
yield 4;
yield 5;
} ());
// [14, 15]```

### mapEntries

• `(a => b) => Iterable [k, a] => [[k, b]]`
• `(a => b) => Iterable [k, Promise a] => Promise [[k, b]]`
• `(a => Promise b) => Iterable [k, a] => Promise [[k, b]]`
• `(a => Promise b) => Iterable [k, Promise a] => Promise [[k, b]]`
• source
```mapEntries(a => a + 10, [['a', 1], ['b', 2]]);
// [['a', 11], ['b', 12]]

mapEntries(a => Promise.resolve(a + 10), [['a', 1], ['b', 2]]).then(log);
// [['a', 11], ['b', 12]]

// entries == Object.entries
// object == Object.fromEntries
object(mapEntries(a => a + 10, entries({ a: 1, b: 2})));
// { a: 11, b: 12 }

go({ a: 1, b: 2},
entries,
mapEntries(a => Promise.resolve(a + 10)),
object
).then(log);
// { a: 11, b: 12 }```

### mapObject

• `(a => b) => { k: a } => { k: b }`
• `(a => b) => { k: Promise a } => Promise { k: b }`
• `(a => Promise b) => { k: a } => Promise { k: b }`
• `(a => Promise b) => { k: Promise a } => Promise { k: b }`
• source
```mapObject(a => a + 10, { a: 1, b: 2 });
// { a: 11, b: 12 }

mapObject(a => Promise.resolve(a + 10), { a: 1, b: 2 }).then(log);
// { a: 11, b: 12 }

go(
{ a: 1, b: 2 },
mapObject(a => Promise.resolve(a + 10)),
log);
// { a: 11, b: 12 }```

### max

```max([1, 3, 7, 4]);
// 7```

### maxBy

```maxBy(a => a * -1, [1, 3, 7, 4]);
// 1```

### min

```min([1, 3, 7, 4]);
// 1```

### minBy

```minBy(a => a * -1, [1, 3, 7, 4]);
// 7```

### noop

`function noop() {}`

### object

```object([['a', 1], ['b', 2], ['c', 3]]);
// {a: 1, b: 2, c: 3}```

### omit

```omit(['a, c'], {a: 1, b: 2, c: 3, d: 4});
// {b: 2, d: 4}```

### partition

```partition(a => a % 2, [1, 2, 3, 4, 5]);
// [[1, 3, 5], [2, 4]]```

### pick

```pick(['a, c'], {a: 1, b: 2, c: 3, d: 4});
// {a: 1, c: 3}```

### pluck

• `String k => Iterable a => [a[k]]`
• `String k => Iterable Promise a => Promise [a[k]]`
• source
```pluck('id', [{ id: 1 }, { id: 3 }]);
// [1, 3]```

### range

• `([start=0], end, [step=1]) => [Number a, ...]`
• source
```range(4);
// => [0, 1, 2, 3]

range(-4);
// => [0, -1, -2, -3]

range(1, 5);
// => [1, 2, 3, 4]

range(0, 20, 5);
// => [0, 5, 10, 15]

range(0, -4, -1);
// => [0, -1, -2, -3]```

### reduce

• `((a, b) => c) => Iterable a => c`
• `((a, b) => Promise c) => Iterable a => Promise c`
• `((a, b) => c) => Iterable Promise a => Promise c`
• `((acc, b) => acc) => acc => Iterable b => acc`
• `((acc, b) => Promise acc) => acc => Iterable b => Promise acc`
• `((acc, b) => acc) => acc => Iterable Promise b => Promise acc`
• source
```const add = (a, b) => a + b

// 6

// 16

Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3)
])
// 6```

### reject

• `(a => Boolean) => Iterable a => []`
• `(a => Boolean) => Iterable Promise a => Promise []`
• `(a => Promise Boolean) => Iterable a => Promise []`
• `(a => Promise Boolean) => Iterable Promise a => Promise []`
• source
```reject(a => a % 2, [1, 2, 3]);
// [2]

reject(a => a % 2, [
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3)
]).then(log);
// [2]

reject(
a => Promise.resolve(a % 2),
[1, 2, 3]
).then(log);
// [2]

reject(a => Promise.resolve(a % 2), [
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3)
]).then(log);
// [2]```

### unique

• `Iterable a => [a]`
• `Iterable a => Promise [a]`
• source
```unique([1, 2, 3, 1, 2, 4]);
// [1, 2, 3, 4]```

### uniqueBy

• `(a => b) => Iterable a => [a]`
• `(a => b) => Iterable Promise a => Promise [a]`
• `(a => Promise b) => Iterable a => Promise [a]`
• `(a => Promise b) => Iterable Promise a => Promise [a]`
• source
```const users = [
{name: 'aa'},
{name: 'Aa'},
{name: 'bb'},
{name: 'cc'},
{name: 'bb'}
];

uniqueBy(u => u.name.toUpperCase(), users);
// [{name: 'aa'}, {name: 'bb'}, {name: 'cc'}]```

## Lazy

### L.filter

```const iterator = L.filter(a => a % 2, [1, 2, 3]);
iterator.next(); // { value: 1, done: false }
iterator.next(); // { value: 3, done: false }
iterator.next(); // { value: undefined, done: true }

go(
L.range(1, Infinity),
L.filter(a => a % 2),
take(2));
// [1, 3]

await go(
L.range(Infinity),
L.map(a => Promise.resolve(a)),
L.filter(a => a % 2),
take(2));
// [1, 3]```

## Stoppable

### reduceS stop

```reduceS((a, b) => {
const res = a + b;
return res > 5  ? stop(res) : res;
}, [1, 2, 3, 4]);
// 6```

### goS, pipeS, stop, stopIf

```const f1 = pipeS(a => a % 2 ? stop(a) : a, a => a + 10);
f1(1);
// 1
f1(2);
// 12

const f2 = pipeS(stopIf(a => a % 2), a => a + 10);
f2(1);
// 1
f2(2);
// 12

goS({a: 1, b: 2}, stopIf({a: 1}), ({a, b}) => ({a: a + 10, b}));
// {a: 1, b: 2}

goS({a: 2, b: 2}, stopIf({a: 1}), ({a, b}) => ({a: a + 10, b}));
// {a: 12, b: 2}

goS({a: 1, b: 2},
stopIf({a: 1}, null),
({a, b}) => ({a: a + 10, b}));
// null```

## String

### string

You can’t perform that action at this time.