liminal 0.4.0 → 0.4.1
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/lib/index.js +1 -19
- package/package.json +7 -21
- package/LICENSE +0 -15
- package/README.md +0 -94
- package/jest.config.js +0 -4
- package/lib/array/array.d.ts +0 -6
- package/lib/array/array.js +0 -21
- package/lib/array/index.d.ts +0 -11
- package/lib/array/index.js +0 -17
- package/lib/composition/composition.d.ts +0 -6
- package/lib/composition/composition.js +0 -37
- package/lib/composition/index.d.ts +0 -9
- package/lib/composition/index.js +0 -13
- package/lib/composition/types.d.ts +0 -2
- package/lib/composition/types.js +0 -2
- package/lib/dispatch/getType.d.ts +0 -2
- package/lib/dispatch/getType.js +0 -15
- package/lib/dispatch/identity.d.ts +0 -2
- package/lib/dispatch/identity.js +0 -4
- package/lib/dispatch/index.d.ts +0 -22
- package/lib/dispatch/index.js +0 -13
- package/lib/dispatch/types.d.ts +0 -14
- package/lib/dispatch/types.js +0 -16
- package/lib/error/error.d.ts +0 -24
- package/lib/error/error.js +0 -25
- package/lib/error/index.d.ts +0 -2
- package/lib/error/index.js +0 -4
- package/lib/index.d.ts +0 -67
- package/lib/multimethod/index.d.ts +0 -7
- package/lib/multimethod/index.js +0 -9
- package/lib/multimethod/multimethod.d.ts +0 -3
- package/lib/multimethod/multimethod.js +0 -101
- package/src/array/array.ts +0 -29
- package/src/array/index.ts +0 -12
- package/src/composition/composition.ts +0 -45
- package/src/composition/index.ts +0 -10
- package/src/composition/types.ts +0 -6
- package/src/dispatch/getType.ts +0 -17
- package/src/dispatch/identity.ts +0 -3
- package/src/dispatch/index.ts +0 -11
- package/src/dispatch/types.ts +0 -15
- package/src/error/error.ts +0 -26
- package/src/error/index.ts +0 -3
- package/src/index.ts +0 -15
- package/src/multimethod/index.ts +0 -8
- package/src/multimethod/multimethod.test.ts +0 -10
- package/src/multimethod/multimethod.ts +0 -148
- package/tsconfig.json +0 -12
package/lib/index.js
CHANGED
|
@@ -1,19 +1 @@
|
|
|
1
|
-
"
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const array_1 = require("./array");
|
|
4
|
-
exports.array = array_1.default;
|
|
5
|
-
const composition_1 = require("./composition");
|
|
6
|
-
exports.composition = composition_1.default;
|
|
7
|
-
const dispatch_1 = require("./dispatch");
|
|
8
|
-
exports.dispatch = dispatch_1.default;
|
|
9
|
-
const error_1 = require("./error");
|
|
10
|
-
exports.error = error_1.default;
|
|
11
|
-
const multimethod_1 = require("./multimethod");
|
|
12
|
-
exports.multimethod = multimethod_1.default;
|
|
13
|
-
exports.default = {
|
|
14
|
-
array: array_1.default,
|
|
15
|
-
composition: composition_1.default,
|
|
16
|
-
dispatch: dispatch_1.default,
|
|
17
|
-
error: error_1.default,
|
|
18
|
-
multimethod: multimethod_1.default,
|
|
19
|
-
};
|
|
1
|
+
console.log("Hello world!");
|
package/package.json
CHANGED
|
@@ -1,26 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "liminal",
|
|
3
|
-
"version": "0.4.
|
|
4
|
-
"description": "
|
|
5
|
-
"repository": "
|
|
6
|
-
"homepage": "
|
|
3
|
+
"version": "0.4.1",
|
|
4
|
+
"description": "Deprecated",
|
|
5
|
+
"repository": "",
|
|
6
|
+
"homepage": "",
|
|
7
7
|
"main": "lib/index.js",
|
|
8
8
|
"license": "ISC",
|
|
9
|
-
"scripts": {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
"test": "jest"
|
|
13
|
-
},
|
|
14
|
-
"devDependencies": {
|
|
15
|
-
"@types/jest": "^24.0.18",
|
|
16
|
-
"@types/node": "^12.7.4",
|
|
17
|
-
"jest": "^24.9.0",
|
|
18
|
-
"nodemon": "^1.19.2",
|
|
19
|
-
"ts-jest": "^24.1.0",
|
|
20
|
-
"ts-node": "^8.3.0",
|
|
21
|
-
"typescript": "^3.6.3"
|
|
22
|
-
},
|
|
23
|
-
"dependencies": {
|
|
24
|
-
"deep-strict-equal": "^0.2.0"
|
|
25
|
-
}
|
|
9
|
+
"scripts": {},
|
|
10
|
+
"devDependencies": {},
|
|
11
|
+
"dependencies": {}
|
|
26
12
|
}
|
package/LICENSE
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
Copyright 2019 Maciej Cąderek
|
|
2
|
-
|
|
3
|
-
Permission to use, copy, modify, and/or distribute this software
|
|
4
|
-
for any purpose with or without fee is hereby granted,
|
|
5
|
-
provided that the above copyright notice
|
|
6
|
-
and this permission notice appear in all copies.
|
|
7
|
-
|
|
8
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
9
|
-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
|
10
|
-
OF MERCHANTABILITY AND FITNESS.
|
|
11
|
-
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
|
|
12
|
-
OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
|
|
13
|
-
DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
14
|
-
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
15
|
-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
# Liminal - functional tools for composing applications
|
|
2
|
-
|
|
3
|
-
## Notes
|
|
4
|
-
|
|
5
|
-
Projects is in a POC stage. Stay tuned!
|
|
6
|
-
|
|
7
|
-
## API
|
|
8
|
-
|
|
9
|
-
### Functional composition
|
|
10
|
-
|
|
11
|
-
#### pipe
|
|
12
|
-
|
|
13
|
-
Executes functions in provided order, starting with initial value.
|
|
14
|
-
|
|
15
|
-
```js
|
|
16
|
-
import { pipe } from 'liminal'
|
|
17
|
-
|
|
18
|
-
const doubleSquare = pipe(
|
|
19
|
-
(x) => x * 2,
|
|
20
|
-
(x) => x ** 2,
|
|
21
|
-
)
|
|
22
|
-
|
|
23
|
-
const result = doubleSquare(2) // -> 16
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
#### compose
|
|
27
|
-
|
|
28
|
-
Executes functions in reversed order, starting with initial value.
|
|
29
|
-
|
|
30
|
-
```js
|
|
31
|
-
import { compose } from 'liminal'
|
|
32
|
-
|
|
33
|
-
const doubleSquare = compose(
|
|
34
|
-
(x) => x ** 2,
|
|
35
|
-
(x) => x * 2,
|
|
36
|
-
)
|
|
37
|
-
|
|
38
|
-
const result = doubleSquare(2) // -> 16
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
#### railSync
|
|
42
|
-
|
|
43
|
-
Works similar to `pipe`, but additionally catches errors and pipes them downstream as a value.
|
|
44
|
-
|
|
45
|
-
Errors can be thrown or returned.
|
|
46
|
-
|
|
47
|
-
```js
|
|
48
|
-
import { compose } from 'liminal'
|
|
49
|
-
|
|
50
|
-
const doubleSquare = compose(
|
|
51
|
-
(x) => x ** 2,
|
|
52
|
-
(x) => x * 2,
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
const result = doubleSquare(2) // -> 16
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
```js
|
|
59
|
-
import { compose } from 'liminal'
|
|
60
|
-
|
|
61
|
-
const doubleSquare = compose(
|
|
62
|
-
(x) => {
|
|
63
|
-
if (typeof x !== 'number') {
|
|
64
|
-
return new Error('Number expected')
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return x ** 2
|
|
68
|
-
},
|
|
69
|
-
(x) => x * 2,
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
const result = doubleSquare('foo') // -> Error object
|
|
73
|
-
```
|
|
74
|
-
|
|
75
|
-
#### rail
|
|
76
|
-
|
|
77
|
-
Works similar to `rail`, but additionally waits for promises if are returned and passes result values between functions.
|
|
78
|
-
|
|
79
|
-
Errors can be thrown or returned.
|
|
80
|
-
|
|
81
|
-
```js
|
|
82
|
-
import { compose } from 'liminal'
|
|
83
|
-
|
|
84
|
-
const doubleSquare = compose(
|
|
85
|
-
(x) => x ** 2,
|
|
86
|
-
(x) => x * 2,
|
|
87
|
-
)
|
|
88
|
-
|
|
89
|
-
const main = async () => {
|
|
90
|
-
const result = await doubleSquare(Promise.resolve(2)) // -> 16
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
main()
|
|
94
|
-
```
|
package/jest.config.js
DELETED
package/lib/array/array.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export declare const filter: (fn: any) => (arr: any) => any;
|
|
2
|
-
export declare const map: (fn: any) => (arr: any) => any;
|
|
3
|
-
export declare const reduce: (fn: any, initialValue: any) => (arr: any) => any;
|
|
4
|
-
export declare const reduceRight: (fn: any, initialValue: any) => (arr: any) => any;
|
|
5
|
-
export declare const reverse: (arr: any) => any;
|
|
6
|
-
export declare const sort: (fn: any) => (arr: any) => any[];
|
package/lib/array/array.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.filter = (fn) => (arr) => arr.filter(fn);
|
|
4
|
-
exports.map = (fn) => (arr) => arr.map(fn);
|
|
5
|
-
exports.reduce = (fn, initialValue) => (arr) => arr.reduce(fn, initialValue);
|
|
6
|
-
exports.reduceRight = (fn, initialValue) => (arr) => arr.reduceRight(fn, initialValue);
|
|
7
|
-
exports.reverse = (arr) => arr.reverse();
|
|
8
|
-
exports.sort = (fn) => (arr) => {
|
|
9
|
-
if (fn) {
|
|
10
|
-
return [...arr].sort(fn);
|
|
11
|
-
}
|
|
12
|
-
if (typeof arr[0] === 'number') {
|
|
13
|
-
return [...arr].sort((a, b) => a - b);
|
|
14
|
-
}
|
|
15
|
-
if (typeof arr[0] === 'bigint') {
|
|
16
|
-
return [...arr].sort((a, b) => {
|
|
17
|
-
return 1;
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
return [...arr].sort();
|
|
21
|
-
};
|
package/lib/array/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { filter, map, reduce, reduceRight, reverse, sort } from './array';
|
|
2
|
-
export { filter, map, reduce, reduceRight, reverse, sort };
|
|
3
|
-
declare const _default: {
|
|
4
|
-
filter: (fn: any) => (arr: any) => any;
|
|
5
|
-
map: (fn: any) => (arr: any) => any;
|
|
6
|
-
reduce: (fn: any, initialValue: any) => (arr: any) => any;
|
|
7
|
-
reduceRight: (fn: any, initialValue: any) => (arr: any) => any;
|
|
8
|
-
reverse: (arr: any) => any;
|
|
9
|
-
sort: (fn: any) => (arr: any) => any[];
|
|
10
|
-
};
|
|
11
|
-
export default _default;
|
package/lib/array/index.js
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const array_1 = require("./array");
|
|
4
|
-
exports.filter = array_1.filter;
|
|
5
|
-
exports.map = array_1.map;
|
|
6
|
-
exports.reduce = array_1.reduce;
|
|
7
|
-
exports.reduceRight = array_1.reduceRight;
|
|
8
|
-
exports.reverse = array_1.reverse;
|
|
9
|
-
exports.sort = array_1.sort;
|
|
10
|
-
exports.default = {
|
|
11
|
-
filter: array_1.filter,
|
|
12
|
-
map: array_1.map,
|
|
13
|
-
reduce: array_1.reduce,
|
|
14
|
-
reduceRight: array_1.reduceRight,
|
|
15
|
-
reverse: array_1.reverse,
|
|
16
|
-
sort: array_1.sort,
|
|
17
|
-
};
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const array_1 = require("../array");
|
|
4
|
-
const wrapSync = (fn) => (input) => {
|
|
5
|
-
if (input instanceof Error) {
|
|
6
|
-
return input;
|
|
7
|
-
}
|
|
8
|
-
let result;
|
|
9
|
-
try {
|
|
10
|
-
result = fn(input);
|
|
11
|
-
}
|
|
12
|
-
catch (error) {
|
|
13
|
-
result = error;
|
|
14
|
-
}
|
|
15
|
-
return typeof result === 'undefined' ? input : result;
|
|
16
|
-
};
|
|
17
|
-
const wrap = (fn) => (input) => {
|
|
18
|
-
if (input instanceof Promise) {
|
|
19
|
-
return input.then((rawInput) => {
|
|
20
|
-
return wrapSync(fn)(rawInput);
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
return wrapSync(fn)(input);
|
|
24
|
-
};
|
|
25
|
-
const chain = (reducingFn, wrappingFn = null) => (...fns) => (initialArg) => {
|
|
26
|
-
return wrappingFn
|
|
27
|
-
? reducingFn((arg, fn) => wrappingFn(fn)(arg), initialArg)(fns)
|
|
28
|
-
: reducingFn((arg, fn) => fn(arg), initialArg)(fns);
|
|
29
|
-
};
|
|
30
|
-
const compose = chain(array_1.reduceRight);
|
|
31
|
-
exports.compose = compose;
|
|
32
|
-
const pipe = chain(array_1.reduce);
|
|
33
|
-
exports.pipe = pipe;
|
|
34
|
-
const rail = chain(array_1.reduce, wrap);
|
|
35
|
-
exports.rail = rail;
|
|
36
|
-
const railSync = chain(array_1.reduce, wrapSync);
|
|
37
|
-
exports.railSync = railSync;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { compose, pipe, rail, railSync } from './composition';
|
|
2
|
-
export { compose, pipe, rail, railSync };
|
|
3
|
-
declare const _default: {
|
|
4
|
-
compose: import("./types").ChainFunctions;
|
|
5
|
-
pipe: import("./types").ChainFunctions;
|
|
6
|
-
rail: import("./types").ChainFunctions;
|
|
7
|
-
railSync: import("./types").ChainFunctions;
|
|
8
|
-
};
|
|
9
|
-
export default _default;
|
package/lib/composition/index.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const composition_1 = require("./composition");
|
|
4
|
-
exports.compose = composition_1.compose;
|
|
5
|
-
exports.pipe = composition_1.pipe;
|
|
6
|
-
exports.rail = composition_1.rail;
|
|
7
|
-
exports.railSync = composition_1.railSync;
|
|
8
|
-
exports.default = {
|
|
9
|
-
compose: composition_1.compose,
|
|
10
|
-
pipe: composition_1.pipe,
|
|
11
|
-
rail: composition_1.rail,
|
|
12
|
-
railSync: composition_1.railSync,
|
|
13
|
-
};
|
package/lib/composition/types.js
DELETED
package/lib/dispatch/getType.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const getType = (val) => {
|
|
4
|
-
if (Array.isArray(val)) {
|
|
5
|
-
return 'array';
|
|
6
|
-
}
|
|
7
|
-
if (val === null) {
|
|
8
|
-
return 'null';
|
|
9
|
-
}
|
|
10
|
-
if (val instanceof RegExp) {
|
|
11
|
-
return 'regexp';
|
|
12
|
-
}
|
|
13
|
-
return typeof val;
|
|
14
|
-
};
|
|
15
|
-
exports.default = getType;
|
package/lib/dispatch/identity.js
DELETED
package/lib/dispatch/index.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import getType from './getType';
|
|
2
|
-
import identity from './identity';
|
|
3
|
-
import types from './types';
|
|
4
|
-
export { getType, identity, types };
|
|
5
|
-
declare const _default: {
|
|
6
|
-
getType: (val: any) => "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "array" | "null" | "regexp";
|
|
7
|
-
identity: (x: any) => any;
|
|
8
|
-
types: {
|
|
9
|
-
array: string;
|
|
10
|
-
object: string;
|
|
11
|
-
function: string;
|
|
12
|
-
string: string;
|
|
13
|
-
number: string;
|
|
14
|
-
bigint: string;
|
|
15
|
-
symbol: string;
|
|
16
|
-
regexp: string;
|
|
17
|
-
null: string;
|
|
18
|
-
undefined: string;
|
|
19
|
-
boolean: string;
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
export default _default;
|
package/lib/dispatch/index.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const getType_1 = require("./getType");
|
|
4
|
-
exports.getType = getType_1.default;
|
|
5
|
-
const identity_1 = require("./identity");
|
|
6
|
-
exports.identity = identity_1.default;
|
|
7
|
-
const types_1 = require("./types");
|
|
8
|
-
exports.types = types_1.default;
|
|
9
|
-
exports.default = {
|
|
10
|
-
getType: getType_1.default,
|
|
11
|
-
identity: identity_1.default,
|
|
12
|
-
types: types_1.default,
|
|
13
|
-
};
|
package/lib/dispatch/types.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
declare const types: {
|
|
2
|
-
array: string;
|
|
3
|
-
object: string;
|
|
4
|
-
function: string;
|
|
5
|
-
string: string;
|
|
6
|
-
number: string;
|
|
7
|
-
bigint: string;
|
|
8
|
-
symbol: string;
|
|
9
|
-
regexp: string;
|
|
10
|
-
null: string;
|
|
11
|
-
undefined: string;
|
|
12
|
-
boolean: string;
|
|
13
|
-
};
|
|
14
|
-
export default types;
|
package/lib/dispatch/types.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const types = {
|
|
4
|
-
array: 'array',
|
|
5
|
-
object: 'object',
|
|
6
|
-
function: 'function',
|
|
7
|
-
string: 'string',
|
|
8
|
-
number: 'number',
|
|
9
|
-
bigint: 'bigint',
|
|
10
|
-
symbol: 'symbol',
|
|
11
|
-
regexp: 'regexp',
|
|
12
|
-
null: 'null',
|
|
13
|
-
undefined: 'undefined',
|
|
14
|
-
boolean: 'boolean',
|
|
15
|
-
};
|
|
16
|
-
exports.default = types;
|
package/lib/error/error.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
declare const _default: {
|
|
3
|
-
create: ({ name, defaultMessage }: {
|
|
4
|
-
name: any;
|
|
5
|
-
defaultMessage?: string;
|
|
6
|
-
}) => {
|
|
7
|
-
new (message?: string): {
|
|
8
|
-
name: string;
|
|
9
|
-
message: string;
|
|
10
|
-
stack?: string;
|
|
11
|
-
};
|
|
12
|
-
captureStackTrace(targetObject: Object, constructorOpt?: Function): void;
|
|
13
|
-
prepareStackTrace?: (err: Error, stackTraces: NodeJS.CallSite[]) => any;
|
|
14
|
-
stackTraceLimit: number;
|
|
15
|
-
};
|
|
16
|
-
toJSON: (error: any) => {
|
|
17
|
-
error: {
|
|
18
|
-
name: any;
|
|
19
|
-
message: any;
|
|
20
|
-
stacktrace: any;
|
|
21
|
-
};
|
|
22
|
-
};
|
|
23
|
-
};
|
|
24
|
-
export default _default;
|
package/lib/error/error.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const create = ({ name, defaultMessage = '' }) => {
|
|
4
|
-
const err = class extends Error {
|
|
5
|
-
constructor(message = defaultMessage) {
|
|
6
|
-
super(message);
|
|
7
|
-
this.name = name;
|
|
8
|
-
this.message = message;
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
return err;
|
|
12
|
-
};
|
|
13
|
-
const toJSON = (error) => {
|
|
14
|
-
return {
|
|
15
|
-
error: {
|
|
16
|
-
name: error.name,
|
|
17
|
-
message: error.message,
|
|
18
|
-
stacktrace: error.stack,
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
};
|
|
22
|
-
exports.default = {
|
|
23
|
-
create,
|
|
24
|
-
toJSON,
|
|
25
|
-
};
|
package/lib/error/index.d.ts
DELETED
package/lib/error/index.js
DELETED
package/lib/index.d.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import array from './array';
|
|
3
|
-
import composition from './composition';
|
|
4
|
-
import dispatch from './dispatch';
|
|
5
|
-
import error from './error';
|
|
6
|
-
import multimethod from './multimethod';
|
|
7
|
-
export { array, composition, dispatch, error, multimethod };
|
|
8
|
-
declare const _default: {
|
|
9
|
-
array: {
|
|
10
|
-
filter: (fn: any) => (arr: any) => any;
|
|
11
|
-
map: (fn: any) => (arr: any) => any;
|
|
12
|
-
reduce: (fn: any, initialValue: any) => (arr: any) => any;
|
|
13
|
-
reduceRight: (fn: any, initialValue: any) => (arr: any) => any;
|
|
14
|
-
reverse: (arr: any) => any;
|
|
15
|
-
sort: (fn: any) => (arr: any) => any[];
|
|
16
|
-
};
|
|
17
|
-
composition: {
|
|
18
|
-
compose: import("./composition/types").ChainFunctions;
|
|
19
|
-
pipe: import("./composition/types").ChainFunctions;
|
|
20
|
-
rail: import("./composition/types").ChainFunctions;
|
|
21
|
-
railSync: import("./composition/types").ChainFunctions;
|
|
22
|
-
};
|
|
23
|
-
dispatch: {
|
|
24
|
-
getType: (val: any) => "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "array" | "null" | "regexp";
|
|
25
|
-
identity: (x: any) => any;
|
|
26
|
-
types: {
|
|
27
|
-
array: string;
|
|
28
|
-
object: string;
|
|
29
|
-
function: string;
|
|
30
|
-
string: string;
|
|
31
|
-
number: string;
|
|
32
|
-
bigint: string;
|
|
33
|
-
symbol: string;
|
|
34
|
-
regexp: string;
|
|
35
|
-
null: string;
|
|
36
|
-
undefined: string;
|
|
37
|
-
boolean: string;
|
|
38
|
-
};
|
|
39
|
-
};
|
|
40
|
-
error: {
|
|
41
|
-
create: ({ name, defaultMessage }: {
|
|
42
|
-
name: any;
|
|
43
|
-
defaultMessage?: string;
|
|
44
|
-
}) => {
|
|
45
|
-
new (message?: string): {
|
|
46
|
-
name: string;
|
|
47
|
-
message: string;
|
|
48
|
-
stack?: string;
|
|
49
|
-
};
|
|
50
|
-
captureStackTrace(targetObject: Object, constructorOpt?: Function): void;
|
|
51
|
-
prepareStackTrace?: (err: Error, stackTraces: NodeJS.CallSite[]) => any;
|
|
52
|
-
stackTraceLimit: number;
|
|
53
|
-
};
|
|
54
|
-
toJSON: (error: any) => {
|
|
55
|
-
error: {
|
|
56
|
-
name: any;
|
|
57
|
-
message: any;
|
|
58
|
-
stacktrace: any;
|
|
59
|
-
};
|
|
60
|
-
};
|
|
61
|
-
};
|
|
62
|
-
multimethod: {
|
|
63
|
-
multi: (dispatch: any, ...methods: any[]) => any;
|
|
64
|
-
method: (...args: any[]) => (multimethod: any) => any;
|
|
65
|
-
};
|
|
66
|
-
};
|
|
67
|
-
export default _default;
|
package/lib/multimethod/index.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const multimethod_1 = require("./multimethod");
|
|
4
|
-
exports.multi = multimethod_1.multi;
|
|
5
|
-
exports.method = multimethod_1.method;
|
|
6
|
-
exports.default = {
|
|
7
|
-
multi: multimethod_1.multi,
|
|
8
|
-
method: multimethod_1.method,
|
|
9
|
-
};
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const equal = require("deep-strict-equal");
|
|
4
|
-
const composition_1 = require("../composition");
|
|
5
|
-
const multimethodKey = Symbol ? Symbol('multimethod') : 'multimethod';
|
|
6
|
-
const countSegments = (dispatch) => {
|
|
7
|
-
let count = 1;
|
|
8
|
-
let current = dispatch;
|
|
9
|
-
try {
|
|
10
|
-
while (typeof current === 'function') {
|
|
11
|
-
const next = current();
|
|
12
|
-
if (typeof next === 'function') {
|
|
13
|
-
count++;
|
|
14
|
-
current = next;
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
return count;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
catch (_a) { }
|
|
22
|
-
return count;
|
|
23
|
-
};
|
|
24
|
-
const createSimpleTarget = (methodEntries, defaultMethod, dispatch) => {
|
|
25
|
-
return (...args) => {
|
|
26
|
-
let currentDispatchValue = dispatch(...args);
|
|
27
|
-
const entry = methodEntries.find(([dispatchValue]) => equal(dispatchValue, currentDispatchValue));
|
|
28
|
-
const target = entry ? entry[1] : defaultMethod;
|
|
29
|
-
if (!entry && target === null) {
|
|
30
|
-
throw new Error('No method specified for provided arguments');
|
|
31
|
-
}
|
|
32
|
-
if (typeof target !== 'function') {
|
|
33
|
-
return target;
|
|
34
|
-
}
|
|
35
|
-
return target(...args);
|
|
36
|
-
};
|
|
37
|
-
};
|
|
38
|
-
const createSegmentedTarget = (methodEntries, defaultMethod, dispatch, segmentsCount) => {
|
|
39
|
-
const recur = (counter, previousSegmentsArgs = []) => {
|
|
40
|
-
if (counter === 1) {
|
|
41
|
-
return (...args) => {
|
|
42
|
-
const segmentsArgs = [...previousSegmentsArgs, args];
|
|
43
|
-
let count = segmentsArgs.length;
|
|
44
|
-
let currentDispatchValue = dispatch;
|
|
45
|
-
for (let i = 0; i < count; i++) {
|
|
46
|
-
currentDispatchValue = currentDispatchValue(...segmentsArgs[i]);
|
|
47
|
-
}
|
|
48
|
-
const entry = methodEntries.find(([dispatchValue]) => equal(dispatchValue, currentDispatchValue));
|
|
49
|
-
const target = entry ? entry[1] : defaultMethod;
|
|
50
|
-
if (!entry && target === null) {
|
|
51
|
-
throw new Error('No method specified for provided arguments');
|
|
52
|
-
}
|
|
53
|
-
if (typeof target !== 'function') {
|
|
54
|
-
return target;
|
|
55
|
-
}
|
|
56
|
-
let result = target;
|
|
57
|
-
for (let i = 0; i < count; i++) {
|
|
58
|
-
result = result(...segmentsArgs[i]);
|
|
59
|
-
}
|
|
60
|
-
return result;
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
return (...args) => {
|
|
64
|
-
return recur(counter - 1, [...previousSegmentsArgs, args]);
|
|
65
|
-
};
|
|
66
|
-
};
|
|
67
|
-
return recur(segmentsCount);
|
|
68
|
-
};
|
|
69
|
-
const createMultimethod = (methodEntries = []) => (defaultMethod = null) => (dispatch, ...methods) => {
|
|
70
|
-
const segmentsCount = countSegments(dispatch);
|
|
71
|
-
const resultFn = segmentsCount === 1
|
|
72
|
-
? createSimpleTarget(methodEntries, defaultMethod, dispatch)
|
|
73
|
-
: createSegmentedTarget(methodEntries, defaultMethod, dispatch, segmentsCount);
|
|
74
|
-
resultFn[multimethodKey] = {
|
|
75
|
-
methodEntries,
|
|
76
|
-
defaultMethod,
|
|
77
|
-
dispatch,
|
|
78
|
-
};
|
|
79
|
-
if (methods.length !== 0) {
|
|
80
|
-
return composition_1.pipe(...methods)(resultFn);
|
|
81
|
-
}
|
|
82
|
-
return resultFn;
|
|
83
|
-
};
|
|
84
|
-
const multi = createMultimethod()();
|
|
85
|
-
exports.multi = multi;
|
|
86
|
-
const method = (...args) => (multimethod) => {
|
|
87
|
-
if (!multimethod[multimethodKey]) {
|
|
88
|
-
throw new Error('Function is not a multimethod');
|
|
89
|
-
}
|
|
90
|
-
const [first, second] = args;
|
|
91
|
-
const isNotDefault = second !== undefined;
|
|
92
|
-
const fn = isNotDefault ? second : first;
|
|
93
|
-
const dispatchValues = isNotDefault ? first : null;
|
|
94
|
-
const { methodEntries, defaultMethod, dispatch } = multimethod[multimethodKey];
|
|
95
|
-
if (isNotDefault) {
|
|
96
|
-
const newMethodEntries = [[dispatchValues, fn], ...methodEntries];
|
|
97
|
-
return createMultimethod(newMethodEntries)(defaultMethod)(dispatch);
|
|
98
|
-
}
|
|
99
|
-
return createMultimethod(methodEntries)(fn)(dispatch);
|
|
100
|
-
};
|
|
101
|
-
exports.method = method;
|
package/src/array/array.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
export const filter = (fn) => (arr) => arr.filter(fn)
|
|
2
|
-
|
|
3
|
-
export const map = (fn) => (arr) => arr.map(fn)
|
|
4
|
-
|
|
5
|
-
export const reduce = (fn, initialValue) => (arr) =>
|
|
6
|
-
arr.reduce(fn, initialValue)
|
|
7
|
-
|
|
8
|
-
export const reduceRight = (fn, initialValue) => (arr) =>
|
|
9
|
-
arr.reduceRight(fn, initialValue)
|
|
10
|
-
|
|
11
|
-
export const reverse = (arr) => arr.reverse()
|
|
12
|
-
|
|
13
|
-
export const sort = (fn) => (arr) => {
|
|
14
|
-
if (fn) {
|
|
15
|
-
return [...arr].sort(fn)
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (typeof arr[0] === 'number') {
|
|
19
|
-
return [...arr].sort((a, b) => a - b)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (typeof arr[0] === 'bigint') {
|
|
23
|
-
return [...arr].sort((a, b) => {
|
|
24
|
-
return 1
|
|
25
|
-
})
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return [...arr].sort()
|
|
29
|
-
}
|
package/src/array/index.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { reduce, reduceRight } from '../array'
|
|
2
|
-
import { ChainFactory, ChainFunctions } from './types'
|
|
3
|
-
|
|
4
|
-
const wrapSync = (fn) => (input) => {
|
|
5
|
-
if (input instanceof Error) {
|
|
6
|
-
return input
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
let result
|
|
10
|
-
|
|
11
|
-
try {
|
|
12
|
-
result = fn(input)
|
|
13
|
-
} catch (error) {
|
|
14
|
-
result = error
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return typeof result === 'undefined' ? input : result
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const wrap = (fn) => (input) => {
|
|
21
|
-
if (input instanceof Promise) {
|
|
22
|
-
return input.then((rawInput) => {
|
|
23
|
-
return wrapSync(fn)(rawInput)
|
|
24
|
-
})
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return wrapSync(fn)(input)
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const chain: ChainFactory = (reducingFn, wrappingFn = null) => (...fns) => (
|
|
31
|
-
initialArg,
|
|
32
|
-
) => {
|
|
33
|
-
return wrappingFn
|
|
34
|
-
? reducingFn((arg: any, fn: Function) => wrappingFn(fn)(arg), initialArg)(
|
|
35
|
-
fns,
|
|
36
|
-
)
|
|
37
|
-
: reducingFn((arg: any, fn: Function) => fn(arg), initialArg)(fns)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const compose: ChainFunctions = chain(reduceRight)
|
|
41
|
-
const pipe: ChainFunctions = chain(reduce)
|
|
42
|
-
const rail: ChainFunctions = chain(reduce, wrap)
|
|
43
|
-
const railSync: ChainFunctions = chain(reduce, wrapSync)
|
|
44
|
-
|
|
45
|
-
export { compose, pipe, rail, railSync }
|
package/src/composition/index.ts
DELETED
package/src/composition/types.ts
DELETED
package/src/dispatch/getType.ts
DELETED
package/src/dispatch/identity.ts
DELETED
package/src/dispatch/index.ts
DELETED
package/src/dispatch/types.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
const types = {
|
|
2
|
-
array: 'array',
|
|
3
|
-
object: 'object',
|
|
4
|
-
function: 'function',
|
|
5
|
-
string: 'string',
|
|
6
|
-
number: 'number',
|
|
7
|
-
bigint: 'bigint',
|
|
8
|
-
symbol: 'symbol',
|
|
9
|
-
regexp: 'regexp',
|
|
10
|
-
null: 'null',
|
|
11
|
-
undefined: 'undefined',
|
|
12
|
-
boolean: 'boolean',
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export default types
|
package/src/error/error.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
const create = ({ name, defaultMessage = '' }) => {
|
|
2
|
-
const err = class extends Error {
|
|
3
|
-
constructor(message = defaultMessage) {
|
|
4
|
-
super(message)
|
|
5
|
-
this.name = name
|
|
6
|
-
this.message = message
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
return err
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const toJSON = (error) => {
|
|
14
|
-
return {
|
|
15
|
-
error: {
|
|
16
|
-
name: error.name,
|
|
17
|
-
message: error.message,
|
|
18
|
-
stacktrace: error.stack,
|
|
19
|
-
},
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export default {
|
|
24
|
-
create,
|
|
25
|
-
toJSON,
|
|
26
|
-
}
|
package/src/error/index.ts
DELETED
package/src/index.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import array from './array'
|
|
2
|
-
import composition from './composition'
|
|
3
|
-
import dispatch from './dispatch'
|
|
4
|
-
import error from './error'
|
|
5
|
-
import multimethod from './multimethod'
|
|
6
|
-
|
|
7
|
-
export { array, composition, dispatch, error, multimethod }
|
|
8
|
-
|
|
9
|
-
export default {
|
|
10
|
-
array,
|
|
11
|
-
composition,
|
|
12
|
-
dispatch,
|
|
13
|
-
error,
|
|
14
|
-
multimethod,
|
|
15
|
-
}
|
package/src/multimethod/index.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { multi } from './multimethod'
|
|
2
|
-
|
|
3
|
-
describe('multi', () => {
|
|
4
|
-
describe('executed with dispatch function only', () => {
|
|
5
|
-
it('creates multimethod that throws error: no matching method', () => {
|
|
6
|
-
const fn = multi(() => true)
|
|
7
|
-
expect(fn).toThrowError('No method specified for provided arguments')
|
|
8
|
-
})
|
|
9
|
-
})
|
|
10
|
-
})
|
|
@@ -1,148 +0,0 @@
|
|
|
1
|
-
import * as equal from 'deep-strict-equal'
|
|
2
|
-
import { pipe } from '../composition'
|
|
3
|
-
|
|
4
|
-
const multimethodKey = Symbol ? Symbol('multimethod') : 'multimethod'
|
|
5
|
-
|
|
6
|
-
const countSegments = (dispatch) => {
|
|
7
|
-
let count = 1
|
|
8
|
-
let current = dispatch
|
|
9
|
-
|
|
10
|
-
try {
|
|
11
|
-
while (typeof current === 'function') {
|
|
12
|
-
const next = current()
|
|
13
|
-
if (typeof next === 'function') {
|
|
14
|
-
count++
|
|
15
|
-
current = next
|
|
16
|
-
} else {
|
|
17
|
-
return count
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
} catch {}
|
|
21
|
-
|
|
22
|
-
return count
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const createSimpleTarget = (methodEntries, defaultMethod, dispatch) => {
|
|
26
|
-
return (...args) => {
|
|
27
|
-
let currentDispatchValue = dispatch(...args)
|
|
28
|
-
|
|
29
|
-
const entry = methodEntries.find(([dispatchValue]) =>
|
|
30
|
-
equal(dispatchValue, currentDispatchValue),
|
|
31
|
-
)
|
|
32
|
-
|
|
33
|
-
const target = entry ? entry[1] : defaultMethod
|
|
34
|
-
|
|
35
|
-
if (!entry && target === null) {
|
|
36
|
-
throw new Error('No method specified for provided arguments')
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (typeof target !== 'function') {
|
|
40
|
-
return target
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return target(...args)
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const createSegmentedTarget = (
|
|
48
|
-
methodEntries,
|
|
49
|
-
defaultMethod,
|
|
50
|
-
dispatch,
|
|
51
|
-
segmentsCount,
|
|
52
|
-
) => {
|
|
53
|
-
const recur = (counter, previousSegmentsArgs = []) => {
|
|
54
|
-
if (counter === 1) {
|
|
55
|
-
return (...args) => {
|
|
56
|
-
const segmentsArgs = [...previousSegmentsArgs, args]
|
|
57
|
-
let count = segmentsArgs.length
|
|
58
|
-
|
|
59
|
-
let currentDispatchValue = dispatch
|
|
60
|
-
|
|
61
|
-
for (let i = 0; i < count; i++) {
|
|
62
|
-
currentDispatchValue = currentDispatchValue(...segmentsArgs[i])
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const entry = methodEntries.find(([dispatchValue]) =>
|
|
66
|
-
equal(dispatchValue, currentDispatchValue),
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
const target = entry ? entry[1] : defaultMethod
|
|
70
|
-
|
|
71
|
-
if (!entry && target === null) {
|
|
72
|
-
throw new Error('No method specified for provided arguments')
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (typeof target !== 'function') {
|
|
76
|
-
return target
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
let result: any = target
|
|
80
|
-
|
|
81
|
-
for (let i = 0; i < count; i++) {
|
|
82
|
-
result = result(...segmentsArgs[i])
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return result
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return (...args) => {
|
|
90
|
-
return recur(counter - 1, [...previousSegmentsArgs, args])
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return recur(segmentsCount)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const createMultimethod = (methodEntries = []) => (defaultMethod = null) => (
|
|
98
|
-
dispatch,
|
|
99
|
-
...methods
|
|
100
|
-
) => {
|
|
101
|
-
const segmentsCount = countSegments(dispatch)
|
|
102
|
-
|
|
103
|
-
const resultFn =
|
|
104
|
-
segmentsCount === 1
|
|
105
|
-
? createSimpleTarget(methodEntries, defaultMethod, dispatch)
|
|
106
|
-
: createSegmentedTarget(
|
|
107
|
-
methodEntries,
|
|
108
|
-
defaultMethod,
|
|
109
|
-
dispatch,
|
|
110
|
-
segmentsCount,
|
|
111
|
-
)
|
|
112
|
-
|
|
113
|
-
resultFn[multimethodKey] = {
|
|
114
|
-
methodEntries,
|
|
115
|
-
defaultMethod,
|
|
116
|
-
dispatch,
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
if (methods.length !== 0) {
|
|
120
|
-
return pipe(...methods)(resultFn)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return resultFn
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
const multi = createMultimethod()()
|
|
127
|
-
|
|
128
|
-
const method = (...args) => (multimethod) => {
|
|
129
|
-
if (!multimethod[multimethodKey]) {
|
|
130
|
-
throw new Error('Function is not a multimethod')
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const [first, second] = args
|
|
134
|
-
const isNotDefault = second !== undefined
|
|
135
|
-
const fn = isNotDefault ? second : first
|
|
136
|
-
const dispatchValues = isNotDefault ? first : null
|
|
137
|
-
|
|
138
|
-
const { methodEntries, defaultMethod, dispatch } = multimethod[multimethodKey]
|
|
139
|
-
|
|
140
|
-
if (isNotDefault) {
|
|
141
|
-
const newMethodEntries = [[dispatchValues, fn], ...methodEntries]
|
|
142
|
-
return createMultimethod(newMethodEntries)(defaultMethod)(dispatch)
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return createMultimethod(methodEntries)(fn)(dispatch)
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
export { multi, method }
|
package/tsconfig.json
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "es2015",
|
|
4
|
-
"module": "commonjs",
|
|
5
|
-
"removeComments": true,
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"outDir": "./lib",
|
|
8
|
-
"preserveConstEnums": true
|
|
9
|
-
},
|
|
10
|
-
"include": ["src"],
|
|
11
|
-
"exclude": ["node_modules", "src/**/*.test.ts", "src/**/*.temp.ts"]
|
|
12
|
-
}
|