@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,213 @@
|
|
|
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 === "?") optional = true;
|
|
36
|
+
if (chr === "!") shortcut = true;
|
|
37
|
+
}
|
|
38
|
+
while (specialChars.includes(expectedValueType[expectedValueType.length - 1])) {
|
|
39
|
+
const chr = expectedValueType[expectedValueType.length - 1];
|
|
40
|
+
expectedValueType = expectedValueType.slice(0, -1);
|
|
41
|
+
if (chr === "?") optional = true;
|
|
42
|
+
if (chr === "!") shortcut = true;
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
expectedValueType,
|
|
46
|
+
optional,
|
|
47
|
+
shortcut
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
const isValueMatchPrimitive = (rec, key, typeValue, options) => {
|
|
51
|
+
if (typeValue === "skip") return true;
|
|
52
|
+
const {
|
|
53
|
+
optional,
|
|
54
|
+
shortcut,
|
|
55
|
+
expectedValueType
|
|
56
|
+
} = trimTypeValue(typeValue);
|
|
57
|
+
if (options.shortcut && !shortcut) return true;
|
|
58
|
+
if (key in rec && rec[key] !== undefined) {
|
|
59
|
+
const actualValueType = typeof rec[key];
|
|
60
|
+
if (actualValueType !== expectedValueType) {
|
|
61
|
+
PredicateError.expected(options.raise, key, `primitive type ${expectedValueType}`);
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
} else if (!optional) {
|
|
65
|
+
PredicateError.mandatory(options.raise, key);
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
};
|
|
70
|
+
const isValueMatchClazz = (value, typeValue, key, options) => {
|
|
71
|
+
if (!(value instanceof typeValue.clazz)) {
|
|
72
|
+
const name = typeof typeValue.clazz === "function" ? typeValue.clazz.name : "<something>";
|
|
73
|
+
PredicateError.expected(options.raise, key, `instance of ${name}`);
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
return true;
|
|
77
|
+
};
|
|
78
|
+
const isValueMatchPredicate = (value, typeValue, key, options) => {
|
|
79
|
+
try {
|
|
80
|
+
if (!typeValue.predicate(value, options)) {
|
|
81
|
+
PredicateError.predicateFailed(options.raise, key);
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
} catch (e) {
|
|
85
|
+
if (e instanceof PredicateError) PredicateError.keyError(options.raise, key, e);
|
|
86
|
+
throw e;
|
|
87
|
+
}
|
|
88
|
+
return true;
|
|
89
|
+
};
|
|
90
|
+
const isValueMatchValue = (value, typeValue, key, options) => {
|
|
91
|
+
if (value !== typeValue.value) {
|
|
92
|
+
PredicateError.expected(options.raise, key, "value");
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
return true;
|
|
96
|
+
};
|
|
97
|
+
// Helper
|
|
98
|
+
const isValueMatchPresent = (value, typeValue, key, options) => {
|
|
99
|
+
if (options.shortcut && !typeValue.shortcut) return true;
|
|
100
|
+
if ("clazz" in typeValue) {
|
|
101
|
+
if (!isValueMatchClazz(value, typeValue, key, options)) return false;
|
|
102
|
+
} else if ("predicate" in typeValue) {
|
|
103
|
+
if (!isValueMatchPredicate(value, typeValue, key, options)) return false;
|
|
104
|
+
} else if ("value" in typeValue) {
|
|
105
|
+
if (!isValueMatchValue(value, typeValue, key, options)) return false;
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
};
|
|
109
|
+
const isValueMatchAdvanced = (rec, key, typeValue, options) => {
|
|
110
|
+
if (options.shortcut && !typeValue.shortcut) return true;
|
|
111
|
+
if (key in rec && rec[key] !== undefined) {
|
|
112
|
+
return isValueMatchPresent(rec[key], typeValue, key, options);
|
|
113
|
+
}
|
|
114
|
+
const result = Boolean(typeValue.optional);
|
|
115
|
+
if (!result) PredicateError.mandatory(options.raise, key);
|
|
116
|
+
return result;
|
|
117
|
+
};
|
|
118
|
+
/** Check a single match between a profile and an actual object value */
|
|
119
|
+
const isValueMatch = (rec, key, typeValue, options) => {
|
|
120
|
+
if (typeof typeValue === "string") return isValueMatchPrimitive(rec, key, typeValue, options);
|
|
121
|
+
if (typeof typeValue === "function") {
|
|
122
|
+
if (options.shortcut) return true;
|
|
123
|
+
try {
|
|
124
|
+
const res = typeValue(rec[key], options);
|
|
125
|
+
if (!res) PredicateError.predicateFailed(options.raise, key);
|
|
126
|
+
return res;
|
|
127
|
+
} catch (e) {
|
|
128
|
+
if (e instanceof PredicateError) PredicateError.keyError(options.raise, key, e);
|
|
129
|
+
throw e;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return isValueMatchAdvanced(rec, key, typeValue, options);
|
|
133
|
+
};
|
|
134
|
+
const checkArrayTypeDef = (valueType, rec, keyName, options) => {
|
|
135
|
+
let anyAccept = false;
|
|
136
|
+
const errors = [];
|
|
137
|
+
for (const typeInfo of valueType) {
|
|
138
|
+
try {
|
|
139
|
+
const res = isValueMatch(rec, keyName, typeInfo, options);
|
|
140
|
+
if (res) {
|
|
141
|
+
anyAccept = true;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
} catch (e) {
|
|
145
|
+
errors.push(asError(e));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
if (!anyAccept) {
|
|
149
|
+
PredicateError.allTryFailed(options.raise, keyName, errors);
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
return true;
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* Check if an object's properties fit the basic types profile.
|
|
156
|
+
*
|
|
157
|
+
* Only fit to check primitive types, equality and using other predicates.
|
|
158
|
+
* If `shortcut: true` is used in the options, only properties marked as "shortcut" are checked.
|
|
159
|
+
* For structured properties like instanceof and predicate this is indicated by the `shortcut`
|
|
160
|
+
* property; for primary type predicate this is indicated by the "!" character either before or
|
|
161
|
+
* after the type name.
|
|
162
|
+
*
|
|
163
|
+
* @public
|
|
164
|
+
*/
|
|
165
|
+
export const isObjectProfile = (obj, profile, options = false) => {
|
|
166
|
+
const opt = getPredicateOptions(options);
|
|
167
|
+
if (typeof obj !== "object" || obj === null) {
|
|
168
|
+
PredicateError.predicateFailed(opt.raise, "root");
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
const rec = obj;
|
|
172
|
+
for (const keyName of Object.keys(profile)) {
|
|
173
|
+
const valueType = profile[keyName];
|
|
174
|
+
if (Array.isArray(valueType)) {
|
|
175
|
+
const res = checkArrayTypeDef(valueType, rec, keyName, opt);
|
|
176
|
+
if (!res) return false;
|
|
177
|
+
} else if (!isValueMatch(rec, keyName, valueType, opt)) return false;
|
|
178
|
+
}
|
|
179
|
+
return true;
|
|
180
|
+
};
|
|
181
|
+
/**
|
|
182
|
+
* Create a predicate function based on the given profile.
|
|
183
|
+
*
|
|
184
|
+
* @public
|
|
185
|
+
*/
|
|
186
|
+
export const makeProfilePredicate = profile => (obj, options = false) => isObjectProfile(obj, profile, options);
|
|
187
|
+
/**
|
|
188
|
+
* Check if all of an object properties match a given type.
|
|
189
|
+
*
|
|
190
|
+
* @public
|
|
191
|
+
*/
|
|
192
|
+
export const isKeyValueOfType = (obj, typePredicate, options = false) => {
|
|
193
|
+
const opt = getPredicateOptions(options);
|
|
194
|
+
if (typeof obj !== "object") {
|
|
195
|
+
PredicateError.predicateFailed(opt.raise, undefined);
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
const rec = obj;
|
|
199
|
+
for (const key of Object.keys(rec)) {
|
|
200
|
+
try {
|
|
201
|
+
if (!typePredicate(rec[key], opt)) {
|
|
202
|
+
PredicateError.predicateFailed(opt.raise, key);
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
} catch (e) {
|
|
206
|
+
if (e instanceof PredicateError) PredicateError.keyError(opt.raise, key, e);
|
|
207
|
+
throw e;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return true;
|
|
211
|
+
};
|
|
212
|
+
/** @public */
|
|
213
|
+
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,123 @@
|
|
|
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) return {
|
|
25
|
+
raise: false,
|
|
26
|
+
shortcut: false
|
|
27
|
+
};
|
|
28
|
+
if (typeof optionsOrRaise === "boolean") return {
|
|
29
|
+
raise: optionsOrRaise,
|
|
30
|
+
shortcut: false
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
raise: false,
|
|
34
|
+
shortcut: false,
|
|
35
|
+
...optionsOrRaise
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Create a predicate that check in sequence multiple predicates and return true if one succeed.
|
|
40
|
+
*
|
|
41
|
+
* Used for construction similar to `type T = T1 | T2`
|
|
42
|
+
*/
|
|
43
|
+
export const makeOrPredicate = predicates => (obj, options = false) => {
|
|
44
|
+
const opt = getPredicateOptions(options);
|
|
45
|
+
const errors = [];
|
|
46
|
+
for (const predicate of predicates) {
|
|
47
|
+
try {
|
|
48
|
+
if (predicate(obj, {
|
|
49
|
+
raise: true,
|
|
50
|
+
shortcut: opt.shortcut
|
|
51
|
+
})) return true;
|
|
52
|
+
} catch (e) {
|
|
53
|
+
errors.push(asError(e));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (opt.raise) throw new PredicateError({
|
|
57
|
+
cause: errors
|
|
58
|
+
});
|
|
59
|
+
return false;
|
|
60
|
+
};
|
|
61
|
+
/** Ensure that the string match a given regex */
|
|
62
|
+
export const isMatchRegexPredicate = (obj, regex, options = false) => {
|
|
63
|
+
const opt = getPredicateOptions(options);
|
|
64
|
+
if (typeof obj !== "string" || !regex.exec(obj)) {
|
|
65
|
+
PredicateError.predicateFailed(opt.raise, undefined);
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
};
|
|
70
|
+
/** Return a regex portion for a choice of strings */
|
|
71
|
+
const regexForStringChoice = choices => `(${choices.map(c => c.replaceAll("|", "\\|").replaceAll(")", "\\)")).join("|")})`;
|
|
72
|
+
/** Substitutions for template types */
|
|
73
|
+
const defaultSubstitutions = {
|
|
74
|
+
bigint: "-?\\d+",
|
|
75
|
+
boolean: regexForStringChoice(["true", "false"]),
|
|
76
|
+
null: "null",
|
|
77
|
+
number: "[+-]?(?:\\d+\\.?\\d*|\\d*\\.?\\d+)",
|
|
78
|
+
string: ".*",
|
|
79
|
+
undefined: "undefined"
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* Convert a type template string to a regex.
|
|
83
|
+
*
|
|
84
|
+
* The template string name can be any of:
|
|
85
|
+
*
|
|
86
|
+
* - string
|
|
87
|
+
* - number
|
|
88
|
+
* - bigint
|
|
89
|
+
* - boolean
|
|
90
|
+
* - null
|
|
91
|
+
* - undefined
|
|
92
|
+
*
|
|
93
|
+
* Unknown names will be picked from the `substitutions` parameter.
|
|
94
|
+
* Missing names will throw an error.
|
|
95
|
+
*
|
|
96
|
+
* @param template - The type template string.
|
|
97
|
+
* @param substitutions - An object for direct string substitutions of unknown keys from template
|
|
98
|
+
*/
|
|
99
|
+
export const makeTemplateRegex = (template, substitutions) => {
|
|
100
|
+
const regexStr = template.replace(/\$\{(?<inputText>.+?)\}/gu, (_, inputText) => {
|
|
101
|
+
if (inputText in defaultSubstitutions) return defaultSubstitutions[inputText];
|
|
102
|
+
if (substitutions && inputText in substitutions) {
|
|
103
|
+
return regexForStringChoice(substitutions[inputText]);
|
|
104
|
+
}
|
|
105
|
+
throw new Error(`Unknown substitution "${inputText}" in template`);
|
|
106
|
+
});
|
|
107
|
+
return new RegExp(`^${regexStr}$`, "u");
|
|
108
|
+
};
|
|
109
|
+
/**
|
|
110
|
+
* Match a template type value.
|
|
111
|
+
*
|
|
112
|
+
* It is advisable to use `makeTemplatePredicate()` instead, as it will cache the computed regex.
|
|
113
|
+
*/
|
|
114
|
+
export const isTemplatePredicate = (obj, template, substitutions, options = false) => {
|
|
115
|
+
if (typeof obj !== "string") return false;
|
|
116
|
+
const regex = makeTemplateRegex(template, substitutions);
|
|
117
|
+
return isMatchRegexPredicate(obj, regex, options);
|
|
118
|
+
};
|
|
119
|
+
/** Create a predicate function that matches the given template type string. */
|
|
120
|
+
export const makeTemplatePredicate = (template, substitutions) => {
|
|
121
|
+
const regex = makeTemplateRegex(template, substitutions);
|
|
122
|
+
return (obj, options = false) => isMatchRegexPredicate(obj, regex, options);
|
|
123
|
+
};
|
|
@@ -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>;
|
|
@@ -0,0 +1,30 @@
|
|
|
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 { isNumber } from "./primitive.js";
|
|
17
|
+
import { makeOrPredicate, makeTemplatePredicate } from "./types.js";
|
|
18
|
+
/** All `ms()` units, extracted from type definition */
|
|
19
|
+
const msBaseUnits = ["Years", "Year", "Yrs", "Yr", "Y", "Weeks", "Week", "W", "Days", "Day", "D", "Hours", "Hour", "Hrs", "Hr", "H", "Minutes", "Minute", "Mins", "Min", "M", "Seconds", "Second", "Secs", "Sec", "s", "Milliseconds", "Millisecond", "Msecs", "Msec", "Ms"];
|
|
20
|
+
/** All `ms()` units, including lowercase and uppercase */
|
|
21
|
+
const msUnitAnyCase = Array.from(new Set([...msBaseUnits, ...msBaseUnits.map(c => c.toUpperCase()), ...msBaseUnits.map(c => c.toLowerCase())]));
|
|
22
|
+
const isMsStrInput = makeTemplatePredicate("${number}");
|
|
23
|
+
const isMsStrUnitPredicate = makeTemplatePredicate("${number}${msUnitAnyCase}", {
|
|
24
|
+
msUnitAnyCase
|
|
25
|
+
});
|
|
26
|
+
const isMsSpaceUnitPredicate = makeTemplatePredicate("${number} ${msUnitAnyCase}", {
|
|
27
|
+
msUnitAnyCase
|
|
28
|
+
});
|
|
29
|
+
/** Check that a value is suitable for input for `ms()` */
|
|
30
|
+
export const isMsInput = makeOrPredicate([isNumber, isMsStrInput, isMsStrUnitPredicate, isMsSpaceUnitPredicate]);
|
|
@@ -0,0 +1,176 @@
|
|
|
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 { pureBuf82B64, buf82b64 } from "./bits/uint8array.js";
|
|
17
|
+
/**
|
|
18
|
+
* Return a Uint8Array view on the provided ArrayBuffer.
|
|
19
|
+
*
|
|
20
|
+
* If clone is set to true, return a clone of the source array. Otherwise a view is returned.
|
|
21
|
+
*
|
|
22
|
+
* @public
|
|
23
|
+
*/
|
|
24
|
+
export declare const getUint8Array: (arrayBuffer: ArrayBuffer | Uint8Array, offset?: number, length?: number, clone?: boolean) => Uint8Array;
|
|
25
|
+
/**
|
|
26
|
+
* Convert an Uint8Array to a raw string.
|
|
27
|
+
*
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
export declare const buf82str: (buf8: Uint8Array) => string;
|
|
31
|
+
/**
|
|
32
|
+
* Convert a string to an Uint8Array
|
|
33
|
+
*
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
export declare const str2buf8: (str: string) => Uint8Array;
|
|
37
|
+
/** @internal */
|
|
38
|
+
export declare const pureBuf82Hex: (buf8: Uint8Array, addPrefix?: boolean) => string;
|
|
39
|
+
/**
|
|
40
|
+
* Convert an Uint8Array to a hex string.
|
|
41
|
+
*
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
export declare const buf82hex: (buf8: Uint8Array, addPrefix?: boolean) => string;
|
|
45
|
+
/** @internal */
|
|
46
|
+
export declare const pureHex2Buf8: (hex: string) => Uint8Array;
|
|
47
|
+
/**
|
|
48
|
+
* Convert an hex string to an Uint8Array.
|
|
49
|
+
*
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
52
|
+
export declare const hex2buf8: (hex: string) => Uint8Array;
|
|
53
|
+
/**
|
|
54
|
+
* Concatenate multiple Uint8Array into one output.
|
|
55
|
+
*
|
|
56
|
+
* The output will have each individual buffer length stored so that they can be
|
|
57
|
+
* split afterward using explodeBuf8List(). Simple concatenation is available with merge().
|
|
58
|
+
*
|
|
59
|
+
* Output have the following format for each buffer:
|
|
60
|
+
*
|
|
61
|
+
* - 1 byte: the character "!", that identifies the kind of data that follows
|
|
62
|
+
* - 1 byte: length of the "buffer length as a string(base36)"
|
|
63
|
+
* - x bytes: buffer length as a string(base36)
|
|
64
|
+
* - y bytes: buffer content
|
|
65
|
+
*
|
|
66
|
+
* A special byte set to '#' can be appended to indicate the end of the buffers list. This allows
|
|
67
|
+
* the output of this function to be followed by other data while still being able to use the
|
|
68
|
+
* explode function.
|
|
69
|
+
*
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
72
|
+
export declare const concatenateBuf8List: (buf8s: Array<Uint8Array>, addEndMarker?: boolean) => Uint8Array;
|
|
73
|
+
/**
|
|
74
|
+
* Separate Uint8Array previously merged using concatenateBuf8List().
|
|
75
|
+
*
|
|
76
|
+
* Each buffer prefixed by a "!" uses the format described in `concatenateBuf8List()`.
|
|
77
|
+
* If a buffer is not prefixed by a "!", it uses the old format:
|
|
78
|
+
*
|
|
79
|
+
* - x bytes: the length of the buffer, as an hexadecimal string
|
|
80
|
+
* - 1 bytes: the "," character
|
|
81
|
+
* - y bytes: buffer content
|
|
82
|
+
*
|
|
83
|
+
* As a special case, if the first byte read for the next buffer is "#", which is an impossible
|
|
84
|
+
* character for both the old and new format, it is interpreted as the end of the buffer list.
|
|
85
|
+
*
|
|
86
|
+
* @public
|
|
87
|
+
*/
|
|
88
|
+
export declare const explodeBuf8List: (mergedBuf8s: Uint8Array) => Array<Uint8Array>;
|
|
89
|
+
/**
|
|
90
|
+
* Return the length of the buffer list, up to either the end marker or the buffer size.
|
|
91
|
+
*
|
|
92
|
+
* @public
|
|
93
|
+
*/
|
|
94
|
+
export declare const concatenatedBuf8ListSize: (mergedBuf8s: Uint8Array) => number;
|
|
95
|
+
/**
|
|
96
|
+
* Merge Uint8Arrays into one.
|
|
97
|
+
*
|
|
98
|
+
* This performs a simple concatenation;
|
|
99
|
+
*
|
|
100
|
+
* @public
|
|
101
|
+
*/
|
|
102
|
+
export declare const merge: (buf8s: Array<Uint8Array>) => Uint8Array;
|
|
103
|
+
/** @internal */
|
|
104
|
+
export declare const pureB642Buf8: (b64: string, strict?: boolean) => Uint8Array;
|
|
105
|
+
/**
|
|
106
|
+
* Convert a B64 string to an Uint8Array.
|
|
107
|
+
*
|
|
108
|
+
* @public
|
|
109
|
+
*/
|
|
110
|
+
export declare const b642buf8: (b64: string, strict?: boolean) => Uint8Array;
|
|
111
|
+
/** @internal */
|
|
112
|
+
export declare const pureUtf82Buf8: (str: string) => Uint8Array;
|
|
113
|
+
/**
|
|
114
|
+
* Convert an UTF-8 string into an Uint8Array.
|
|
115
|
+
*
|
|
116
|
+
* @public
|
|
117
|
+
*/
|
|
118
|
+
export declare const utf82buf8: (utf8: string) => Uint8Array;
|
|
119
|
+
/** @internal */
|
|
120
|
+
export declare const pureBuf82Utf8: (buf8: Uint8Array) => string;
|
|
121
|
+
/**
|
|
122
|
+
* Convert an Uint8Array into an UTF-8 string
|
|
123
|
+
*
|
|
124
|
+
* @public
|
|
125
|
+
*/
|
|
126
|
+
export declare const buf82utf8: (buf8: Uint8Array) => string;
|
|
127
|
+
/**
|
|
128
|
+
* Check that the content of two Uint8Array is equal.
|
|
129
|
+
*
|
|
130
|
+
* @public
|
|
131
|
+
*/
|
|
132
|
+
export declare const buf8Equal: (op1: Uint8Array, op2: Uint8Array) => boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Find the position of a string in a buffer
|
|
135
|
+
*
|
|
136
|
+
* @param needle - String to find. Only work with 1 byte-wide characters.
|
|
137
|
+
*
|
|
138
|
+
* @param ab - Buffer to look into
|
|
139
|
+
*
|
|
140
|
+
* @param startPosition - Starting index to look from
|
|
141
|
+
*
|
|
142
|
+
* @returns
|
|
143
|
+
* The position of the string in the buffer. If not found returns -1
|
|
144
|
+
*
|
|
145
|
+
* @public
|
|
146
|
+
*/
|
|
147
|
+
export declare const findString: (needle: string, buf8: Uint8Array, startPosition?: number) => number;
|
|
148
|
+
/**
|
|
149
|
+
* Find the position of a string in a buffer from the end
|
|
150
|
+
*
|
|
151
|
+
* @param needle - String to find. Only work with 1 byte characters.
|
|
152
|
+
*
|
|
153
|
+
* @param ab - Buffer to look into
|
|
154
|
+
*
|
|
155
|
+
* @param startPosition - Starting index to look from
|
|
156
|
+
*
|
|
157
|
+
* @param endPosition - Last index to look from. The needle must fit completely before this.
|
|
158
|
+
* -1 mean end of the file.
|
|
159
|
+
*
|
|
160
|
+
* @returns
|
|
161
|
+
* The position of the string in the buffer. If not found returns -1
|
|
162
|
+
*
|
|
163
|
+
* @public
|
|
164
|
+
*/
|
|
165
|
+
export declare const findLastString: (needle: string, buf8: Uint8Array, startPosition?: number, endPosition?: number) => number;
|
|
166
|
+
/**
|
|
167
|
+
* Check if a byte sequence looks like a valid UTF-8 sequence that *does* have wide characters.
|
|
168
|
+
*
|
|
169
|
+
* https://en.wikipedia.org/wiki/UTF-8
|
|
170
|
+
*
|
|
171
|
+
* @returns
|
|
172
|
+
* `true` if the byte sequence does contain UTF-8 sequences.
|
|
173
|
+
* Regular ASCII string will return `false` as they won't contain such sequence.
|
|
174
|
+
*/
|
|
175
|
+
export declare const isUTF8Sequence: (bytes: Uint8Array) => boolean;
|
|
176
|
+
export { buf82b64, pureBuf82B64 };
|