liminal 0.3.4 → 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.
Files changed (45) hide show
  1. package/lib/index.js +1 -19
  2. package/package.json +7 -21
  3. package/LICENSE +0 -15
  4. package/README.md +0 -94
  5. package/jest.config.js +0 -4
  6. package/lib/array/array.d.ts +0 -6
  7. package/lib/array/array.js +0 -21
  8. package/lib/array/index.d.ts +0 -11
  9. package/lib/array/index.js +0 -17
  10. package/lib/composition/composition.d.ts +0 -6
  11. package/lib/composition/composition.js +0 -37
  12. package/lib/composition/index.d.ts +0 -9
  13. package/lib/composition/index.js +0 -13
  14. package/lib/composition/types.d.ts +0 -2
  15. package/lib/composition/types.js +0 -2
  16. package/lib/dispatch/getType.d.ts +0 -2
  17. package/lib/dispatch/getType.js +0 -15
  18. package/lib/dispatch/index.d.ts +0 -20
  19. package/lib/dispatch/index.js +0 -10
  20. package/lib/dispatch/types.d.ts +0 -14
  21. package/lib/dispatch/types.js +0 -16
  22. package/lib/error/error.d.ts +0 -24
  23. package/lib/error/error.js +0 -25
  24. package/lib/error/index.d.ts +0 -2
  25. package/lib/error/index.js +0 -4
  26. package/lib/index.d.ts +0 -66
  27. package/lib/multimethod/index.d.ts +0 -7
  28. package/lib/multimethod/index.js +0 -9
  29. package/lib/multimethod/multimethod.d.ts +0 -3
  30. package/lib/multimethod/multimethod.js +0 -85
  31. package/src/array/array.ts +0 -29
  32. package/src/array/index.ts +0 -12
  33. package/src/composition/composition.ts +0 -45
  34. package/src/composition/index.ts +0 -10
  35. package/src/composition/types.ts +0 -6
  36. package/src/dispatch/getType.ts +0 -17
  37. package/src/dispatch/index.ts +0 -9
  38. package/src/dispatch/types.ts +0 -15
  39. package/src/error/error.ts +0 -26
  40. package/src/error/index.ts +0 -3
  41. package/src/index.ts +0 -15
  42. package/src/multimethod/index.ts +0 -8
  43. package/src/multimethod/multimethod.test.ts +0 -10
  44. package/src/multimethod/multimethod.ts +0 -112
  45. package/tsconfig.json +0 -12
package/lib/index.js CHANGED
@@ -1,19 +1 @@
1
- "use strict";
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.3.4",
4
- "description": "Functional tools for composing applications",
5
- "repository": "https://github.com/caderek/liminal",
6
- "homepage": "https://github.com/caderek/liminal",
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
- "start": "nodemon --exec ts-node src/index.ts",
11
- "build": "tsc",
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
@@ -1,4 +0,0 @@
1
- module.exports = {
2
- preset: 'ts-jest',
3
- testEnvironment: 'node',
4
- }
@@ -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[];
@@ -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
- };
@@ -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;
@@ -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,6 +0,0 @@
1
- import { ChainFunctions } from './types';
2
- declare const compose: ChainFunctions;
3
- declare const pipe: ChainFunctions;
4
- declare const rail: ChainFunctions;
5
- declare const railSync: ChainFunctions;
6
- export { compose, pipe, rail, railSync };
@@ -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;
@@ -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
- };
@@ -1,2 +0,0 @@
1
- export declare type ChainFunctions = (...fns: Function[]) => (initialArg: any) => any;
2
- export declare type ChainFactory = (reduceFn: Function, wrappingFn?: Function) => ChainFunctions;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,2 +0,0 @@
1
- declare const getType: (val: any) => "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "array" | "null" | "regexp";
2
- export default getType;
@@ -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;
@@ -1,20 +0,0 @@
1
- import getType from './getType';
2
- import types from './types';
3
- export { getType, types };
4
- declare const _default: {
5
- getType: (val: any) => "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "array" | "null" | "regexp";
6
- types: {
7
- array: string;
8
- object: string;
9
- function: string;
10
- string: string;
11
- number: string;
12
- bigint: string;
13
- symbol: string;
14
- regexp: string;
15
- null: string;
16
- undefined: string;
17
- boolean: string;
18
- };
19
- };
20
- export default _default;
@@ -1,10 +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 types_1 = require("./types");
6
- exports.types = types_1.default;
7
- exports.default = {
8
- getType: getType_1.default,
9
- types: types_1.default,
10
- };
@@ -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;
@@ -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;
@@ -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;
@@ -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
- };
@@ -1,2 +0,0 @@
1
- import error from './error';
2
- export default error;
@@ -1,4 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const error_1 = require("./error");
4
- exports.default = error_1.default;
package/lib/index.d.ts DELETED
@@ -1,66 +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
- types: {
26
- array: string;
27
- object: string;
28
- function: string;
29
- string: string;
30
- number: string;
31
- bigint: string;
32
- symbol: string;
33
- regexp: string;
34
- null: string;
35
- undefined: string;
36
- boolean: string;
37
- };
38
- };
39
- error: {
40
- create: ({ name, defaultMessage }: {
41
- name: any;
42
- defaultMessage?: string;
43
- }) => {
44
- new (message?: string): {
45
- name: string;
46
- message: string;
47
- stack?: string;
48
- };
49
- captureStackTrace(targetObject: Object, constructorOpt?: Function): void;
50
- prepareStackTrace?: (err: Error, stackTraces: NodeJS.CallSite[]) => any;
51
- stackTraceLimit: number;
52
- };
53
- toJSON: (error: any) => {
54
- error: {
55
- name: any;
56
- message: any;
57
- stacktrace: any;
58
- };
59
- };
60
- };
61
- multimethod: {
62
- multi: (dispatch: any, ...methods: any[]) => any;
63
- method: (...args: any[]) => (multimethod: any) => any;
64
- };
65
- };
66
- export default _default;
@@ -1,7 +0,0 @@
1
- import { multi, method } from './multimethod';
2
- export { multi, method };
3
- declare const _default: {
4
- multi: (dispatch: any, ...methods: any[]) => any;
5
- method: (...args: any[]) => (multimethod: any) => any;
6
- };
7
- export default _default;
@@ -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,3 +0,0 @@
1
- declare const multi: (dispatch: any, ...methods: any[]) => any;
2
- declare const method: (...args: any[]) => (multimethod: any) => any;
3
- export { multi, method };
@@ -1,85 +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 createMultimethod = (methodEntries = []) => (defaultMethod = null) => (dispatch, ...methods) => {
25
- const segmentsCount = countSegments(dispatch);
26
- const createSegmentedFn = (segmentsCount, previousSegmentsArgs = []) => {
27
- if (segmentsCount === 1) {
28
- return (...args) => {
29
- const segmentsArgs = [...previousSegmentsArgs, args];
30
- let count = segmentsArgs.length;
31
- let currentDispatchValue = dispatch;
32
- for (let i = 0; i < count; i++) {
33
- currentDispatchValue = currentDispatchValue(...segmentsArgs[i]);
34
- }
35
- const entry = methodEntries.find(([dispatchValue]) => equal(dispatchValue, currentDispatchValue));
36
- const target = entry ? entry[1] : defaultMethod;
37
- if (!entry && target === null) {
38
- throw new Error('No method specified for provided arguments');
39
- }
40
- if (typeof target !== 'function') {
41
- return target;
42
- }
43
- let result = target;
44
- for (let i = 0; i < count; i++) {
45
- result = result(...segmentsArgs[i]);
46
- }
47
- return result;
48
- };
49
- }
50
- return (...args) => {
51
- return createSegmentedFn(segmentsCount - 1, [
52
- ...previousSegmentsArgs,
53
- args,
54
- ]);
55
- };
56
- };
57
- const resultFn = createSegmentedFn(segmentsCount);
58
- resultFn[multimethodKey] = {
59
- methodEntries,
60
- defaultMethod,
61
- dispatch,
62
- };
63
- if (methods.length !== 0) {
64
- return composition_1.pipe(...methods)(resultFn);
65
- }
66
- return resultFn;
67
- };
68
- const multi = createMultimethod()();
69
- exports.multi = multi;
70
- const method = (...args) => (multimethod) => {
71
- if (!multimethod[multimethodKey]) {
72
- throw new Error('Function is not a multimethod');
73
- }
74
- const [first, second] = args;
75
- const isNotDefault = second !== undefined;
76
- const fn = isNotDefault ? second : first;
77
- const dispatchValues = isNotDefault ? first : null;
78
- const { methodEntries, defaultMethod, dispatch } = multimethod[multimethodKey];
79
- if (isNotDefault) {
80
- const newMethodEntries = [[dispatchValues, fn], ...methodEntries];
81
- return createMultimethod(newMethodEntries)(defaultMethod)(dispatch);
82
- }
83
- return createMultimethod(methodEntries)(fn)(dispatch);
84
- };
85
- exports.method = method;
@@ -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
- }
@@ -1,12 +0,0 @@
1
- import { filter, map, reduce, reduceRight, reverse, sort } from './array'
2
-
3
- export { filter, map, reduce, reduceRight, reverse, sort }
4
-
5
- export default {
6
- filter,
7
- map,
8
- reduce,
9
- reduceRight,
10
- reverse,
11
- sort,
12
- }
@@ -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 }
@@ -1,10 +0,0 @@
1
- import { compose, pipe, rail, railSync } from './composition'
2
-
3
- export { compose, pipe, rail, railSync }
4
-
5
- export default {
6
- compose,
7
- pipe,
8
- rail,
9
- railSync,
10
- }
@@ -1,6 +0,0 @@
1
- export type ChainFunctions = (...fns: Function[]) => (initialArg: any) => any
2
-
3
- export type ChainFactory = (
4
- reduceFn: Function,
5
- wrappingFn?: Function,
6
- ) => ChainFunctions
@@ -1,17 +0,0 @@
1
- const getType = (val) => {
2
- if (Array.isArray(val)) {
3
- return 'array'
4
- }
5
-
6
- if (val === null) {
7
- return 'null'
8
- }
9
-
10
- if (val instanceof RegExp) {
11
- return 'regexp'
12
- }
13
-
14
- return typeof val
15
- }
16
-
17
- export default getType
@@ -1,9 +0,0 @@
1
- import getType from './getType'
2
- import types from './types'
3
-
4
- export { getType, types }
5
-
6
- export default {
7
- getType,
8
- types,
9
- }
@@ -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
@@ -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
- }
@@ -1,3 +0,0 @@
1
- import error from './error'
2
-
3
- export default error
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
- }
@@ -1,8 +0,0 @@
1
- import { multi, method } from './multimethod'
2
-
3
- export { multi, method }
4
-
5
- export default {
6
- multi,
7
- method,
8
- }
@@ -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,112 +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 createMultimethod = (methodEntries = []) => (defaultMethod = null) => (
26
- dispatch,
27
- ...methods
28
- ) => {
29
- const segmentsCount = countSegments(dispatch)
30
-
31
- const createSegmentedFn = (segmentsCount, previousSegmentsArgs = []) => {
32
- if (segmentsCount === 1) {
33
- return (...args) => {
34
- const segmentsArgs = [...previousSegmentsArgs, args]
35
- let count = segmentsArgs.length
36
-
37
- let currentDispatchValue = dispatch
38
-
39
- for (let i = 0; i < count; i++) {
40
- currentDispatchValue = currentDispatchValue(...segmentsArgs[i])
41
- }
42
-
43
- const entry = methodEntries.find(([dispatchValue]) =>
44
- equal(dispatchValue, currentDispatchValue),
45
- )
46
-
47
- const target = entry ? entry[1] : defaultMethod
48
-
49
- if (!entry && target === null) {
50
- throw new Error('No method specified for provided arguments')
51
- }
52
-
53
- if (typeof target !== 'function') {
54
- return target
55
- }
56
-
57
- let result: any = target
58
-
59
- for (let i = 0; i < count; i++) {
60
- result = result(...segmentsArgs[i])
61
- }
62
-
63
- return result
64
- }
65
- }
66
-
67
- return (...args) => {
68
- return createSegmentedFn(segmentsCount - 1, [
69
- ...previousSegmentsArgs,
70
- args,
71
- ])
72
- }
73
- }
74
-
75
- const resultFn = createSegmentedFn(segmentsCount)
76
-
77
- resultFn[multimethodKey] = {
78
- methodEntries,
79
- defaultMethod,
80
- dispatch,
81
- }
82
-
83
- if (methods.length !== 0) {
84
- return pipe(...methods)(resultFn)
85
- }
86
-
87
- return resultFn
88
- }
89
-
90
- const multi = createMultimethod()()
91
-
92
- const method = (...args) => (multimethod) => {
93
- if (!multimethod[multimethodKey]) {
94
- throw new Error('Function is not a multimethod')
95
- }
96
-
97
- const [first, second] = args
98
- const isNotDefault = second !== undefined
99
- const fn = isNotDefault ? second : first
100
- const dispatchValues = isNotDefault ? first : null
101
-
102
- const { methodEntries, defaultMethod, dispatch } = multimethod[multimethodKey]
103
-
104
- if (isNotDefault) {
105
- const newMethodEntries = [[dispatchValues, fn], ...methodEntries]
106
- return createMultimethod(newMethodEntries)(defaultMethod)(dispatch)
107
- }
108
-
109
- return createMultimethod(methodEntries)(fn)(dispatch)
110
- }
111
-
112
- 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
- }