@thi.ng/compose 2.1.49 → 2.1.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/comp.js +36 -34
- package/complement.js +5 -2
- package/constantly.js +4 -1
- package/delay.js +22 -18
- package/delayed.js +4 -1
- package/identity.js +4 -6
- package/ifdef.js +4 -7
- package/juxt.js +31 -28
- package/package.json +8 -6
- package/partial.js +25 -22
- package/promisify.js +6 -19
- package/thread-first.js +7 -39
- package/thread-last.js +7 -39
- package/trampoline.js +8 -35
package/CHANGELOG.md
CHANGED
package/comp.js
CHANGED
|
@@ -1,37 +1,39 @@
|
|
|
1
1
|
import { illegalArity } from "@thi.ng/errors/illegal-arity";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
2
|
+
function comp(...fns) {
|
|
3
|
+
let [a, b, c, d, e, f, g, h, i, j] = fns;
|
|
4
|
+
switch (fns.length) {
|
|
5
|
+
case 0:
|
|
6
|
+
illegalArity(0);
|
|
7
|
+
case 1:
|
|
8
|
+
return a;
|
|
9
|
+
case 2:
|
|
10
|
+
return (...xs) => a(b(...xs));
|
|
11
|
+
case 3:
|
|
12
|
+
return (...xs) => a(b(c(...xs)));
|
|
13
|
+
case 4:
|
|
14
|
+
return (...xs) => a(b(c(d(...xs))));
|
|
15
|
+
case 5:
|
|
16
|
+
return (...xs) => a(b(c(d(e(...xs)))));
|
|
17
|
+
case 6:
|
|
18
|
+
return (...xs) => a(b(c(d(e(f(...xs))))));
|
|
19
|
+
case 7:
|
|
20
|
+
return (...xs) => a(b(c(d(e(f(g(...xs)))))));
|
|
21
|
+
case 8:
|
|
22
|
+
return (...xs) => a(b(c(d(e(f(g(h(...xs))))))));
|
|
23
|
+
case 9:
|
|
24
|
+
return (...xs) => a(b(c(d(e(f(g(h(i(...xs)))))))));
|
|
25
|
+
case 10:
|
|
26
|
+
default:
|
|
27
|
+
const fn = (...xs) => a(b(c(d(e(f(g(h(i(j(...xs))))))))));
|
|
28
|
+
return fns.length === 10 ? fn : comp(fn, ...fns.slice(10));
|
|
29
|
+
}
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
function compL(...fns) {
|
|
32
|
+
return comp.apply(null, fns.reverse());
|
|
33
33
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
34
|
+
const compI = compL;
|
|
35
|
+
export {
|
|
36
|
+
comp,
|
|
37
|
+
compI,
|
|
38
|
+
compL
|
|
39
|
+
};
|
package/complement.js
CHANGED
package/constantly.js
CHANGED
package/delay.js
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
return this.value;
|
|
16
|
-
}
|
|
17
|
-
isRealized() {
|
|
18
|
-
return this.realized;
|
|
1
|
+
const delay = (body) => new Delay(body);
|
|
2
|
+
class Delay {
|
|
3
|
+
value;
|
|
4
|
+
body;
|
|
5
|
+
realized;
|
|
6
|
+
constructor(body) {
|
|
7
|
+
this.body = body;
|
|
8
|
+
this.realized = false;
|
|
9
|
+
}
|
|
10
|
+
deref() {
|
|
11
|
+
if (!this.realized) {
|
|
12
|
+
this.value = this.body();
|
|
13
|
+
this.realized = true;
|
|
19
14
|
}
|
|
15
|
+
return this.value;
|
|
16
|
+
}
|
|
17
|
+
isRealized() {
|
|
18
|
+
return this.realized;
|
|
19
|
+
}
|
|
20
20
|
}
|
|
21
|
+
export {
|
|
22
|
+
Delay,
|
|
23
|
+
delay
|
|
24
|
+
};
|
package/delayed.js
CHANGED
package/identity.js
CHANGED
package/ifdef.js
CHANGED
package/juxt.js
CHANGED
|
@@ -1,29 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
1
|
+
function juxt(...fns) {
|
|
2
|
+
const [a, b, c, d, e, f, g, h] = fns;
|
|
3
|
+
switch (fns.length) {
|
|
4
|
+
case 1:
|
|
5
|
+
return (x) => [a(x)];
|
|
6
|
+
case 2:
|
|
7
|
+
return (x) => [a(x), b(x)];
|
|
8
|
+
case 3:
|
|
9
|
+
return (x) => [a(x), b(x), c(x)];
|
|
10
|
+
case 4:
|
|
11
|
+
return (x) => [a(x), b(x), c(x), d(x)];
|
|
12
|
+
case 5:
|
|
13
|
+
return (x) => [a(x), b(x), c(x), d(x), e(x)];
|
|
14
|
+
case 6:
|
|
15
|
+
return (x) => [a(x), b(x), c(x), d(x), e(x), f(x)];
|
|
16
|
+
case 7:
|
|
17
|
+
return (x) => [a(x), b(x), c(x), d(x), e(x), f(x), g(x)];
|
|
18
|
+
case 8:
|
|
19
|
+
return (x) => [a(x), b(x), c(x), d(x), e(x), f(x), g(x), h(x)];
|
|
20
|
+
default:
|
|
21
|
+
return (x) => {
|
|
22
|
+
let res = new Array(fns.length);
|
|
23
|
+
for (let i = fns.length; i-- > 0; ) {
|
|
24
|
+
res[i] = fns[i](x);
|
|
25
|
+
}
|
|
26
|
+
return res;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
29
|
}
|
|
30
|
+
export {
|
|
31
|
+
juxt
|
|
32
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/compose",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.51",
|
|
4
4
|
"description": "Optimized functional composition helpers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -24,7 +24,9 @@
|
|
|
24
24
|
"author": "Karsten Schmidt (https://thi.ng)",
|
|
25
25
|
"license": "Apache-2.0",
|
|
26
26
|
"scripts": {
|
|
27
|
-
"build": "yarn
|
|
27
|
+
"build": "yarn build:esbuild && yarn build:decl",
|
|
28
|
+
"build:decl": "tsc --declaration --emitDeclarationOnly",
|
|
29
|
+
"build:esbuild": "esbuild --format=esm --platform=neutral --target=es2022 --tsconfig=tsconfig.json --outdir=. src/**/*.ts",
|
|
28
30
|
"clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc",
|
|
29
31
|
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
|
|
30
32
|
"doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
|
|
@@ -33,12 +35,12 @@
|
|
|
33
35
|
"test": "bun test"
|
|
34
36
|
},
|
|
35
37
|
"dependencies": {
|
|
36
|
-
"@thi.ng/api": "^8.9.
|
|
37
|
-
"@thi.ng/errors": "^2.4.
|
|
38
|
+
"@thi.ng/api": "^8.9.12",
|
|
39
|
+
"@thi.ng/errors": "^2.4.6"
|
|
38
40
|
},
|
|
39
41
|
"devDependencies": {
|
|
40
42
|
"@microsoft/api-extractor": "^7.38.3",
|
|
41
|
-
"
|
|
43
|
+
"esbuild": "^0.19.8",
|
|
42
44
|
"rimraf": "^5.0.5",
|
|
43
45
|
"tools": "^0.0.1",
|
|
44
46
|
"typedoc": "^0.25.4",
|
|
@@ -104,5 +106,5 @@
|
|
|
104
106
|
"default": "./trampoline.js"
|
|
105
107
|
}
|
|
106
108
|
},
|
|
107
|
-
"gitHead": "
|
|
109
|
+
"gitHead": "5e7bafedfc3d53bc131469a28de31dd8e5b4a3ff\n"
|
|
108
110
|
}
|
package/partial.js
CHANGED
|
@@ -1,24 +1,27 @@
|
|
|
1
1
|
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
2
|
+
function partial(fn, ...args) {
|
|
3
|
+
let [a, b, c, d, e, f, g, h] = args;
|
|
4
|
+
switch (args.length) {
|
|
5
|
+
case 1:
|
|
6
|
+
return (...xs) => fn(a, ...xs);
|
|
7
|
+
case 2:
|
|
8
|
+
return (...xs) => fn(a, b, ...xs);
|
|
9
|
+
case 3:
|
|
10
|
+
return (...xs) => fn(a, b, c, ...xs);
|
|
11
|
+
case 4:
|
|
12
|
+
return (...xs) => fn(a, b, c, d, ...xs);
|
|
13
|
+
case 5:
|
|
14
|
+
return (...xs) => fn(a, b, c, d, e, ...xs);
|
|
15
|
+
case 6:
|
|
16
|
+
return (...xs) => fn(a, b, c, d, e, f, ...xs);
|
|
17
|
+
case 7:
|
|
18
|
+
return (...xs) => fn(a, b, c, d, e, f, g, ...xs);
|
|
19
|
+
case 8:
|
|
20
|
+
return (...xs) => fn(a, b, c, d, e, f, g, h, ...xs);
|
|
21
|
+
default:
|
|
22
|
+
illegalArgs();
|
|
23
|
+
}
|
|
24
24
|
}
|
|
25
|
+
export {
|
|
26
|
+
partial
|
|
27
|
+
};
|
package/promisify.js
CHANGED
|
@@ -1,19 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* callback, which then either resolves or rejects the promise.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* (async () => {
|
|
12
|
-
* const body = await promisify(partial(fs.readFile, "foo.txt"));
|
|
13
|
-
* console.log(body.toString());
|
|
14
|
-
* })();
|
|
15
|
-
* ```
|
|
16
|
-
*
|
|
17
|
-
* @param fn - function accepting a callback
|
|
18
|
-
*/
|
|
19
|
-
export const promisify = (fn) => new Promise((resolve, reject) => fn((err, result) => (err != null ? reject(err) : resolve(result))));
|
|
1
|
+
const promisify = (fn) => new Promise(
|
|
2
|
+
(resolve, reject) => fn((err, result) => err != null ? reject(err) : resolve(result))
|
|
3
|
+
);
|
|
4
|
+
export {
|
|
5
|
+
promisify
|
|
6
|
+
};
|
package/thread-first.js
CHANGED
|
@@ -1,39 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
* @remarks
|
|
10
|
-
* This operator allows the code to be read more easily in the order of
|
|
11
|
-
* execution (same as the `->` operator/macro in Clojure).
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* const neg = (x) => -x;
|
|
16
|
-
* const sub = (a, b) => a - b;
|
|
17
|
-
* const div = (a, b) => a / b;
|
|
18
|
-
*
|
|
19
|
-
* // without operator: (-5 - 10) / 20
|
|
20
|
-
* console.log(div(sub(neg(5), 10), 20));
|
|
21
|
-
* // -0.75
|
|
22
|
-
*
|
|
23
|
-
* // rewritten using operator:
|
|
24
|
-
* threadFirst(
|
|
25
|
-
* 5,
|
|
26
|
-
* neg, // -5
|
|
27
|
-
* [sub, 10], // (-5) - 10
|
|
28
|
-
* [div, 20], // (-5 - 10) / 20
|
|
29
|
-
* console.log
|
|
30
|
-
* );
|
|
31
|
-
* // -0.75
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* @param init - start value
|
|
35
|
-
* @param fns - functions / S-expressions
|
|
36
|
-
*/
|
|
37
|
-
export const threadFirst = (init, ...fns) => fns.reduce((acc, expr) => typeof expr === "function"
|
|
38
|
-
? expr(acc)
|
|
39
|
-
: expr[0](acc, ...expr.slice(1)), init);
|
|
1
|
+
const threadFirst = (init, ...fns) => fns.reduce(
|
|
2
|
+
(acc, expr) => typeof expr === "function" ? expr(acc) : expr[0](acc, ...expr.slice(1)),
|
|
3
|
+
init
|
|
4
|
+
);
|
|
5
|
+
export {
|
|
6
|
+
threadFirst
|
|
7
|
+
};
|
package/thread-last.js
CHANGED
|
@@ -1,39 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
* @remarks
|
|
10
|
-
* This operator allows the code to be read more easily in the order of
|
|
11
|
-
* execution (same as the `->>` operator/macro in Clojure).
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* const neg = (x) => -x;
|
|
16
|
-
* const sub = (a, b) => a - b;
|
|
17
|
-
* const div = (a, b) => a / b;
|
|
18
|
-
*
|
|
19
|
-
* // without operator: 20 / (10 - (-5))
|
|
20
|
-
* console.log(div(20, sub(10, neg(5))));
|
|
21
|
-
* // 1.3333333333333333
|
|
22
|
-
*
|
|
23
|
-
* // rewritten using operator:
|
|
24
|
-
* threadLast(
|
|
25
|
-
* 5,
|
|
26
|
-
* neg, // -5
|
|
27
|
-
* [sub, 10], // 10 - (-5)
|
|
28
|
-
* [div, 20], // 20 / (10 - (-5))
|
|
29
|
-
* console.log
|
|
30
|
-
* );
|
|
31
|
-
* // 1.3333333333333333
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* @param init - start value
|
|
35
|
-
* @param fns - functions / S-expressions
|
|
36
|
-
*/
|
|
37
|
-
export const threadLast = (init, ...fns) => fns.reduce((acc, expr) => typeof expr === "function"
|
|
38
|
-
? expr(acc)
|
|
39
|
-
: expr[0](...expr.slice(1), acc), init);
|
|
1
|
+
const threadLast = (init, ...fns) => fns.reduce(
|
|
2
|
+
(acc, expr) => typeof expr === "function" ? expr(acc) : expr[0](...expr.slice(1), acc),
|
|
3
|
+
init
|
|
4
|
+
);
|
|
5
|
+
export {
|
|
6
|
+
threadLast
|
|
7
|
+
};
|
package/trampoline.js
CHANGED
|
@@ -1,36 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* a 1-elem array).
|
|
10
|
-
*
|
|
11
|
-
* This function should be used for non-stack consuming recursion. I.e.
|
|
12
|
-
* a trampoline is a form of continuation passing style and only ever
|
|
13
|
-
* consumes max. 2 extra stack frames, independent from recursion depth.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```ts
|
|
17
|
-
* const countdown = (acc, x) =>
|
|
18
|
-
* x >= 0 ?
|
|
19
|
-
* () => (acc.push(x), countdown(acc, x-1)) :
|
|
20
|
-
* acc;
|
|
21
|
-
*
|
|
22
|
-
* trampoline(countdown([], 4))
|
|
23
|
-
* // [ 4, 3, 2, 1, 0 ]
|
|
24
|
-
*
|
|
25
|
-
* trampoline(countdown([], -1))
|
|
26
|
-
* // []
|
|
27
|
-
* ```
|
|
28
|
-
*
|
|
29
|
-
* @param f - function
|
|
30
|
-
*/
|
|
31
|
-
export const trampoline = (f) => {
|
|
32
|
-
while (typeof f === "function") {
|
|
33
|
-
f = f();
|
|
34
|
-
}
|
|
35
|
-
return f;
|
|
1
|
+
const trampoline = (f) => {
|
|
2
|
+
while (typeof f === "function") {
|
|
3
|
+
f = f();
|
|
4
|
+
}
|
|
5
|
+
return f;
|
|
6
|
+
};
|
|
7
|
+
export {
|
|
8
|
+
trampoline
|
|
36
9
|
};
|