@keeex/utils 7.0.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/LICENSE +9 -0
- package/README.md +105 -0
- package/lib/array.d.ts +28 -0
- package/lib/array.js +36 -0
- package/lib/arraybuffer.d.ts +138 -0
- package/lib/arraybuffer.js +141 -0
- package/lib/async/asynctrigger.d.ts +50 -0
- package/lib/async/asynctrigger.js +108 -0
- package/lib/async/deferredpromise.d.ts +32 -0
- package/lib/async/deferredpromise.js +66 -0
- package/lib/async/keycache.d.ts +56 -0
- package/lib/async/keycache.js +103 -0
- package/lib/async/queues.d.ts +69 -0
- package/lib/async/queues.js +135 -0
- package/lib/async/timecache.d.ts +58 -0
- package/lib/async/timecache.js +118 -0
- package/lib/base58.d.ts +27 -0
- package/lib/base58.js +83 -0
- package/lib/base64.d.ts +51 -0
- package/lib/base64.js +126 -0
- package/lib/benchmark.d.ts +126 -0
- package/lib/benchmark.js +177 -0
- package/lib/bits/arraybuffer.d.ts +35 -0
- package/lib/bits/arraybuffer.js +64 -0
- package/lib/bits/base64.d.ts +35 -0
- package/lib/bits/base64.js +70 -0
- package/lib/bits/hex.d.ts +17 -0
- package/lib/bits/hex.js +30 -0
- package/lib/bits/uint8array.d.ts +28 -0
- package/lib/bits/uint8array.js +42 -0
- package/lib/bytebuffer.d.ts +27 -0
- package/lib/bytebuffer.js +29 -0
- package/lib/consts.d.ts +33 -0
- package/lib/consts.js +33 -0
- package/lib/cron/logger.d.ts +22 -0
- package/lib/cron/logger.js +31 -0
- package/lib/cron/scheduledtask.d.ts +71 -0
- package/lib/cron/scheduledtask.js +137 -0
- package/lib/cron/types.d.ts +53 -0
- package/lib/cron/types.js +31 -0
- package/lib/cron.d.ts +29 -0
- package/lib/cron.js +47 -0
- package/lib/dict.d.ts +56 -0
- package/lib/dict.js +74 -0
- package/lib/error.d.ts +25 -0
- package/lib/error.js +41 -0
- package/lib/global.d.ts +27 -0
- package/lib/global.js +53 -0
- package/lib/hex.d.ts +32 -0
- package/lib/hex.js +58 -0
- package/lib/idx.d.ts +51 -0
- package/lib/idx.js +81 -0
- package/lib/json.d.ts +57 -0
- package/lib/json.js +116 -0
- package/lib/marshalling/marshaller.d.ts +51 -0
- package/lib/marshalling/marshaller.js +155 -0
- package/lib/marshalling/unmarshaller.d.ts +53 -0
- package/lib/marshalling/unmarshaller.js +124 -0
- package/lib/marshalling/util.d.ts +25 -0
- package/lib/marshalling/util.js +25 -0
- package/lib/number.d.ts +17 -0
- package/lib/number.js +21 -0
- package/lib/path.d.ts +25 -0
- package/lib/path.js +29 -0
- package/lib/promise.d.ts +42 -0
- package/lib/promise.js +78 -0
- package/lib/starttime.d.ts +23 -0
- package/lib/starttime.js +29 -0
- package/lib/string.d.ts +65 -0
- package/lib/string.js +108 -0
- package/lib/types/array.d.ts +34 -0
- package/lib/types/array.js +64 -0
- package/lib/types/enum.d.ts +30 -0
- package/lib/types/enum.js +44 -0
- package/lib/types/predicateerror.d.ts +40 -0
- package/lib/types/predicateerror.js +107 -0
- package/lib/types/primitive.d.ts +23 -0
- package/lib/types/primitive.js +34 -0
- package/lib/types/record.d.ts +67 -0
- package/lib/types/record.js +235 -0
- package/lib/types/types.d.ts +64 -0
- package/lib/types/types.js +115 -0
- package/lib/types/utils.d.ts +18 -0
- package/lib/types/utils.js +67 -0
- package/lib/uint8array.d.ts +176 -0
- package/lib/uint8array.js +438 -0
- package/lib/units.d.ts +159 -0
- package/lib/units.js +290 -0
- package/lib/utils/buffer.d.ts +49 -0
- package/lib/utils/buffer.js +79 -0
- package/lib/utils/fourbytes.d.ts +29 -0
- package/lib/utils/fourbytes.js +45 -0
- package/package.json +1 -0
- package/web/array.d.ts +28 -0
- package/web/array.js +34 -0
- package/web/arraybuffer.d.ts +138 -0
- package/web/arraybuffer.js +141 -0
- package/web/async/asynctrigger.d.ts +50 -0
- package/web/async/asynctrigger.js +106 -0
- package/web/async/deferredpromise.d.ts +32 -0
- package/web/async/deferredpromise.js +65 -0
- package/web/async/keycache.d.ts +56 -0
- package/web/async/keycache.js +97 -0
- package/web/async/queues.d.ts +69 -0
- package/web/async/queues.js +131 -0
- package/web/async/timecache.d.ts +58 -0
- package/web/async/timecache.js +107 -0
- package/web/base58.d.ts +27 -0
- package/web/base58.js +78 -0
- package/web/base64.d.ts +51 -0
- package/web/base64.js +136 -0
- package/web/benchmark.d.ts +126 -0
- package/web/benchmark.js +183 -0
- package/web/bits/arraybuffer.d.ts +35 -0
- package/web/bits/arraybuffer.js +59 -0
- package/web/bits/base64.d.ts +35 -0
- package/web/bits/base64.js +67 -0
- package/web/bits/hex.d.ts +17 -0
- package/web/bits/hex.js +27 -0
- package/web/bits/uint8array.d.ts +28 -0
- package/web/bits/uint8array.js +41 -0
- package/web/bytebuffer.d.ts +27 -0
- package/web/bytebuffer.js +29 -0
- package/web/consts.d.ts +33 -0
- package/web/consts.js +33 -0
- package/web/cron/logger.d.ts +22 -0
- package/web/cron/logger.js +30 -0
- package/web/cron/scheduledtask.d.ts +71 -0
- package/web/cron/scheduledtask.js +136 -0
- package/web/cron/types.d.ts +53 -0
- package/web/cron/types.js +31 -0
- package/web/cron.d.ts +29 -0
- package/web/cron.js +47 -0
- package/web/dict.d.ts +56 -0
- package/web/dict.js +67 -0
- package/web/error.d.ts +25 -0
- package/web/error.js +39 -0
- package/web/global.d.ts +27 -0
- package/web/global.js +49 -0
- package/web/hex.d.ts +32 -0
- package/web/hex.js +52 -0
- package/web/idx.d.ts +51 -0
- package/web/idx.js +76 -0
- package/web/json.d.ts +57 -0
- package/web/json.js +98 -0
- package/web/marshalling/marshaller.d.ts +51 -0
- package/web/marshalling/marshaller.js +150 -0
- package/web/marshalling/unmarshaller.d.ts +53 -0
- package/web/marshalling/unmarshaller.js +115 -0
- package/web/marshalling/util.d.ts +25 -0
- package/web/marshalling/util.js +25 -0
- package/web/number.d.ts +17 -0
- package/web/number.js +21 -0
- package/web/path.d.ts +25 -0
- package/web/path.js +26 -0
- package/web/promise.d.ts +42 -0
- package/web/promise.js +74 -0
- package/web/starttime.d.ts +23 -0
- package/web/starttime.js +29 -0
- package/web/string.d.ts +65 -0
- package/web/string.js +101 -0
- package/web/types/array.d.ts +34 -0
- package/web/types/array.js +63 -0
- package/web/types/enum.d.ts +30 -0
- package/web/types/enum.js +40 -0
- package/web/types/predicateerror.d.ts +40 -0
- package/web/types/predicateerror.js +128 -0
- package/web/types/primitive.d.ts +23 -0
- package/web/types/primitive.js +33 -0
- package/web/types/record.d.ts +67 -0
- package/web/types/record.js +213 -0
- package/web/types/types.d.ts +64 -0
- package/web/types/types.js +123 -0
- package/web/types/utils.d.ts +18 -0
- package/web/types/utils.js +30 -0
- package/web/uint8array.d.ts +176 -0
- package/web/uint8array.js +412 -0
- package/web/units.d.ts +159 -0
- package/web/units.js +312 -0
- package/web/utils/buffer.d.ts +49 -0
- package/web/utils/buffer.js +76 -0
- package/web/utils/fourbytes.d.ts +29 -0
- package/web/utils/fourbytes.js +45 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
const getErrorPath = ({ keyName, error, cause }) => {
|
|
17
|
+
const res = { key: keyName };
|
|
18
|
+
if (Array.isArray(cause)) {
|
|
19
|
+
res.cause = cause.map((singleCause) => getErrorPath({ cause: singleCause }));
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
21
|
+
}
|
|
22
|
+
else if (cause instanceof PredicateError) {
|
|
23
|
+
res.cause = getErrorPath({
|
|
24
|
+
cause: cause.cause,
|
|
25
|
+
error: cause.error,
|
|
26
|
+
keyName: cause.keyName,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
else if (error) {
|
|
30
|
+
res.cause = error;
|
|
31
|
+
}
|
|
32
|
+
else if (cause) {
|
|
33
|
+
res.cause = cause.message;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
res.cause = "<unknown error>";
|
|
37
|
+
}
|
|
38
|
+
return res;
|
|
39
|
+
};
|
|
40
|
+
const createMessage = ({ keyName, error, cause }) => {
|
|
41
|
+
const errorPath = getErrorPath({
|
|
42
|
+
cause,
|
|
43
|
+
error,
|
|
44
|
+
keyName,
|
|
45
|
+
});
|
|
46
|
+
return JSON.stringify(errorPath);
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Construct an error whose message indicate what property failed to validate and why
|
|
50
|
+
*
|
|
51
|
+
* The static function provided will raise the appropriate error if the `raise` parameter is true,
|
|
52
|
+
* and do nothing otherwise.
|
|
53
|
+
*
|
|
54
|
+
* @internal
|
|
55
|
+
*/
|
|
56
|
+
export class PredicateError extends Error {
|
|
57
|
+
#keyName;
|
|
58
|
+
#error;
|
|
59
|
+
constructor(params) {
|
|
60
|
+
super(createMessage(params), { cause: params.cause });
|
|
61
|
+
this.#keyName = params.keyName;
|
|
62
|
+
this.#error = params.error;
|
|
63
|
+
}
|
|
64
|
+
get keyName() {
|
|
65
|
+
return this.#keyName;
|
|
66
|
+
}
|
|
67
|
+
get error() {
|
|
68
|
+
return this.#error;
|
|
69
|
+
}
|
|
70
|
+
static mandatory = (raise, keyName) => {
|
|
71
|
+
if (raise)
|
|
72
|
+
throw new PredicateError({ keyName, error: "mandatory property missing" });
|
|
73
|
+
};
|
|
74
|
+
static expected = (raise, keyName, expectedMessage, value) => {
|
|
75
|
+
if (raise) {
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/init-declarations
|
|
77
|
+
let valueStr;
|
|
78
|
+
if (value !== undefined) {
|
|
79
|
+
try {
|
|
80
|
+
valueStr = JSON.stringify(value);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
try {
|
|
84
|
+
valueStr = value.toString();
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
valueStr = "<value>";
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const msg = valueStr === undefined ? expectedMessage : `${expectedMessage} (${valueStr})`;
|
|
92
|
+
throw new PredicateError({ keyName, error: `expected ${msg}` });
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
static predicateFailed = (raise, keyName) => {
|
|
96
|
+
if (raise)
|
|
97
|
+
throw new PredicateError({ keyName, error: "predicate failed" });
|
|
98
|
+
};
|
|
99
|
+
static allTryFailed = (raise, keyName, cause) => {
|
|
100
|
+
if (raise)
|
|
101
|
+
throw new PredicateError({ cause, error: "all possibilities failed", keyName });
|
|
102
|
+
};
|
|
103
|
+
static keyError = (raise, keyName, cause) => {
|
|
104
|
+
if (raise)
|
|
105
|
+
throw new PredicateError({ cause, error: cause.message, keyName });
|
|
106
|
+
};
|
|
107
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
import * as types from "./types.js";
|
|
17
|
+
export declare const isPrimitive: <T>(obj: unknown, primitiveType: string, options?: types.PredicateOptions) => obj is T;
|
|
18
|
+
export declare const makePrimitivePredicate: <T>(primitiveType: string) => types.TypePredicate<T>;
|
|
19
|
+
export declare const isBoolean: types.TypePredicate<boolean>;
|
|
20
|
+
export declare const isNumber: types.TypePredicate<number>;
|
|
21
|
+
export declare const isString: types.TypePredicate<string>;
|
|
22
|
+
export declare const isSymbol: types.TypePredicate<symbol>;
|
|
23
|
+
export declare const isBigInt: types.TypePredicate<bigint>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
// #region Imports
|
|
17
|
+
import { PredicateError } from "./predicateerror.js";
|
|
18
|
+
import * as types from "./types.js";
|
|
19
|
+
// #endregion
|
|
20
|
+
// #region Predicates
|
|
21
|
+
export const isPrimitive = (obj, primitiveType, options = false) => {
|
|
22
|
+
if (typeof obj === primitiveType)
|
|
23
|
+
return true;
|
|
24
|
+
const opt = types.getPredicateOptions(options);
|
|
25
|
+
PredicateError.predicateFailed(opt.raise, undefined);
|
|
26
|
+
return false;
|
|
27
|
+
};
|
|
28
|
+
export const makePrimitivePredicate = (primitiveType) => (obj, options = false) => isPrimitive(obj, primitiveType, options);
|
|
29
|
+
export const isBoolean = makePrimitivePredicate("boolean");
|
|
30
|
+
export const isNumber = makePrimitivePredicate("number");
|
|
31
|
+
export const isString = makePrimitivePredicate("string");
|
|
32
|
+
export const isSymbol = makePrimitivePredicate("symbol");
|
|
33
|
+
export const isBigInt = makePrimitivePredicate("bigint");
|
|
34
|
+
// #endregion
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
import { PredicateOptions, TypePredicate } from "./types.js";
|
|
17
|
+
interface ProfileValue<T = unknown> {
|
|
18
|
+
value: T;
|
|
19
|
+
optional?: boolean;
|
|
20
|
+
shortcut?: boolean;
|
|
21
|
+
}
|
|
22
|
+
interface InstanceOfValue {
|
|
23
|
+
clazz: any;
|
|
24
|
+
optional?: boolean;
|
|
25
|
+
shortcut?: boolean;
|
|
26
|
+
}
|
|
27
|
+
interface PredicateValue<T = unknown> {
|
|
28
|
+
predicate: TypePredicate<T>;
|
|
29
|
+
optional?: boolean;
|
|
30
|
+
shortcut?: boolean;
|
|
31
|
+
}
|
|
32
|
+
type AdvancedProfileValue = ProfileValue | InstanceOfValue | PredicateValue;
|
|
33
|
+
type ProfileEntryType = string | TypePredicate | AdvancedProfileValue;
|
|
34
|
+
/**
|
|
35
|
+
* Simple profile to check.
|
|
36
|
+
*
|
|
37
|
+
* Keys are property names, values are typeof values.
|
|
38
|
+
* A value prefixed or suffixed with '?' is optional.
|
|
39
|
+
*/
|
|
40
|
+
export type Profile<T> = Record<keyof T, ProfileEntryType | Array<ProfileEntryType>>;
|
|
41
|
+
/**
|
|
42
|
+
* Check if an object's properties fit the basic types profile.
|
|
43
|
+
*
|
|
44
|
+
* Only fit to check primitive types, equality and using other predicates.
|
|
45
|
+
* If `shortcut: true` is used in the options, only properties marked as "shortcut" are checked.
|
|
46
|
+
* For structured properties like instanceof and predicate this is indicated by the `shortcut`
|
|
47
|
+
* property; for primary type predicate this is indicated by the "!" character either before or
|
|
48
|
+
* after the type name.
|
|
49
|
+
*
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
52
|
+
export declare const isObjectProfile: <T>(obj: unknown, profile: Profile<T>, options?: PredicateOptions) => obj is T;
|
|
53
|
+
/**
|
|
54
|
+
* Create a predicate function based on the given profile.
|
|
55
|
+
*
|
|
56
|
+
* @public
|
|
57
|
+
*/
|
|
58
|
+
export declare const makeProfilePredicate: <TargetType>(profile: Profile<TargetType>) => TypePredicate<TargetType>;
|
|
59
|
+
/**
|
|
60
|
+
* Check if all of an object properties match a given type.
|
|
61
|
+
*
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
64
|
+
export declare const isKeyValueOfType: <ValueType>(obj: unknown, typePredicate: TypePredicate<ValueType>, options?: PredicateOptions) => obj is Record<string, ValueType>;
|
|
65
|
+
/** @public */
|
|
66
|
+
export declare const makeKeyValueOfTypePredicate: <ValueType>(typePredicate: TypePredicate<ValueType>) => TypePredicate<Record<string, ValueType>>;
|
|
67
|
+
export {};
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
import { asError } from "../error.js";
|
|
17
|
+
import { PredicateError } from "./predicateerror.js";
|
|
18
|
+
import { getPredicateOptions, } from "./types.js";
|
|
19
|
+
/**
|
|
20
|
+
* Extract modifiers from primitive type name
|
|
21
|
+
*
|
|
22
|
+
* A type prefixed/suffixed with "?" is optional.
|
|
23
|
+
* A type prefixed/suffixed with "!" is a shortcut (only shortcut are tested in shortcut mode)
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
const trimTypeValue = (typeName) => {
|
|
28
|
+
let expectedValueType = typeName;
|
|
29
|
+
let optional = false;
|
|
30
|
+
let shortcut = false;
|
|
31
|
+
const specialChars = ["?", "!"];
|
|
32
|
+
while (specialChars.includes(expectedValueType[0])) {
|
|
33
|
+
const chr = expectedValueType[0];
|
|
34
|
+
expectedValueType = expectedValueType.slice(1);
|
|
35
|
+
if (chr === "?")
|
|
36
|
+
optional = true;
|
|
37
|
+
if (chr === "!")
|
|
38
|
+
shortcut = true;
|
|
39
|
+
}
|
|
40
|
+
while (specialChars.includes(expectedValueType[expectedValueType.length - 1])) {
|
|
41
|
+
const chr = expectedValueType[expectedValueType.length - 1];
|
|
42
|
+
expectedValueType = expectedValueType.slice(0, -1);
|
|
43
|
+
if (chr === "?")
|
|
44
|
+
optional = true;
|
|
45
|
+
if (chr === "!")
|
|
46
|
+
shortcut = true;
|
|
47
|
+
}
|
|
48
|
+
return { expectedValueType, optional, shortcut };
|
|
49
|
+
};
|
|
50
|
+
const isValueMatchPrimitive = (rec, key, typeValue, options) => {
|
|
51
|
+
if (typeValue === "skip")
|
|
52
|
+
return true;
|
|
53
|
+
const { optional, shortcut, expectedValueType } = trimTypeValue(typeValue);
|
|
54
|
+
if (options.shortcut && !shortcut)
|
|
55
|
+
return true;
|
|
56
|
+
if (key in rec && rec[key] !== undefined) {
|
|
57
|
+
const actualValueType = typeof rec[key];
|
|
58
|
+
if (actualValueType !== expectedValueType) {
|
|
59
|
+
PredicateError.expected(options.raise, key, `primitive type ${expectedValueType}`);
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
else if (!optional) {
|
|
64
|
+
PredicateError.mandatory(options.raise, key);
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
};
|
|
69
|
+
const isValueMatchClazz = (value, typeValue, key, options) => {
|
|
70
|
+
if (!(value instanceof typeValue.clazz)) {
|
|
71
|
+
const name = typeof typeValue.clazz === "function"
|
|
72
|
+
? typeValue.clazz.name
|
|
73
|
+
: "<something>";
|
|
74
|
+
PredicateError.expected(options.raise, key, `instance of ${name}`);
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
return true;
|
|
78
|
+
};
|
|
79
|
+
const isValueMatchPredicate = (value, typeValue, key, options) => {
|
|
80
|
+
try {
|
|
81
|
+
if (!typeValue.predicate(value, options)) {
|
|
82
|
+
PredicateError.predicateFailed(options.raise, key);
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
if (e instanceof PredicateError)
|
|
88
|
+
PredicateError.keyError(options.raise, key, e);
|
|
89
|
+
throw e;
|
|
90
|
+
}
|
|
91
|
+
return true;
|
|
92
|
+
};
|
|
93
|
+
const isValueMatchValue = (value, typeValue, key, options) => {
|
|
94
|
+
if (value !== typeValue.value) {
|
|
95
|
+
PredicateError.expected(options.raise, key, "value");
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
};
|
|
100
|
+
// Helper
|
|
101
|
+
const isValueMatchPresent = (value, typeValue, key, options) => {
|
|
102
|
+
if (options.shortcut && !typeValue.shortcut)
|
|
103
|
+
return true;
|
|
104
|
+
if ("clazz" in typeValue) {
|
|
105
|
+
if (!isValueMatchClazz(value, typeValue, key, options))
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
else if ("predicate" in typeValue) {
|
|
109
|
+
if (!isValueMatchPredicate(value, typeValue, key, options))
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
else if ("value" in typeValue) {
|
|
113
|
+
if (!isValueMatchValue(value, typeValue, key, options))
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
return true;
|
|
117
|
+
};
|
|
118
|
+
const isValueMatchAdvanced = (rec, key, typeValue, options) => {
|
|
119
|
+
if (options.shortcut && !typeValue.shortcut)
|
|
120
|
+
return true;
|
|
121
|
+
if (key in rec && rec[key] !== undefined) {
|
|
122
|
+
return isValueMatchPresent(rec[key], typeValue, key, options);
|
|
123
|
+
}
|
|
124
|
+
const result = Boolean(typeValue.optional);
|
|
125
|
+
if (!result)
|
|
126
|
+
PredicateError.mandatory(options.raise, key);
|
|
127
|
+
return result;
|
|
128
|
+
};
|
|
129
|
+
/** Check a single match between a profile and an actual object value */
|
|
130
|
+
const isValueMatch = (rec, key, typeValue, options) => {
|
|
131
|
+
if (typeof typeValue === "string")
|
|
132
|
+
return isValueMatchPrimitive(rec, key, typeValue, options);
|
|
133
|
+
if (typeof typeValue === "function") {
|
|
134
|
+
if (options.shortcut)
|
|
135
|
+
return true;
|
|
136
|
+
try {
|
|
137
|
+
const res = typeValue(rec[key], options);
|
|
138
|
+
if (!res)
|
|
139
|
+
PredicateError.predicateFailed(options.raise, key);
|
|
140
|
+
return res;
|
|
141
|
+
}
|
|
142
|
+
catch (e) {
|
|
143
|
+
if (e instanceof PredicateError)
|
|
144
|
+
PredicateError.keyError(options.raise, key, e);
|
|
145
|
+
throw e;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return isValueMatchAdvanced(rec, key, typeValue, options);
|
|
149
|
+
};
|
|
150
|
+
const checkArrayTypeDef = (valueType, rec, keyName, options) => {
|
|
151
|
+
let anyAccept = false;
|
|
152
|
+
const errors = [];
|
|
153
|
+
for (const typeInfo of valueType) {
|
|
154
|
+
try {
|
|
155
|
+
const res = isValueMatch(rec, keyName, typeInfo, options);
|
|
156
|
+
if (res) {
|
|
157
|
+
anyAccept = true;
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch (e) {
|
|
162
|
+
errors.push(asError(e));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (!anyAccept) {
|
|
166
|
+
PredicateError.allTryFailed(options.raise, keyName, errors);
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
return true;
|
|
170
|
+
};
|
|
171
|
+
/**
|
|
172
|
+
* Check if an object's properties fit the basic types profile.
|
|
173
|
+
*
|
|
174
|
+
* Only fit to check primitive types, equality and using other predicates.
|
|
175
|
+
* If `shortcut: true` is used in the options, only properties marked as "shortcut" are checked.
|
|
176
|
+
* For structured properties like instanceof and predicate this is indicated by the `shortcut`
|
|
177
|
+
* property; for primary type predicate this is indicated by the "!" character either before or
|
|
178
|
+
* after the type name.
|
|
179
|
+
*
|
|
180
|
+
* @public
|
|
181
|
+
*/
|
|
182
|
+
export const isObjectProfile = (obj, profile, options = false) => {
|
|
183
|
+
const opt = getPredicateOptions(options);
|
|
184
|
+
if (typeof obj !== "object" || obj === null) {
|
|
185
|
+
PredicateError.predicateFailed(opt.raise, "root");
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
const rec = obj;
|
|
189
|
+
for (const keyName of Object.keys(profile)) {
|
|
190
|
+
const valueType = profile[keyName];
|
|
191
|
+
if (Array.isArray(valueType)) {
|
|
192
|
+
const res = checkArrayTypeDef(valueType, rec, keyName, opt);
|
|
193
|
+
if (!res)
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
else if (!isValueMatch(rec, keyName, valueType, opt))
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
return true;
|
|
200
|
+
};
|
|
201
|
+
/**
|
|
202
|
+
* Create a predicate function based on the given profile.
|
|
203
|
+
*
|
|
204
|
+
* @public
|
|
205
|
+
*/
|
|
206
|
+
export const makeProfilePredicate = (profile) => (obj, options = false) => isObjectProfile(obj, profile, options);
|
|
207
|
+
/**
|
|
208
|
+
* Check if all of an object properties match a given type.
|
|
209
|
+
*
|
|
210
|
+
* @public
|
|
211
|
+
*/
|
|
212
|
+
export const isKeyValueOfType = (obj, typePredicate, options = false) => {
|
|
213
|
+
const opt = getPredicateOptions(options);
|
|
214
|
+
if (typeof obj !== "object") {
|
|
215
|
+
PredicateError.predicateFailed(opt.raise, undefined);
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
const rec = obj;
|
|
219
|
+
for (const key of Object.keys(rec)) {
|
|
220
|
+
try {
|
|
221
|
+
if (!typePredicate(rec[key], opt)) {
|
|
222
|
+
PredicateError.predicateFailed(opt.raise, key);
|
|
223
|
+
return false;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
catch (e) {
|
|
227
|
+
if (e instanceof PredicateError)
|
|
228
|
+
PredicateError.keyError(opt.raise, key, e);
|
|
229
|
+
throw e;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return true;
|
|
233
|
+
};
|
|
234
|
+
/** @public */
|
|
235
|
+
export const makeKeyValueOfTypePredicate = (typePredicate) => (obj, options = false) => isKeyValueOfType(obj, typePredicate, options);
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
export interface PredicateOptionsFull {
|
|
17
|
+
raise?: boolean;
|
|
18
|
+
shortcut?: boolean;
|
|
19
|
+
}
|
|
20
|
+
export type PredicateOptions = boolean | PredicateOptionsFull;
|
|
21
|
+
export type TypePredicate<T = unknown> = (obj: unknown, optionsOrRaise?: PredicateOptions) => obj is T;
|
|
22
|
+
/**
|
|
23
|
+
* Wrap the optional second argument of predicates into the full predicate options
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export declare const getPredicateOptions: (optionsOrRaise?: PredicateOptions) => Required<PredicateOptionsFull>;
|
|
28
|
+
/**
|
|
29
|
+
* Create a predicate that check in sequence multiple predicates and return true if one succeed.
|
|
30
|
+
*
|
|
31
|
+
* Used for construction similar to `type T = T1 | T2`
|
|
32
|
+
*/
|
|
33
|
+
export declare const makeOrPredicate: <T>(predicates: Array<TypePredicate>) => TypePredicate<T>;
|
|
34
|
+
/** Type used for something that can be either a promise or not */
|
|
35
|
+
export type Awaitable<T> = Promise<T> | T;
|
|
36
|
+
/** Ensure that the string match a given regex */
|
|
37
|
+
export declare const isMatchRegexPredicate: <T>(obj: unknown, regex: RegExp, options?: PredicateOptions) => obj is T;
|
|
38
|
+
/**
|
|
39
|
+
* Convert a type template string to a regex.
|
|
40
|
+
*
|
|
41
|
+
* The template string name can be any of:
|
|
42
|
+
*
|
|
43
|
+
* - string
|
|
44
|
+
* - number
|
|
45
|
+
* - bigint
|
|
46
|
+
* - boolean
|
|
47
|
+
* - null
|
|
48
|
+
* - undefined
|
|
49
|
+
*
|
|
50
|
+
* Unknown names will be picked from the `substitutions` parameter.
|
|
51
|
+
* Missing names will throw an error.
|
|
52
|
+
*
|
|
53
|
+
* @param template - The type template string.
|
|
54
|
+
* @param substitutions - An object for direct string substitutions of unknown keys from template
|
|
55
|
+
*/
|
|
56
|
+
export declare const makeTemplateRegex: (template: string, substitutions?: Record<string, Array<string>>) => RegExp;
|
|
57
|
+
/**
|
|
58
|
+
* Match a template type value.
|
|
59
|
+
*
|
|
60
|
+
* It is advisable to use `makeTemplatePredicate()` instead, as it will cache the computed regex.
|
|
61
|
+
*/
|
|
62
|
+
export declare const isTemplatePredicate: <T>(obj: unknown, template: string, substitutions: Record<string, Array<string>>, options?: PredicateOptions) => obj is T;
|
|
63
|
+
/** Create a predicate function that matches the given template type string. */
|
|
64
|
+
export declare const makeTemplatePredicate: <T>(template: string, substitutions?: Record<string, Array<string>>) => TypePredicate<T>;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
import { asError } from "../error.js";
|
|
17
|
+
import { PredicateError } from "./predicateerror.js";
|
|
18
|
+
/**
|
|
19
|
+
* Wrap the optional second argument of predicates into the full predicate options
|
|
20
|
+
*
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export const getPredicateOptions = (optionsOrRaise) => {
|
|
24
|
+
if (!optionsOrRaise)
|
|
25
|
+
return { raise: false, shortcut: false };
|
|
26
|
+
if (typeof optionsOrRaise === "boolean")
|
|
27
|
+
return { raise: optionsOrRaise, shortcut: false };
|
|
28
|
+
return { raise: false, shortcut: false, ...optionsOrRaise };
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Create a predicate that check in sequence multiple predicates and return true if one succeed.
|
|
32
|
+
*
|
|
33
|
+
* Used for construction similar to `type T = T1 | T2`
|
|
34
|
+
*/
|
|
35
|
+
export const makeOrPredicate = (predicates) => (obj, options = false) => {
|
|
36
|
+
const opt = getPredicateOptions(options);
|
|
37
|
+
const errors = [];
|
|
38
|
+
for (const predicate of predicates) {
|
|
39
|
+
try {
|
|
40
|
+
if (predicate(obj, { raise: true, shortcut: opt.shortcut }))
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
errors.push(asError(e));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (opt.raise)
|
|
48
|
+
throw new PredicateError({ cause: errors });
|
|
49
|
+
return false;
|
|
50
|
+
};
|
|
51
|
+
/** Ensure that the string match a given regex */
|
|
52
|
+
export const isMatchRegexPredicate = (obj, regex, options = false) => {
|
|
53
|
+
const opt = getPredicateOptions(options);
|
|
54
|
+
if (typeof obj !== "string" || !regex.exec(obj)) {
|
|
55
|
+
PredicateError.predicateFailed(opt.raise, undefined);
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
return true;
|
|
59
|
+
};
|
|
60
|
+
/** Return a regex portion for a choice of strings */
|
|
61
|
+
const regexForStringChoice = (choices) => `(${choices.map((c) => c.replaceAll("|", "\\|").replaceAll(")", "\\)")).join("|")})`;
|
|
62
|
+
/** Substitutions for template types */
|
|
63
|
+
const defaultSubstitutions = {
|
|
64
|
+
bigint: "-?\\d+",
|
|
65
|
+
boolean: regexForStringChoice(["true", "false"]),
|
|
66
|
+
null: "null",
|
|
67
|
+
number: "[+-]?(?:\\d+\\.?\\d*|\\d*\\.?\\d+)",
|
|
68
|
+
string: ".*",
|
|
69
|
+
undefined: "undefined",
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Convert a type template string to a regex.
|
|
73
|
+
*
|
|
74
|
+
* The template string name can be any of:
|
|
75
|
+
*
|
|
76
|
+
* - string
|
|
77
|
+
* - number
|
|
78
|
+
* - bigint
|
|
79
|
+
* - boolean
|
|
80
|
+
* - null
|
|
81
|
+
* - undefined
|
|
82
|
+
*
|
|
83
|
+
* Unknown names will be picked from the `substitutions` parameter.
|
|
84
|
+
* Missing names will throw an error.
|
|
85
|
+
*
|
|
86
|
+
* @param template - The type template string.
|
|
87
|
+
* @param substitutions - An object for direct string substitutions of unknown keys from template
|
|
88
|
+
*/
|
|
89
|
+
export const makeTemplateRegex = (template, substitutions) => {
|
|
90
|
+
const regexStr = template.replace(/\$\{(?<inputText>.+?)\}/gu, (_, inputText) => {
|
|
91
|
+
if (inputText in defaultSubstitutions)
|
|
92
|
+
return defaultSubstitutions[inputText];
|
|
93
|
+
if (substitutions && inputText in substitutions) {
|
|
94
|
+
return regexForStringChoice(substitutions[inputText]);
|
|
95
|
+
}
|
|
96
|
+
throw new Error(`Unknown substitution "${inputText}" in template`);
|
|
97
|
+
});
|
|
98
|
+
return new RegExp(`^${regexStr}$`, "u");
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Match a template type value.
|
|
102
|
+
*
|
|
103
|
+
* It is advisable to use `makeTemplatePredicate()` instead, as it will cache the computed regex.
|
|
104
|
+
*/
|
|
105
|
+
export const isTemplatePredicate = (obj, template, substitutions, options = false) => {
|
|
106
|
+
if (typeof obj !== "string")
|
|
107
|
+
return false;
|
|
108
|
+
const regex = makeTemplateRegex(template, substitutions);
|
|
109
|
+
return isMatchRegexPredicate(obj, regex, options);
|
|
110
|
+
};
|
|
111
|
+
/** Create a predicate function that matches the given template type string. */
|
|
112
|
+
export const makeTemplatePredicate = (template, substitutions) => {
|
|
113
|
+
const regex = makeTemplateRegex(template, substitutions);
|
|
114
|
+
return (obj, options = false) => isMatchRegexPredicate(obj, regex, options);
|
|
115
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* @preserve
|
|
4
|
+
*
|
|
5
|
+
* MIT License
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2023 KeeeX SAS
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
12
|
+
*
|
|
13
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
import ms from "ms";
|
|
17
|
+
/** Check that a value is suitable for input for `ms()` */
|
|
18
|
+
export declare const isMsInput: import("./types.js").TypePredicate<number | ms.StringValue>;
|