@rainbow-o23/n1 1.0.50 → 1.0.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/index.cjs +10 -579
- package/index.js +9 -539
- package/lib/envs.d.ts +1 -1
- package/lib/pipeline/index.d.ts +0 -2
- package/lib/pipeline/step-helpers-utils.d.ts +1 -1
- package/package.json +2 -1
- package/rollup.config.base.js +4 -1
- package/src/lib/envs.ts +2 -20
- package/src/lib/pipeline/index.ts +0 -2
- package/src/lib/pipeline/step-helpers-utils.ts +7 -11
- package/lib/pipeline/step-helpers-value-operator.d.ts +0 -34
- package/lib/pipeline/value-operators/action-types.d.ts +0 -18
- package/lib/pipeline/value-operators/index.d.ts +0 -8
- package/lib/pipeline/value-operators/test-any-actions.d.ts +0 -10
- package/lib/pipeline/value-operators/test-decimal-actions.d.ts +0 -25
- package/lib/pipeline/value-operators/test-string-actions.d.ts +0 -2
- package/lib/pipeline/value-operators/testers.d.ts +0 -36
- package/lib/pipeline/value-operators/transform-decimal-actions.d.ts +0 -21
- package/lib/pipeline/value-operators/transform-string-actions.d.ts +0 -10
- package/lib/pipeline/value-operators/transformers.d.ts +0 -31
- package/src/lib/pipeline/step-helpers-value-operator.ts +0 -307
- package/src/lib/pipeline/value-operators/action-types.ts +0 -8
- package/src/lib/pipeline/value-operators/index.ts +0 -10
- package/src/lib/pipeline/value-operators/test-any-actions.ts +0 -58
- package/src/lib/pipeline/value-operators/test-decimal-actions.ts +0 -85
- package/src/lib/pipeline/value-operators/test-string-actions.ts +0 -15
- package/src/lib/pipeline/value-operators/testers.ts +0 -59
- package/src/lib/pipeline/value-operators/transform-decimal-actions.ts +0 -88
- package/src/lib/pipeline/value-operators/transform-string-actions.ts +0 -49
- package/src/lib/pipeline/value-operators/transformers.ts +0 -34
|
@@ -1,9 +1,8 @@
|
|
|
1
|
+
import {isArrayLike, isLength, isPrototype, IValueOperator, ValueOperator} from '@rainbow-n19/n1';
|
|
1
2
|
import {customAlphabet, nanoid} from 'nanoid';
|
|
2
3
|
import {CatchableError, ERR_TRIM_NON_STRING, ExposedUncatchableError, UncatchableError} from '../utils';
|
|
3
4
|
import {PipelineStepHelpers} from './step-helpers';
|
|
4
|
-
import {IValueOperator, ValueOperator} from './step-helpers-value-operator';
|
|
5
5
|
import {StaticImplements} from './types';
|
|
6
|
-
import {isArrayLike, isBlank, isEmpty, isLength, isNotBlank, isNotEmpty, OBJECT_PROTOTYPE} from './value-operators';
|
|
7
6
|
|
|
8
7
|
export interface PipelineStepErrorOptions {
|
|
9
8
|
// exactly same as http status
|
|
@@ -135,10 +134,7 @@ export class StepHelpersUtils {
|
|
|
135
134
|
// utils
|
|
136
135
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
137
136
|
public static isPrototype(value: any): boolean {
|
|
138
|
-
|
|
139
|
-
const proto = (typeof Ctor === 'function' && Ctor.prototype) || OBJECT_PROTOTYPE;
|
|
140
|
-
|
|
141
|
-
return value === proto;
|
|
137
|
+
return isPrototype(value);
|
|
142
138
|
}
|
|
143
139
|
|
|
144
140
|
/**
|
|
@@ -168,7 +164,7 @@ export class StepHelpersUtils {
|
|
|
168
164
|
*/
|
|
169
165
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
170
166
|
public static isEmpty(value: any): boolean {
|
|
171
|
-
return
|
|
167
|
+
return ValueOperator.of(value).isEmpty().ok();
|
|
172
168
|
}
|
|
173
169
|
|
|
174
170
|
/**
|
|
@@ -177,7 +173,7 @@ export class StepHelpersUtils {
|
|
|
177
173
|
*/
|
|
178
174
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
179
175
|
public static isNotEmpty(value: any): boolean {
|
|
180
|
-
return
|
|
176
|
+
return ValueOperator.of(value).isNotEmpty().ok();
|
|
181
177
|
}
|
|
182
178
|
|
|
183
179
|
/**
|
|
@@ -186,7 +182,7 @@ export class StepHelpersUtils {
|
|
|
186
182
|
*/
|
|
187
183
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
188
184
|
public static isBlank(value: any): boolean {
|
|
189
|
-
return
|
|
185
|
+
return ValueOperator.of(value).isBlank().ok();
|
|
190
186
|
}
|
|
191
187
|
|
|
192
188
|
/**
|
|
@@ -195,7 +191,7 @@ export class StepHelpersUtils {
|
|
|
195
191
|
*/
|
|
196
192
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
197
193
|
public static isNotBlank(value: any): boolean {
|
|
198
|
-
return
|
|
194
|
+
return ValueOperator.of(value).isNotBlank().ok();
|
|
199
195
|
}
|
|
200
196
|
|
|
201
197
|
/**
|
|
@@ -216,7 +212,7 @@ export class StepHelpersUtils {
|
|
|
216
212
|
|
|
217
213
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
218
214
|
public static touch(value: any): IValueOperator {
|
|
219
|
-
return ValueOperator.
|
|
215
|
+
return ValueOperator.of(value);
|
|
220
216
|
}
|
|
221
217
|
|
|
222
218
|
/**
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { AllTesters, AllTransformers, RegisteredValueAction, RegisteredValueActionWithParams } from './value-operators';
|
|
2
|
-
export interface FinalValueRetriever {
|
|
3
|
-
value: <T>() => T;
|
|
4
|
-
success: <T>(value: T) => {
|
|
5
|
-
failure: <V>(value: V) => void;
|
|
6
|
-
};
|
|
7
|
-
failure: <V>(value: V) => {
|
|
8
|
-
success: <T>(value: T) => void;
|
|
9
|
-
};
|
|
10
|
-
ok: () => boolean;
|
|
11
|
-
promise: <T>() => Promise<T>;
|
|
12
|
-
}
|
|
13
|
-
export interface DefaultValueSetter extends FinalValueRetriever {
|
|
14
|
-
orUseDefault: (defaultValue: any) => FinalValueRetriever;
|
|
15
|
-
useDefault: (defaultValue: any) => FinalValueRetriever;
|
|
16
|
-
withDefault: (defaultValue: any) => FinalValueRetriever;
|
|
17
|
-
orElse: (defaultValue: any) => FinalValueRetriever;
|
|
18
|
-
else: (defaultValue: any) => FinalValueRetriever;
|
|
19
|
-
}
|
|
20
|
-
type ActionType<T> = T extends RegisteredValueAction ? (ValueActionsWithDefault & (() => ValueActionsWithDefault)) : T extends RegisteredValueActionWithParams ? ((...args: Parameters<T['func']>) => ValueActionsWithDefault) : never;
|
|
21
|
-
export type ValueActions = {
|
|
22
|
-
[K in keyof typeof AllTesters]: ActionType<typeof AllTesters[K]>;
|
|
23
|
-
} & {
|
|
24
|
-
[K in keyof typeof AllTransformers]: ActionType<typeof AllTransformers[K]>;
|
|
25
|
-
};
|
|
26
|
-
export type ValueActionsWithDefault = ValueActions & DefaultValueSetter;
|
|
27
|
-
export type IValueOperator = ValueActions;
|
|
28
|
-
export interface IValueOperatorBoostrap {
|
|
29
|
-
of(value: any): IValueOperator;
|
|
30
|
-
from(value: any): IValueOperator;
|
|
31
|
-
with(value: any): IValueOperator;
|
|
32
|
-
}
|
|
33
|
-
export declare const ValueOperator: IValueOperatorBoostrap;
|
|
34
|
-
export {};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export type ValueActionPassed<V> = {
|
|
2
|
-
test: true;
|
|
3
|
-
value: V;
|
|
4
|
-
};
|
|
5
|
-
export type ValueActionFailed<V> = {
|
|
6
|
-
test: false;
|
|
7
|
-
value: V;
|
|
8
|
-
};
|
|
9
|
-
export type ValueAction<From = any, To = any> = (value?: From) => ValueActionPassed<To> | ValueActionFailed<From>;
|
|
10
|
-
export type RegisteredValueAction = {
|
|
11
|
-
type: 'func';
|
|
12
|
-
func: ValueAction;
|
|
13
|
-
};
|
|
14
|
-
export type RegisteredValueActionWithParams<F = (...args: Array<any>) => ValueAction> = {
|
|
15
|
-
type: 'param';
|
|
16
|
-
func: F;
|
|
17
|
-
};
|
|
18
|
-
export declare const OBJECT_PROTOTYPE: Object;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export * from './action-types';
|
|
2
|
-
export * from './test-any-actions';
|
|
3
|
-
export * from './test-string-actions';
|
|
4
|
-
export * from './test-decimal-actions';
|
|
5
|
-
export * from './testers';
|
|
6
|
-
export * from './transform-string-actions';
|
|
7
|
-
export * from './transform-decimal-actions';
|
|
8
|
-
export * from './transformers';
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { ValueAction } from './action-types';
|
|
2
|
-
export declare const isLength: (value: any) => boolean;
|
|
3
|
-
export declare const isArrayLike: (value: any) => boolean;
|
|
4
|
-
export declare const isPrototype: (value: any) => boolean;
|
|
5
|
-
export declare const isNull: ValueAction<any, null | undefined>;
|
|
6
|
-
export declare const isNotNull: ValueAction<any, NonNullable<any>>;
|
|
7
|
-
export declare const isEmpty: ValueAction;
|
|
8
|
-
export declare const isNotEmpty: ValueAction;
|
|
9
|
-
export declare const isBlank: ValueAction<any, null | undefined | string>;
|
|
10
|
-
export declare const isNotBlank: ValueAction;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import Decimal from 'decimal.js';
|
|
2
|
-
import { ValueAction } from './action-types';
|
|
3
|
-
export declare const isDecimal: ValueAction<any, Decimal>;
|
|
4
|
-
export declare const isInteger: ValueAction<any, Decimal>;
|
|
5
|
-
export type DecimalInterval = 'closed' | 'c' | 'open' | 'o' | 'left-open' | 'lo' | 'right-open' | 'ro';
|
|
6
|
-
export type DecimalInRangeOptions = {
|
|
7
|
-
interval?: DecimalInterval;
|
|
8
|
-
} & ({
|
|
9
|
-
min: Decimal.Value;
|
|
10
|
-
max?: Decimal.Value;
|
|
11
|
-
} | {
|
|
12
|
-
min?: Decimal.Value;
|
|
13
|
-
max: Decimal.Value;
|
|
14
|
-
});
|
|
15
|
-
export declare const isInRange: (options: DecimalInRangeOptions) => ValueAction<any, Decimal>;
|
|
16
|
-
export declare const isPositive: ValueAction<any, Decimal>;
|
|
17
|
-
export declare const isNotPositive: ValueAction<any, Decimal>;
|
|
18
|
-
export declare const isNegative: ValueAction<any, Decimal>;
|
|
19
|
-
export declare const isNotNegative: ValueAction<any, Decimal>;
|
|
20
|
-
export declare const isZero: ValueAction<any, Decimal>;
|
|
21
|
-
export declare const isNotZero: ValueAction<any, Decimal>;
|
|
22
|
-
export declare const isGreaterThan: (compare: Decimal.Value) => ValueAction<any, Decimal>;
|
|
23
|
-
export declare const isGreaterThanOrEqual: (compare: Decimal.Value) => ValueAction<any, Decimal>;
|
|
24
|
-
export declare const isLessThan: (compare: Decimal.Value) => ValueAction<any, Decimal>;
|
|
25
|
-
export declare const isLessThanOrEqual: (compare: Decimal.Value) => ValueAction<any, Decimal>;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { RegisteredValueAction, RegisteredValueActionWithParams } from './action-types';
|
|
2
|
-
import { isGreaterThan, isGreaterThanOrEqual, isInRange, isLessThan, isLessThanOrEqual } from './test-decimal-actions';
|
|
3
|
-
import { regexp } from './test-string-actions';
|
|
4
|
-
declare const testers: {
|
|
5
|
-
isNull: RegisteredValueAction;
|
|
6
|
-
isNotNull: RegisteredValueAction;
|
|
7
|
-
isEmpty: RegisteredValueAction;
|
|
8
|
-
isNotEmpty: RegisteredValueAction;
|
|
9
|
-
isBlank: RegisteredValueAction;
|
|
10
|
-
isNotBlank: RegisteredValueAction;
|
|
11
|
-
regexp: RegisteredValueActionWithParams<typeof regexp>;
|
|
12
|
-
regex: RegisteredValueActionWithParams<typeof regexp>;
|
|
13
|
-
matches: RegisteredValueActionWithParams<typeof regexp>;
|
|
14
|
-
isNumber: RegisteredValueAction;
|
|
15
|
-
isDecimal: RegisteredValueAction;
|
|
16
|
-
isInteger: RegisteredValueAction;
|
|
17
|
-
isInt: RegisteredValueAction;
|
|
18
|
-
isPositive: RegisteredValueAction;
|
|
19
|
-
isNotPositive: RegisteredValueAction;
|
|
20
|
-
isNegative: RegisteredValueAction;
|
|
21
|
-
isNotNegative: RegisteredValueAction;
|
|
22
|
-
isZero: RegisteredValueAction;
|
|
23
|
-
isNotZero: RegisteredValueAction;
|
|
24
|
-
isGreaterThan: RegisteredValueActionWithParams<typeof isGreaterThan>;
|
|
25
|
-
gt: RegisteredValueActionWithParams<typeof isGreaterThan>;
|
|
26
|
-
isGreaterThanOrEqual: RegisteredValueActionWithParams<typeof isGreaterThanOrEqual>;
|
|
27
|
-
gte: RegisteredValueActionWithParams<typeof isGreaterThanOrEqual>;
|
|
28
|
-
isLessThan: RegisteredValueActionWithParams<typeof isLessThan>;
|
|
29
|
-
lt: RegisteredValueActionWithParams<typeof isLessThan>;
|
|
30
|
-
isLessThanOrEqual: RegisteredValueActionWithParams<typeof isLessThanOrEqual>;
|
|
31
|
-
lte: RegisteredValueActionWithParams<typeof isLessThanOrEqual>;
|
|
32
|
-
isInRange: RegisteredValueActionWithParams<typeof isInRange>;
|
|
33
|
-
within: RegisteredValueActionWithParams<typeof isInRange>;
|
|
34
|
-
};
|
|
35
|
-
export declare const AllTesters: Readonly<typeof testers>;
|
|
36
|
-
export {};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import Decimal from 'decimal.js';
|
|
2
|
-
import { ValueAction } from './action-types';
|
|
3
|
-
export declare enum Rounding {
|
|
4
|
-
ROUND_UP = "up",
|
|
5
|
-
ROUND_DOWN = "down",
|
|
6
|
-
ROUND_CEIL = "ceil",
|
|
7
|
-
ROUND_FLOOR = "floor",
|
|
8
|
-
ROUND_HALF_UP = "half-up",
|
|
9
|
-
ROUND_HALF_DOWN = "half-down",
|
|
10
|
-
ROUND_HALF_EVEN = "half-even",
|
|
11
|
-
ROUND_HALF_CEIL = "half-ceil",
|
|
12
|
-
ROUND_HALF_FLOOR = "half-floor"
|
|
13
|
-
}
|
|
14
|
-
export declare const toDecimal: ValueAction<any, Decimal>;
|
|
15
|
-
export declare const toNumber: ValueAction<any, number>;
|
|
16
|
-
export declare const toFixed: (fractionDigits: number, rounding?: Rounding) => ValueAction<any, string>;
|
|
17
|
-
export declare const roundUp: (fractionDigits: number) => ValueAction<any, Decimal>;
|
|
18
|
-
export declare const roundDown: (fractionDigits: number) => ValueAction<any, Decimal>;
|
|
19
|
-
export declare const floor: (fractionDigits: number) => ValueAction<any, Decimal>;
|
|
20
|
-
export declare const ceil: (fractionDigits: number) => ValueAction<any, Decimal>;
|
|
21
|
-
export declare const roundBy: (fractionDigits: number, rounding?: Rounding) => ValueAction<any, Decimal>;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { ValueAction } from './action-types';
|
|
2
|
-
export declare const trim: ValueAction;
|
|
3
|
-
export type PadOptions = {
|
|
4
|
-
length: number;
|
|
5
|
-
char?: string;
|
|
6
|
-
direction?: 'left' | 'right';
|
|
7
|
-
};
|
|
8
|
-
export declare const pad: (options: PadOptions) => ValueAction;
|
|
9
|
-
export declare const padStart: (options: Omit<PadOptions, "direction">) => ValueAction;
|
|
10
|
-
export declare const padEnd: (options: Omit<PadOptions, "direction">) => ValueAction;
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { RegisteredValueAction, RegisteredValueActionWithParams } from './action-types';
|
|
2
|
-
import { ceil, floor, roundBy, roundDown, roundUp, toFixed } from './transform-decimal-actions';
|
|
3
|
-
import { pad, padEnd, padStart } from './transform-string-actions';
|
|
4
|
-
declare const transformers: {
|
|
5
|
-
trim: RegisteredValueAction;
|
|
6
|
-
pad: RegisteredValueActionWithParams<typeof pad>;
|
|
7
|
-
padStart: RegisteredValueActionWithParams<typeof padStart>;
|
|
8
|
-
padLeft: RegisteredValueActionWithParams<typeof padStart>;
|
|
9
|
-
lpad: RegisteredValueActionWithParams<typeof padStart>;
|
|
10
|
-
padEnd: RegisteredValueActionWithParams<typeof padEnd>;
|
|
11
|
-
padRight: RegisteredValueActionWithParams<typeof padEnd>;
|
|
12
|
-
rpad: RegisteredValueActionWithParams<typeof padEnd>;
|
|
13
|
-
toDecimal: RegisteredValueAction;
|
|
14
|
-
toNumber: RegisteredValueAction;
|
|
15
|
-
toFixed0: RegisteredValueAction;
|
|
16
|
-
toFixed1: RegisteredValueAction;
|
|
17
|
-
toFixed2: RegisteredValueAction;
|
|
18
|
-
toFixed3: RegisteredValueAction;
|
|
19
|
-
toFixed4: RegisteredValueAction;
|
|
20
|
-
toFixed5: RegisteredValueAction;
|
|
21
|
-
toFixed6: RegisteredValueAction;
|
|
22
|
-
toFixed: RegisteredValueActionWithParams<typeof toFixed>;
|
|
23
|
-
round: RegisteredValueActionWithParams<typeof roundUp>;
|
|
24
|
-
roundUp: RegisteredValueActionWithParams<typeof roundUp>;
|
|
25
|
-
roundDown: RegisteredValueActionWithParams<typeof roundDown>;
|
|
26
|
-
floor: RegisteredValueActionWithParams<typeof floor>;
|
|
27
|
-
ceil: RegisteredValueActionWithParams<typeof ceil>;
|
|
28
|
-
roundBy: RegisteredValueActionWithParams<typeof roundBy>;
|
|
29
|
-
};
|
|
30
|
-
export declare const AllTransformers: Readonly<typeof transformers>;
|
|
31
|
-
export {};
|
|
@@ -1,307 +0,0 @@
|
|
|
1
|
-
import {StaticImplements} from './types';
|
|
2
|
-
import {
|
|
3
|
-
AllTesters,
|
|
4
|
-
AllTransformers,
|
|
5
|
-
RegisteredValueAction,
|
|
6
|
-
RegisteredValueActionWithParams,
|
|
7
|
-
ValueAction,
|
|
8
|
-
ValueActionFailed,
|
|
9
|
-
ValueActionPassed
|
|
10
|
-
} from './value-operators';
|
|
11
|
-
|
|
12
|
-
interface ValueOperatorBase {
|
|
13
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
-
$value?: any;
|
|
15
|
-
$allowMoreAction: boolean;
|
|
16
|
-
$actions?: Array<ValueAction>;
|
|
17
|
-
$allowUseDefault: boolean;
|
|
18
|
-
$defaultUsed: boolean;
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
-
$defaultValue?: any;
|
|
21
|
-
// control
|
|
22
|
-
$allowNoParamFuncCall: boolean;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* function call is allowed for tester and transformer, only when the {@link $allowNoParamFuncCall} is true.
|
|
27
|
-
* and once it is called, set {@link $allowNoParamFuncCall} to false.
|
|
28
|
-
* otherwise, throw exception.
|
|
29
|
-
*/
|
|
30
|
-
const applyOperator = (operator: IValueOperator, base: ValueOperatorBase) => {
|
|
31
|
-
if (base.$allowNoParamFuncCall) {
|
|
32
|
-
base.$allowNoParamFuncCall = false;
|
|
33
|
-
return operator;
|
|
34
|
-
} else {
|
|
35
|
-
throw new Error(`Function call is not allowed from: ${operator}.`);
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
const NOT_FOUND = Symbol('not found');
|
|
39
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
40
|
-
type ValueActionFind = (operator: IValueOperator, base: ValueOperatorBase, prop: string) => IValueOperator | ((...args: Array<any>) => IValueOperator) | symbol;
|
|
41
|
-
const findValueAction = (actions: Record<string, RegisteredValueAction | RegisteredValueActionWithParams>): ValueActionFind => {
|
|
42
|
-
return (operator: IValueOperator, base: ValueOperatorBase, prop: string) => {
|
|
43
|
-
const tester = actions[prop];
|
|
44
|
-
if (tester == null) {
|
|
45
|
-
return NOT_FOUND;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
base.$allowUseDefault = true;
|
|
49
|
-
if (base.$actions == null) {
|
|
50
|
-
base.$actions = [];
|
|
51
|
-
}
|
|
52
|
-
if (tester.type === 'func') {
|
|
53
|
-
// push to tester stack
|
|
54
|
-
base.$actions.push(tester.func);
|
|
55
|
-
base.$allowNoParamFuncCall = true;
|
|
56
|
-
return operator;
|
|
57
|
-
} else if (tester.type === 'param') {
|
|
58
|
-
// build a function to accept parameters
|
|
59
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
60
|
-
return (...args: Array<any>) => {
|
|
61
|
-
base.$actions.push(tester.func(...args));
|
|
62
|
-
return operator;
|
|
63
|
-
};
|
|
64
|
-
} else {
|
|
65
|
-
throw new Error(`Unknown tester type: ${tester}.`);
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
};
|
|
69
|
-
const findTester = findValueAction(AllTesters);
|
|
70
|
-
const findTransformer = findValueAction(AllTransformers);
|
|
71
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
-
type UseDefaultFind = (operator: IValueOperator, base: ValueOperatorBase, prop: string) => ((defaultValue: any) => IValueOperator) | undefined | symbol;
|
|
73
|
-
const findUseDefault: UseDefaultFind = (operator: IValueOperator, base: ValueOperatorBase, prop: string) => {
|
|
74
|
-
if (!base.$allowUseDefault || !['orUseDefault', 'useDefault', 'withDefault', 'orElse', 'else'].includes(prop)) {
|
|
75
|
-
return NOT_FOUND;
|
|
76
|
-
}
|
|
77
|
-
if ((base.$actions == null || base.$actions.length === 0)) {
|
|
78
|
-
// no action defined, use default value directly, no way
|
|
79
|
-
return (void 0);
|
|
80
|
-
}
|
|
81
|
-
base.$allowMoreAction = false;
|
|
82
|
-
base.$allowUseDefault = false;
|
|
83
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
84
|
-
return (defaultValue: any) => {
|
|
85
|
-
base.$defaultUsed = true;
|
|
86
|
-
base.$defaultValue = defaultValue;
|
|
87
|
-
return operator;
|
|
88
|
-
};
|
|
89
|
-
};
|
|
90
|
-
type ValueFind = (operator: IValueOperator, base: ValueOperatorBase, prop: string) => (<T>() => T) | undefined | symbol;
|
|
91
|
-
const createValueRetrieveFunc = (base: ValueOperatorBase) => {
|
|
92
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
93
|
-
return <T>(): ValueActionPassed<T> | ValueActionFailed<any> => {
|
|
94
|
-
const tested = {test: true, value: base.$value};
|
|
95
|
-
for (const action of (base.$actions ?? [])) {
|
|
96
|
-
const result = action(tested.value);
|
|
97
|
-
if (!result.test) {
|
|
98
|
-
// failed on action proceeding, ignore all tailing actions, and use original value
|
|
99
|
-
tested.test = false;
|
|
100
|
-
tested.value = base.$value;
|
|
101
|
-
break;
|
|
102
|
-
} else {
|
|
103
|
-
// action proceeded successfully
|
|
104
|
-
tested.value = result.value;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
if (tested.test) {
|
|
108
|
-
// all actions proceeded, and value might be transformed, do nothing
|
|
109
|
-
} else if (base.$defaultUsed) {
|
|
110
|
-
// failed on action proceeding, use default value if defined
|
|
111
|
-
// therefore, final result has been treated as success
|
|
112
|
-
tested.test = true;
|
|
113
|
-
tested.value = base.$defaultValue as T;
|
|
114
|
-
} else {
|
|
115
|
-
// failed on acton proceeding, and no default value defined, return value itself
|
|
116
|
-
tested.value = base.$value;
|
|
117
|
-
}
|
|
118
|
-
return tested;
|
|
119
|
-
};
|
|
120
|
-
};
|
|
121
|
-
const findValue: ValueFind = (_operator: IValueOperator, base: ValueOperatorBase, prop: string) => {
|
|
122
|
-
if (prop !== 'value') {
|
|
123
|
-
return NOT_FOUND;
|
|
124
|
-
}
|
|
125
|
-
if ((base.$actions == null || base.$actions.length === 0)) {
|
|
126
|
-
// no action defined, get value directly, no way
|
|
127
|
-
return (void 0);
|
|
128
|
-
}
|
|
129
|
-
// return value anyway, no matter success or failure
|
|
130
|
-
return <T>(): T => createValueRetrieveFunc(base)<T>().value;
|
|
131
|
-
};
|
|
132
|
-
type SuccessCallback = (callback: <T>(value: T) => void) => ({ failure: (callback: <V>(value: V) => void) => void });
|
|
133
|
-
type FailureCallback = (callback: <V>(value: V) => void) => ({ success: (callback: <T>(value: T) => void) => void });
|
|
134
|
-
type SuccessOrFailureCallbackFind = (operator: IValueOperator, base: ValueOperatorBase, prop: string) => SuccessCallback | FailureCallback | undefined | symbol;
|
|
135
|
-
const findSuccessOrFailureCallback: SuccessOrFailureCallbackFind = (_operator: IValueOperator, base: ValueOperatorBase, prop: string) => {
|
|
136
|
-
if (prop !== 'success' && prop !== 'failure') {
|
|
137
|
-
return NOT_FOUND;
|
|
138
|
-
}
|
|
139
|
-
if ((base.$actions == null || base.$actions.length === 0)) {
|
|
140
|
-
// no action defined, get value directly, no way
|
|
141
|
-
return (void 0);
|
|
142
|
-
}
|
|
143
|
-
switch (prop) {
|
|
144
|
-
case 'success':
|
|
145
|
-
return (callback: <T>(value: T) => void) => {
|
|
146
|
-
const tested = createValueRetrieveFunc(base)();
|
|
147
|
-
if (tested.test) {
|
|
148
|
-
callback(tested.value);
|
|
149
|
-
}
|
|
150
|
-
return {
|
|
151
|
-
failure: (callback: <T>(value: T) => void): void => {
|
|
152
|
-
if (!tested.test) {
|
|
153
|
-
callback(tested.value);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
};
|
|
158
|
-
case 'failure':
|
|
159
|
-
return (callback: <V>(value: V) => void) => {
|
|
160
|
-
const tested = createValueRetrieveFunc(base)();
|
|
161
|
-
if (!tested.test) {
|
|
162
|
-
callback(tested.value);
|
|
163
|
-
}
|
|
164
|
-
return {
|
|
165
|
-
success: (callback: <T>(value: T) => void): void => {
|
|
166
|
-
if (tested.test) {
|
|
167
|
-
callback(tested.value);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
};
|
|
171
|
-
};
|
|
172
|
-
default:
|
|
173
|
-
throw new Error(`Unknown callback type: ${prop}.`);
|
|
174
|
-
}
|
|
175
|
-
};
|
|
176
|
-
type OKFind = (operator: IValueOperator, base: ValueOperatorBase, prop: string) => (() => boolean) | symbol;
|
|
177
|
-
const findOK: OKFind = (operator: IValueOperator, base: ValueOperatorBase, prop: string) => {
|
|
178
|
-
if (prop !== 'ok') {
|
|
179
|
-
return NOT_FOUND;
|
|
180
|
-
}
|
|
181
|
-
if ((base.$actions == null || base.$actions.length === 0)) {
|
|
182
|
-
// no action defined, get value directly, no way
|
|
183
|
-
return (void 0);
|
|
184
|
-
}
|
|
185
|
-
return (): boolean => createValueRetrieveFunc(base)().test;
|
|
186
|
-
};
|
|
187
|
-
type PromiseFind = (operator: IValueOperator, base: ValueOperatorBase, prop: string) => (<T>() => Promise<T>) | symbol;
|
|
188
|
-
const findPromise: PromiseFind = (_operator: IValueOperator, base: ValueOperatorBase, prop: string) => {
|
|
189
|
-
if (prop !== 'promise') {
|
|
190
|
-
return NOT_FOUND;
|
|
191
|
-
}
|
|
192
|
-
if ((base.$actions == null || base.$actions.length === 0)) {
|
|
193
|
-
// no action defined, get value directly, no way
|
|
194
|
-
return (void 0);
|
|
195
|
-
}
|
|
196
|
-
return async <T>(): Promise<T> => {
|
|
197
|
-
const tested = createValueRetrieveFunc(base)();
|
|
198
|
-
if (tested.test) {
|
|
199
|
-
return Promise.resolve(tested.value as T);
|
|
200
|
-
} else {
|
|
201
|
-
return Promise.reject(tested.value);
|
|
202
|
-
}
|
|
203
|
-
};
|
|
204
|
-
};
|
|
205
|
-
const getFromOperator = (operator: IValueOperator, base: ValueOperatorBase, prop: string) => {
|
|
206
|
-
// set to false anyway
|
|
207
|
-
base.$allowNoParamFuncCall = false;
|
|
208
|
-
const finds = [
|
|
209
|
-
findTester, findTransformer, findUseDefault,
|
|
210
|
-
findValue, findSuccessOrFailureCallback, findOK, findPromise
|
|
211
|
-
];
|
|
212
|
-
for (const find of finds) {
|
|
213
|
-
const result = find(operator, base, prop);
|
|
214
|
-
if (result !== NOT_FOUND) {
|
|
215
|
-
return result;
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
// not found, return undefined
|
|
219
|
-
return (void 0);
|
|
220
|
-
};
|
|
221
|
-
const createOperator = (base: ValueOperatorBase): IValueOperator => {
|
|
222
|
-
const operatorBase = () => {
|
|
223
|
-
};
|
|
224
|
-
operatorBase.$base = base;
|
|
225
|
-
const operator = new Proxy(operatorBase, {
|
|
226
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
|
|
227
|
-
apply(base: typeof operatorBase, _thisArg: any, _argArray: any[]): any {
|
|
228
|
-
return applyOperator(operator, base.$base);
|
|
229
|
-
},
|
|
230
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unused-vars
|
|
231
|
-
get(base: any, p: string, _receiver: any): any {
|
|
232
|
-
return getFromOperator(operator, base.$base, p);
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
return operator;
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
export interface FinalValueRetriever {
|
|
239
|
-
value: <T>() => T;
|
|
240
|
-
success: <T>(value: T) => { failure: <V>(value: V) => void };
|
|
241
|
-
failure: <V>(value: V) => { success: <T>(value: T) => void };
|
|
242
|
-
ok: () => boolean;
|
|
243
|
-
// parameter of reject original value
|
|
244
|
-
promise: <T>() => Promise<T>;
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
export interface DefaultValueSetter extends FinalValueRetriever {
|
|
248
|
-
// once, or ignored
|
|
249
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
250
|
-
orUseDefault: (defaultValue: any) => FinalValueRetriever;
|
|
251
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
252
|
-
useDefault: (defaultValue: any) => FinalValueRetriever;
|
|
253
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
254
|
-
withDefault: (defaultValue: any) => FinalValueRetriever;
|
|
255
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
256
|
-
orElse: (defaultValue: any) => FinalValueRetriever;
|
|
257
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
258
|
-
else: (defaultValue: any) => FinalValueRetriever;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
type ActionType<T> = T extends RegisteredValueAction
|
|
262
|
-
? (ValueActionsWithDefault & (() => ValueActionsWithDefault))
|
|
263
|
-
: T extends RegisteredValueActionWithParams ? ((...args: Parameters<T['func']>) => ValueActionsWithDefault) : never;
|
|
264
|
-
export type ValueActions =
|
|
265
|
-
& { [K in keyof typeof AllTesters]: ActionType<typeof AllTesters[K]> }
|
|
266
|
-
& { [K in keyof typeof AllTransformers]: ActionType<typeof AllTransformers[K]> };
|
|
267
|
-
export type ValueActionsWithDefault = ValueActions & DefaultValueSetter;
|
|
268
|
-
|
|
269
|
-
export type IValueOperator = ValueActions;
|
|
270
|
-
|
|
271
|
-
export interface IValueOperatorBoostrap {
|
|
272
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
273
|
-
of(value: any): IValueOperator;
|
|
274
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
275
|
-
from(value: any): IValueOperator;
|
|
276
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
277
|
-
with(value: any): IValueOperator;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
@StaticImplements<IValueOperatorBoostrap>()
|
|
281
|
-
class ValueOperatorBootstrap {
|
|
282
|
-
// noinspection JSUnusedLocalSymbols
|
|
283
|
-
private constructor() {
|
|
284
|
-
// avoid extend
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
288
|
-
public static of(value: any): IValueOperator {
|
|
289
|
-
return createOperator({
|
|
290
|
-
$value: value,
|
|
291
|
-
$allowMoreAction: true, $allowUseDefault: false, $defaultUsed: false,
|
|
292
|
-
$allowNoParamFuncCall: false
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
297
|
-
public static from(value: any): IValueOperator {
|
|
298
|
-
return ValueOperatorBootstrap.of(value);
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
302
|
-
public static with(value: any): IValueOperator {
|
|
303
|
-
return ValueOperatorBootstrap.of(value);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
export const ValueOperator: IValueOperatorBoostrap = ValueOperatorBootstrap;
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export type ValueActionPassed<V> = { test: true, value: V };
|
|
2
|
-
export type ValueActionFailed<V> = { test: false, value: V };
|
|
3
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
4
|
-
export type ValueAction<From = any, To = any> = (value?: From) => ValueActionPassed<To> | ValueActionFailed<From>;
|
|
5
|
-
export type RegisteredValueAction = { type: 'func', func: ValueAction }
|
|
6
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
-
export type RegisteredValueActionWithParams<F = (...args: Array<any>) => ValueAction> = { type: 'param', func: F };
|
|
8
|
-
export const OBJECT_PROTOTYPE = Object.prototype;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export * from './action-types';
|
|
2
|
-
|
|
3
|
-
export * from './test-any-actions';
|
|
4
|
-
export * from './test-string-actions';
|
|
5
|
-
export * from './test-decimal-actions';
|
|
6
|
-
export * from './testers';
|
|
7
|
-
|
|
8
|
-
export * from './transform-string-actions';
|
|
9
|
-
export * from './transform-decimal-actions';
|
|
10
|
-
export * from './transformers';
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import {OBJECT_PROTOTYPE, ValueAction} from './action-types';
|
|
2
|
-
|
|
3
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
4
|
-
export const isLength = (value: any): boolean => {
|
|
5
|
-
return typeof value === 'number' && value > -1 && value % 1 === 0 && value <= Number.MAX_SAFE_INTEGER;
|
|
6
|
-
};
|
|
7
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
|
-
export const isArrayLike = (value: any): boolean => {
|
|
9
|
-
return value != null && typeof value !== 'function' && isLength(value.length);
|
|
10
|
-
};
|
|
11
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
12
|
-
export const isPrototype = (value: any): boolean => {
|
|
13
|
-
const Ctor = value && value.constructor;
|
|
14
|
-
const proto = (typeof Ctor === 'function' && Ctor.prototype) || OBJECT_PROTOTYPE;
|
|
15
|
-
|
|
16
|
-
return value === proto;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
-
export const isNull: ValueAction<any, null | undefined> = (value?: any) => {
|
|
21
|
-
return value == null
|
|
22
|
-
? {test: true, value: value as null | undefined}
|
|
23
|
-
: {test: false, value};
|
|
24
|
-
};
|
|
25
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
|
-
export const isNotNull: ValueAction<any, NonNullable<any>> = (value?: any) => ({test: !isNull(value).test, value});
|
|
27
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
-
export const isEmpty: ValueAction = (value?: any) => {
|
|
29
|
-
if (value == null) {
|
|
30
|
-
return {test: true, value: value as null | undefined};
|
|
31
|
-
}
|
|
32
|
-
let length: number | null = null;
|
|
33
|
-
if (isArrayLike(value) && (Array.isArray(value) || typeof value === 'string')) {
|
|
34
|
-
length = value.length;
|
|
35
|
-
} else if (value instanceof Map) {
|
|
36
|
-
length = value.size;
|
|
37
|
-
} else if (value instanceof Set) {
|
|
38
|
-
length = value.size;
|
|
39
|
-
} else if (isPrototype(value)) {
|
|
40
|
-
length = Object.keys(value).length;
|
|
41
|
-
}
|
|
42
|
-
return {test: length === 0, value};
|
|
43
|
-
};
|
|
44
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
45
|
-
export const isNotEmpty: ValueAction = (value?: any) => ({test: !isEmpty(value).test, value});
|
|
46
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
47
|
-
export const isBlank: ValueAction<any, null | undefined | string> = (value?: any) => {
|
|
48
|
-
switch (true) {
|
|
49
|
-
case (value == null):
|
|
50
|
-
return {test: true, value: value as null | undefined};
|
|
51
|
-
case (typeof value === 'string' && value.trim().length === 0):
|
|
52
|
-
return {test: true, value: value as string};
|
|
53
|
-
default:
|
|
54
|
-
return {test: false, value};
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
58
|
-
export const isNotBlank: ValueAction = (value?: any) => ({test: !isBlank(value).test, value});
|