@strictly/base 0.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/.eslintrc.cjs +26 -0
- package/.out/errors/not_implemented.d.ts +3 -0
- package/.out/errors/not_implemented.js +5 -0
- package/.out/errors/unexpected_implementation.d.ts +3 -0
- package/.out/errors/unexpected_implementation.js +5 -0
- package/.out/errors/unreachable.d.ts +3 -0
- package/.out/errors/unreachable.js +6 -0
- package/.out/index.d.ts +20 -0
- package/.out/index.js +20 -0
- package/.out/test/index.d.ts +1 -0
- package/.out/test/index.js +1 -0
- package/.out/test/vitest/expect.d.ts +4 -0
- package/.out/test/vitest/expect.js +13 -0
- package/.out/tsconfig.json +16 -0
- package/.out/tsconfig.tsbuildinfo +1 -0
- package/.out/tsup.config.d.ts +3 -0
- package/.out/tsup.config.js +12 -0
- package/.out/types/element_of_array.d.ts +1 -0
- package/.out/types/element_of_array.js +1 -0
- package/.out/types/extract_generics.d.ts +2 -0
- package/.out/types/extract_generics.js +1 -0
- package/.out/types/is_field_optional.d.ts +1 -0
- package/.out/types/is_field_optional.js +1 -0
- package/.out/types/is_field_readonly.d.ts +8 -0
- package/.out/types/is_field_readonly.js +2 -0
- package/.out/types/maybe.d.ts +3 -0
- package/.out/types/maybe.js +1 -0
- package/.out/types/printable_of.d.ts +1 -0
- package/.out/types/printable_of.js +1 -0
- package/.out/types/required_of_record.d.ts +3 -0
- package/.out/types/required_of_record.js +1 -0
- package/.out/types/specs/element_of_array.tests.d.ts +1 -0
- package/.out/types/specs/element_of_array.tests.js +6 -0
- package/.out/types/specs/is_field_readonly.tests.d.ts +1 -0
- package/.out/types/specs/is_field_readonly.tests.js +9 -0
- package/.out/types/specs/printable_of.tests.d.ts +1 -0
- package/.out/types/specs/printable_of.tests.js +6 -0
- package/.out/types/specs/required_of_record.tests.d.ts +1 -0
- package/.out/types/specs/required_of_record.tests.js +12 -0
- package/.out/types/specs/string_key_of.tests.d.ts +1 -0
- package/.out/types/specs/string_key_of.tests.js +10 -0
- package/.out/types/string_key_of.d.ts +1 -0
- package/.out/types/string_key_of.js +1 -0
- package/.out/util/array.d.ts +1 -0
- package/.out/util/array.js +8 -0
- package/.out/util/cache.d.ts +13 -0
- package/.out/util/cache.js +42 -0
- package/.out/util/delay.d.ts +12 -0
- package/.out/util/delay.js +32 -0
- package/.out/util/format.d.ts +2 -0
- package/.out/util/format.js +11 -0
- package/.out/util/json.d.ts +1 -0
- package/.out/util/json.js +11 -0
- package/.out/util/poll.d.ts +8 -0
- package/.out/util/poll.js +23 -0
- package/.out/util/preconditions.d.ts +11 -0
- package/.out/util/preconditions.js +43 -0
- package/.out/util/promises.d.ts +1 -0
- package/.out/util/promises.js +11 -0
- package/.out/util/record.d.ts +10 -0
- package/.out/util/record.js +64 -0
- package/.out/util/specs/cache.tests.d.ts +1 -0
- package/.out/util/specs/cache.tests.js +49 -0
- package/.out/util/specs/format.tests.d.ts +1 -0
- package/.out/util/specs/format.tests.js +67 -0
- package/.out/util/specs/poll.tests.d.ts +1 -0
- package/.out/util/specs/poll.tests.js +47 -0
- package/.out/vitest.workspace.d.ts +2 -0
- package/.out/vitest.workspace.js +7 -0
- package/.turbo/turbo-build.log +18 -0
- package/.turbo/turbo-check-types.log +3 -0
- package/.turbo/turbo-release$colon$exports.log +3 -0
- package/README.md +3 -0
- package/dist/index.cjs +387 -0
- package/dist/index.d.cts +109 -0
- package/dist/index.d.ts +109 -0
- package/dist/index.js +328 -0
- package/errors/not_implemented.ts +5 -0
- package/errors/unexpected_implementation.ts +5 -0
- package/errors/unreachable.ts +6 -0
- package/index.ts +20 -0
- package/package.exports.json +18 -0
- package/package.json +50 -0
- package/test/index.ts +1 -0
- package/test/vitest/expect.ts +16 -0
- package/tsconfig.build.json +11 -0
- package/tsconfig.json +16 -0
- package/tsup.config.ts +16 -0
- package/types/element_of_array.ts +1 -0
- package/types/extract_generics.ts +2 -0
- package/types/is_field_optional.ts +5 -0
- package/types/is_field_readonly.ts +8 -0
- package/types/maybe.ts +5 -0
- package/types/printable_of.ts +1 -0
- package/types/required_of_record.ts +4 -0
- package/types/specs/element_of_array.tests.ts +9 -0
- package/types/specs/is_field_readonly.tests.ts +15 -0
- package/types/specs/printable_of.tests.ts +8 -0
- package/types/specs/required_of_record.tests.ts +16 -0
- package/types/specs/string_key_of.tests.ts +16 -0
- package/types/string_key_of.ts +1 -0
- package/util/array.ts +12 -0
- package/util/cache.ts +52 -0
- package/util/delay.ts +36 -0
- package/util/format.ts +23 -0
- package/util/json.ts +10 -0
- package/util/poll.ts +40 -0
- package/util/preconditions.ts +83 -0
- package/util/promises.ts +11 -0
- package/util/record.ts +133 -0
- package/util/specs/cache.tests.ts +68 -0
- package/util/specs/format.tests.ts +70 -0
- package/util/specs/poll.tests.ts +75 -0
- package/vitest.workspace.ts +11 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
Cache: () => Cache,
|
|
24
|
+
NotImplementedError: () => NotImplementedError,
|
|
25
|
+
PreconditionFailedError: () => PreconditionFailedError,
|
|
26
|
+
UnexpectedImplementationError: () => UnexpectedImplementationError,
|
|
27
|
+
UnreachableError: () => UnreachableError,
|
|
28
|
+
assertEqual: () => assertEqual,
|
|
29
|
+
assertExists: () => assertExists,
|
|
30
|
+
assertExistsAndReturn: () => assertExistsAndReturn,
|
|
31
|
+
assertIs: () => assertIs,
|
|
32
|
+
assertState: () => assertState,
|
|
33
|
+
asyncReduce: () => asyncReduce,
|
|
34
|
+
callAsPromise: () => callAsPromise,
|
|
35
|
+
checkUnary: () => checkUnary,
|
|
36
|
+
checkValidNumber: () => checkValidNumber,
|
|
37
|
+
constantPollInterval: () => constantPollInterval,
|
|
38
|
+
createDelay: () => createDelay,
|
|
39
|
+
createWarmupDelay: () => createWarmupDelay,
|
|
40
|
+
delay: () => delay,
|
|
41
|
+
errorHandlingJsonParse: () => errorHandlingJsonParse,
|
|
42
|
+
expectDefined: () => expectDefined,
|
|
43
|
+
expectDefinedAndReturn: () => expectDefinedAndReturn,
|
|
44
|
+
expectEquals: () => expectEquals,
|
|
45
|
+
expectTruthy: () => expectTruthy,
|
|
46
|
+
forEach: () => forEach,
|
|
47
|
+
format: () => format,
|
|
48
|
+
map: () => map,
|
|
49
|
+
poll: () => poll,
|
|
50
|
+
reduce: () => reduce,
|
|
51
|
+
reverse: () => reverse,
|
|
52
|
+
rollup: () => rollup,
|
|
53
|
+
secondDelay: () => secondDelay,
|
|
54
|
+
toArray: () => toArray,
|
|
55
|
+
union: () => union
|
|
56
|
+
});
|
|
57
|
+
module.exports = __toCommonJS(index_exports);
|
|
58
|
+
|
|
59
|
+
// errors/not_implemented.ts
|
|
60
|
+
var NotImplementedError = class extends Error {
|
|
61
|
+
constructor(name) {
|
|
62
|
+
super(`${name} not implemented`);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// errors/unexpected_implementation.ts
|
|
67
|
+
var UnexpectedImplementationError = class extends Error {
|
|
68
|
+
constructor(impl) {
|
|
69
|
+
super(impl);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// errors/unreachable.ts
|
|
74
|
+
var UnreachableError = class extends Error {
|
|
75
|
+
constructor(v) {
|
|
76
|
+
super(`Unreachable value received: ${v}`);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
// test/vitest/expect.ts
|
|
81
|
+
function expectEquals(v1, v2) {
|
|
82
|
+
expect(v1).toEqual(v2);
|
|
83
|
+
}
|
|
84
|
+
function expectTruthy(b) {
|
|
85
|
+
expect(b).toBeTruthy();
|
|
86
|
+
}
|
|
87
|
+
function expectDefined(v) {
|
|
88
|
+
expect(v).toBeDefined();
|
|
89
|
+
}
|
|
90
|
+
function expectDefinedAndReturn(v) {
|
|
91
|
+
expectDefined(v);
|
|
92
|
+
return v;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// util/array.ts
|
|
96
|
+
async function asyncReduce(arr, reducer, initial) {
|
|
97
|
+
let acc = initial;
|
|
98
|
+
for (let i = 0; i < arr.length; i++) {
|
|
99
|
+
const v = arr[i];
|
|
100
|
+
acc = await reducer(acc, v, i);
|
|
101
|
+
}
|
|
102
|
+
return acc;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// util/cache.ts
|
|
106
|
+
var Cache = class {
|
|
107
|
+
constructor(valueFactory) {
|
|
108
|
+
this.valueFactory = valueFactory;
|
|
109
|
+
}
|
|
110
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
111
|
+
cache = /* @__PURE__ */ new Map();
|
|
112
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
113
|
+
retrieveFinalMap(...args) {
|
|
114
|
+
return args.slice(0, -1).reduce(function(cache, key) {
|
|
115
|
+
let map2 = cache.get(key);
|
|
116
|
+
if (map2 == null) {
|
|
117
|
+
map2 = /* @__PURE__ */ new Map();
|
|
118
|
+
cache.set(key, map2);
|
|
119
|
+
}
|
|
120
|
+
return map2;
|
|
121
|
+
}, this.cache);
|
|
122
|
+
}
|
|
123
|
+
retrieve(...args) {
|
|
124
|
+
const finalKey = args[args.length - 1];
|
|
125
|
+
const finalMap = this.retrieveFinalMap(...args);
|
|
126
|
+
if (!finalMap.has(finalKey)) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
return [finalMap.get(finalKey)];
|
|
130
|
+
}
|
|
131
|
+
retrieveOrCreate(...args) {
|
|
132
|
+
const finalKey = args[args.length - 1];
|
|
133
|
+
const finalMap = this.retrieveFinalMap(...args);
|
|
134
|
+
if (finalMap == null || !finalMap.has(finalKey)) {
|
|
135
|
+
const value = this.valueFactory(...args);
|
|
136
|
+
finalMap.set(finalKey, value);
|
|
137
|
+
}
|
|
138
|
+
return finalMap.get(finalKey);
|
|
139
|
+
}
|
|
140
|
+
clear(...args) {
|
|
141
|
+
const finalKey = args[args.length - 1];
|
|
142
|
+
const finalMap = this.retrieveFinalMap(...args);
|
|
143
|
+
finalMap.delete(finalKey);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
// util/delay.ts
|
|
148
|
+
function createDelay(millis) {
|
|
149
|
+
return function() {
|
|
150
|
+
return delay(millis);
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function createWarmupDelay(coldMillis, warmMillis) {
|
|
154
|
+
let warmup;
|
|
155
|
+
return function() {
|
|
156
|
+
if (warmup != null) {
|
|
157
|
+
return warmup.then(function() {
|
|
158
|
+
return delay(warmMillis);
|
|
159
|
+
});
|
|
160
|
+
} else {
|
|
161
|
+
warmup = delay(coldMillis);
|
|
162
|
+
return warmup;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function delay(millis) {
|
|
167
|
+
return new Promise(function(resolve) {
|
|
168
|
+
setTimeout(resolve, millis);
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
var secondDelay = createDelay(1e3);
|
|
172
|
+
|
|
173
|
+
// util/format.ts
|
|
174
|
+
function format(message, ...args) {
|
|
175
|
+
let index = 0;
|
|
176
|
+
return message.replaceAll(/{(\d*)}/g, function(_substring, indexString) {
|
|
177
|
+
let argIndex = parseInt(indexString);
|
|
178
|
+
if (Number.isNaN(argIndex)) {
|
|
179
|
+
argIndex = index;
|
|
180
|
+
index++;
|
|
181
|
+
}
|
|
182
|
+
return JSON.stringify(args[argIndex]);
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// util/json.ts
|
|
187
|
+
function errorHandlingJsonParse(json, errorHandler) {
|
|
188
|
+
try {
|
|
189
|
+
return JSON.parse(json);
|
|
190
|
+
} catch (e) {
|
|
191
|
+
errorHandler?.(e);
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// util/poll.ts
|
|
197
|
+
function constantPollInterval(delay2) {
|
|
198
|
+
return function() {
|
|
199
|
+
return delay2;
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
async function poll(f, {
|
|
203
|
+
pollInterval = constantPollInterval(200),
|
|
204
|
+
retries = 3
|
|
205
|
+
} = {}) {
|
|
206
|
+
let retriesRemaining = retries;
|
|
207
|
+
while (retriesRemaining > 0) {
|
|
208
|
+
await delay(pollInterval(retriesRemaining));
|
|
209
|
+
retriesRemaining--;
|
|
210
|
+
const v = await f();
|
|
211
|
+
if (v != null) {
|
|
212
|
+
return v;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// util/preconditions.ts
|
|
219
|
+
var PreconditionFailedError = class extends Error {
|
|
220
|
+
constructor(message, ...args) {
|
|
221
|
+
super(format(message, ...args));
|
|
222
|
+
this.name = "PreconditionFailedError";
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
function assertExistsAndReturn(t, message, ...args) {
|
|
226
|
+
assertExists(t, message, ...args);
|
|
227
|
+
return t;
|
|
228
|
+
}
|
|
229
|
+
function assertExists(v, message, ...args) {
|
|
230
|
+
if (v == null) {
|
|
231
|
+
throw new PreconditionFailedError(message, ...args);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
function assertEqual(a, b, message = "{} != {}", arg1 = a, arg2 = b, ...args) {
|
|
235
|
+
if (a !== b) {
|
|
236
|
+
throw new PreconditionFailedError(
|
|
237
|
+
message,
|
|
238
|
+
arg1,
|
|
239
|
+
arg2,
|
|
240
|
+
...args
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
function assertState(condition, message, ...args) {
|
|
245
|
+
if (!condition) {
|
|
246
|
+
throw new PreconditionFailedError(message, ...args);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
function assertIs(v, condition, message, ...args) {
|
|
250
|
+
if (!condition(v)) {
|
|
251
|
+
throw new PreconditionFailedError(message, ...args);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
function checkUnary(t, message, ...args) {
|
|
255
|
+
if (t.length !== 1) {
|
|
256
|
+
throw new PreconditionFailedError(message, ...args);
|
|
257
|
+
}
|
|
258
|
+
return t[0];
|
|
259
|
+
}
|
|
260
|
+
function checkValidNumber(n, message, ...args) {
|
|
261
|
+
if (isNaN(n) || !isFinite(n)) {
|
|
262
|
+
throw new PreconditionFailedError(message, ...args);
|
|
263
|
+
}
|
|
264
|
+
return n;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// util/promises.ts
|
|
268
|
+
function callAsPromise(f) {
|
|
269
|
+
return new Promise(function(resolve, reject) {
|
|
270
|
+
f(function(e) {
|
|
271
|
+
if (e == null) {
|
|
272
|
+
resolve();
|
|
273
|
+
}
|
|
274
|
+
reject(e);
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
// util/record.ts
|
|
280
|
+
function reverse(obj) {
|
|
281
|
+
return Object.keys(obj).reduce((acc, stringKey) => {
|
|
282
|
+
const key = stringKey;
|
|
283
|
+
const value = obj[key];
|
|
284
|
+
acc[value] = key;
|
|
285
|
+
return acc;
|
|
286
|
+
}, {});
|
|
287
|
+
}
|
|
288
|
+
function rollup(...records) {
|
|
289
|
+
return records.slice(1).reduce((acc, record) => {
|
|
290
|
+
Object.keys(record).forEach((key) => {
|
|
291
|
+
const k = key;
|
|
292
|
+
acc[k] = acc[k] ?? record[k];
|
|
293
|
+
});
|
|
294
|
+
return acc;
|
|
295
|
+
}, records[0]);
|
|
296
|
+
}
|
|
297
|
+
function union(r1, r2) {
|
|
298
|
+
return {
|
|
299
|
+
...r1,
|
|
300
|
+
...r2
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
function map(r, f) {
|
|
304
|
+
return Object.entries(r).reduce(
|
|
305
|
+
function(acc, [
|
|
306
|
+
k,
|
|
307
|
+
v
|
|
308
|
+
]) {
|
|
309
|
+
const typedKey = k;
|
|
310
|
+
acc[typedKey] = f(typedKey, v);
|
|
311
|
+
return acc;
|
|
312
|
+
},
|
|
313
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
314
|
+
{}
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
function reduce(r, f, a) {
|
|
318
|
+
return Object.entries(r).reduce(
|
|
319
|
+
function(acc, [
|
|
320
|
+
k,
|
|
321
|
+
v
|
|
322
|
+
]) {
|
|
323
|
+
const typedKey = k;
|
|
324
|
+
return f(acc, typedKey, v);
|
|
325
|
+
},
|
|
326
|
+
a
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
function forEach(r, f) {
|
|
330
|
+
return Object.entries(r).forEach(
|
|
331
|
+
function([
|
|
332
|
+
k,
|
|
333
|
+
v
|
|
334
|
+
]) {
|
|
335
|
+
return f(k, v);
|
|
336
|
+
}
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
function toArray(r) {
|
|
340
|
+
return reduce(
|
|
341
|
+
r,
|
|
342
|
+
function(acc, k, v) {
|
|
343
|
+
acc.push([
|
|
344
|
+
k,
|
|
345
|
+
v
|
|
346
|
+
]);
|
|
347
|
+
return acc;
|
|
348
|
+
},
|
|
349
|
+
[]
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
353
|
+
0 && (module.exports = {
|
|
354
|
+
Cache,
|
|
355
|
+
NotImplementedError,
|
|
356
|
+
PreconditionFailedError,
|
|
357
|
+
UnexpectedImplementationError,
|
|
358
|
+
UnreachableError,
|
|
359
|
+
assertEqual,
|
|
360
|
+
assertExists,
|
|
361
|
+
assertExistsAndReturn,
|
|
362
|
+
assertIs,
|
|
363
|
+
assertState,
|
|
364
|
+
asyncReduce,
|
|
365
|
+
callAsPromise,
|
|
366
|
+
checkUnary,
|
|
367
|
+
checkValidNumber,
|
|
368
|
+
constantPollInterval,
|
|
369
|
+
createDelay,
|
|
370
|
+
createWarmupDelay,
|
|
371
|
+
delay,
|
|
372
|
+
errorHandlingJsonParse,
|
|
373
|
+
expectDefined,
|
|
374
|
+
expectDefinedAndReturn,
|
|
375
|
+
expectEquals,
|
|
376
|
+
expectTruthy,
|
|
377
|
+
forEach,
|
|
378
|
+
format,
|
|
379
|
+
map,
|
|
380
|
+
poll,
|
|
381
|
+
reduce,
|
|
382
|
+
reverse,
|
|
383
|
+
rollup,
|
|
384
|
+
secondDelay,
|
|
385
|
+
toArray,
|
|
386
|
+
union
|
|
387
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { IsEqual } from 'type-fest';
|
|
2
|
+
|
|
3
|
+
declare class NotImplementedError extends Error {
|
|
4
|
+
constructor(name: string);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
declare class UnexpectedImplementationError extends Error {
|
|
8
|
+
constructor(impl: string);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare class UnreachableError extends Error {
|
|
12
|
+
constructor(v: never);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
declare function expectEquals<V1, V2 extends V1>(v1: V1, v2: V2): asserts v1 is V2;
|
|
16
|
+
declare function expectTruthy(b: boolean): asserts b is true;
|
|
17
|
+
declare function expectDefined<V>(v: V): asserts v is NonNullable<V>;
|
|
18
|
+
declare function expectDefinedAndReturn<V>(v: V): NonNullable<V>;
|
|
19
|
+
|
|
20
|
+
type ElementOfArray<A> = A extends readonly (infer T)[] ? T : never;
|
|
21
|
+
|
|
22
|
+
type ExtractPromiseType<T> = T extends Promise<infer V> ? V : never;
|
|
23
|
+
type ExtractArrayType<T> = T extends Array<infer V> ? V : never;
|
|
24
|
+
|
|
25
|
+
type IsFieldReadonly<R extends Record<string, any>, K extends keyof R> = {
|
|
26
|
+
[P in keyof R]: IsEqual<{
|
|
27
|
+
[Q in P]: R[P];
|
|
28
|
+
}, {
|
|
29
|
+
readonly [Q in P]: R[P];
|
|
30
|
+
}>;
|
|
31
|
+
}[K];
|
|
32
|
+
|
|
33
|
+
type Just<T> = [T];
|
|
34
|
+
type Nothing = null;
|
|
35
|
+
type Maybe<T> = Just<T> | Nothing;
|
|
36
|
+
|
|
37
|
+
type PrintableOf<T> = Extract<T, string | number>;
|
|
38
|
+
|
|
39
|
+
type RequiredOfRecord<R extends Readonly<Record<string | number | symbol, any>>> = {
|
|
40
|
+
[K in keyof R as undefined extends R[K] ? never : K]-?: NonNullable<R[K]>;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
type StringKeyOf<T> = Exclude<keyof T, number | symbol>;
|
|
44
|
+
|
|
45
|
+
declare function asyncReduce<T, Acc>(arr: readonly T[], reducer: (acc: Acc, v: T, index: number) => Promise<Acc>, initial: Acc): Promise<Acc>;
|
|
46
|
+
|
|
47
|
+
type CacheValueFactory<A extends any[], V> = {
|
|
48
|
+
(...args: A): V;
|
|
49
|
+
};
|
|
50
|
+
declare class Cache<A extends any[], V> {
|
|
51
|
+
private readonly valueFactory;
|
|
52
|
+
private readonly cache;
|
|
53
|
+
constructor(valueFactory: CacheValueFactory<A, V>);
|
|
54
|
+
private retrieveFinalMap;
|
|
55
|
+
retrieve(...args: A): Maybe<V>;
|
|
56
|
+
retrieveOrCreate(...args: A): V;
|
|
57
|
+
clear(...args: A): void;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type Delay = () => Promise<void>;
|
|
61
|
+
declare function createDelay(millis: number): Delay;
|
|
62
|
+
/**
|
|
63
|
+
* creates a delay that blocks everything for the specified number of cold milliseconds initially, then
|
|
64
|
+
* the warm millis thereafter
|
|
65
|
+
* @param coldMillis the number of milliseconds to block initial delays for
|
|
66
|
+
* @param warmMillis the number of milliseconds to block subsequent delays for
|
|
67
|
+
* @returns a Promise that awaits for the specified time
|
|
68
|
+
*/
|
|
69
|
+
declare function createWarmupDelay(coldMillis: number, warmMillis: number): () => Promise<void>;
|
|
70
|
+
declare function delay(millis: number): Promise<void>;
|
|
71
|
+
declare const secondDelay: Delay;
|
|
72
|
+
|
|
73
|
+
type FormatArg = string | number | boolean | object | null | undefined | symbol;
|
|
74
|
+
declare function format(message: string, ...args: readonly FormatArg[]): string;
|
|
75
|
+
|
|
76
|
+
declare function errorHandlingJsonParse<T = any>(json: string, errorHandler?: (e: unknown) => void): T | null;
|
|
77
|
+
|
|
78
|
+
declare function constantPollInterval(delay: number): () => number;
|
|
79
|
+
type PollOptions = {
|
|
80
|
+
pollInterval?: (retries: number) => number;
|
|
81
|
+
retries?: number;
|
|
82
|
+
};
|
|
83
|
+
declare function poll<T>(f: () => Promise<Maybe<T>>, { pollInterval, retries, }?: PollOptions): Promise<Maybe<T>>;
|
|
84
|
+
|
|
85
|
+
declare class PreconditionFailedError extends Error {
|
|
86
|
+
constructor(message: string, ...args: readonly FormatArg[]);
|
|
87
|
+
}
|
|
88
|
+
declare function assertExistsAndReturn<T>(t: T, message: string, ...args: readonly FormatArg[]): NonNullable<T>;
|
|
89
|
+
declare function assertExists<V>(v: V, message: string, ...args: readonly FormatArg[]): asserts v is NonNullable<V>;
|
|
90
|
+
declare function assertEqual<T extends FormatArg>(a: T, b: T, message?: string, arg1?: FormatArg, arg2?: FormatArg, ...args: readonly FormatArg[]): void;
|
|
91
|
+
declare function assertState(condition: boolean, message: string, ...args: readonly FormatArg[]): asserts condition is true;
|
|
92
|
+
declare function assertIs<V, T extends V>(v: V, condition: (v: V) => v is T, message: string, ...args: readonly FormatArg[]): asserts v is T;
|
|
93
|
+
declare function checkUnary<T>(t: readonly T[], message: string, ...args: readonly FormatArg[]): T;
|
|
94
|
+
declare function checkValidNumber(n: number, message: string, ...args: readonly FormatArg[]): number;
|
|
95
|
+
|
|
96
|
+
declare function callAsPromise(f: (cb: (e?: unknown) => void) => void): Promise<void>;
|
|
97
|
+
|
|
98
|
+
declare function reverse<Key extends string | number | symbol, Value extends string | number | symbol>(obj: Record<Key, Value>): Record<Value, Key>;
|
|
99
|
+
declare function rollup<R extends Record<K, V>, K extends string | number | symbol = keyof R, V = R[K]>(...records: Partial<R>[]): R;
|
|
100
|
+
declare function union<R1 extends Readonly<Record<K1, V1>>, K1 extends string | number | symbol, V1 extends R1[K1], R2 extends Readonly<Record<K2, V2>>, K2 extends string | number | symbol, V2 extends R2[K2]>(r1: R1, r2: R2): R1 & R2;
|
|
101
|
+
declare function map<K extends string | number | symbol, V, R = V>(r: Readonly<Record<K, V>>, f: (k: K, v: V) => R): Record<K, R>;
|
|
102
|
+
declare function reduce<K extends string | number | symbol, V, A>(r: Readonly<Record<K, V>>, f: (acc: A, k: K, v: V) => A, a: A): A;
|
|
103
|
+
declare function forEach<R extends Readonly<Record<K, V>>, K extends string | number | symbol = R extends Readonly<Record<infer Kk, infer _Vv>> ? Kk : never, V = R extends Readonly<Record<infer _Kk, infer Vv>> ? Vv : never>(r: R, f: (k: K, v: R[K]) => void): void;
|
|
104
|
+
declare function toArray<K extends string | number | symbol, V>(r: Readonly<Record<K, V>>): readonly (readonly [K, V])[];
|
|
105
|
+
type Mutable<T> = {
|
|
106
|
+
-readonly [K in keyof T]: T[K];
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export { Cache, type CacheValueFactory, type Delay, type ElementOfArray, type ExtractArrayType, type ExtractPromiseType, type FormatArg, type IsFieldReadonly, type Just, type Maybe, type Mutable, NotImplementedError, type Nothing, PreconditionFailedError, type PrintableOf, type RequiredOfRecord, type StringKeyOf, UnexpectedImplementationError, UnreachableError, assertEqual, assertExists, assertExistsAndReturn, assertIs, assertState, asyncReduce, callAsPromise, checkUnary, checkValidNumber, constantPollInterval, createDelay, createWarmupDelay, delay, errorHandlingJsonParse, expectDefined, expectDefinedAndReturn, expectEquals, expectTruthy, forEach, format, map, poll, reduce, reverse, rollup, secondDelay, toArray, union };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { IsEqual } from 'type-fest';
|
|
2
|
+
|
|
3
|
+
declare class NotImplementedError extends Error {
|
|
4
|
+
constructor(name: string);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
declare class UnexpectedImplementationError extends Error {
|
|
8
|
+
constructor(impl: string);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
declare class UnreachableError extends Error {
|
|
12
|
+
constructor(v: never);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
declare function expectEquals<V1, V2 extends V1>(v1: V1, v2: V2): asserts v1 is V2;
|
|
16
|
+
declare function expectTruthy(b: boolean): asserts b is true;
|
|
17
|
+
declare function expectDefined<V>(v: V): asserts v is NonNullable<V>;
|
|
18
|
+
declare function expectDefinedAndReturn<V>(v: V): NonNullable<V>;
|
|
19
|
+
|
|
20
|
+
type ElementOfArray<A> = A extends readonly (infer T)[] ? T : never;
|
|
21
|
+
|
|
22
|
+
type ExtractPromiseType<T> = T extends Promise<infer V> ? V : never;
|
|
23
|
+
type ExtractArrayType<T> = T extends Array<infer V> ? V : never;
|
|
24
|
+
|
|
25
|
+
type IsFieldReadonly<R extends Record<string, any>, K extends keyof R> = {
|
|
26
|
+
[P in keyof R]: IsEqual<{
|
|
27
|
+
[Q in P]: R[P];
|
|
28
|
+
}, {
|
|
29
|
+
readonly [Q in P]: R[P];
|
|
30
|
+
}>;
|
|
31
|
+
}[K];
|
|
32
|
+
|
|
33
|
+
type Just<T> = [T];
|
|
34
|
+
type Nothing = null;
|
|
35
|
+
type Maybe<T> = Just<T> | Nothing;
|
|
36
|
+
|
|
37
|
+
type PrintableOf<T> = Extract<T, string | number>;
|
|
38
|
+
|
|
39
|
+
type RequiredOfRecord<R extends Readonly<Record<string | number | symbol, any>>> = {
|
|
40
|
+
[K in keyof R as undefined extends R[K] ? never : K]-?: NonNullable<R[K]>;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
type StringKeyOf<T> = Exclude<keyof T, number | symbol>;
|
|
44
|
+
|
|
45
|
+
declare function asyncReduce<T, Acc>(arr: readonly T[], reducer: (acc: Acc, v: T, index: number) => Promise<Acc>, initial: Acc): Promise<Acc>;
|
|
46
|
+
|
|
47
|
+
type CacheValueFactory<A extends any[], V> = {
|
|
48
|
+
(...args: A): V;
|
|
49
|
+
};
|
|
50
|
+
declare class Cache<A extends any[], V> {
|
|
51
|
+
private readonly valueFactory;
|
|
52
|
+
private readonly cache;
|
|
53
|
+
constructor(valueFactory: CacheValueFactory<A, V>);
|
|
54
|
+
private retrieveFinalMap;
|
|
55
|
+
retrieve(...args: A): Maybe<V>;
|
|
56
|
+
retrieveOrCreate(...args: A): V;
|
|
57
|
+
clear(...args: A): void;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type Delay = () => Promise<void>;
|
|
61
|
+
declare function createDelay(millis: number): Delay;
|
|
62
|
+
/**
|
|
63
|
+
* creates a delay that blocks everything for the specified number of cold milliseconds initially, then
|
|
64
|
+
* the warm millis thereafter
|
|
65
|
+
* @param coldMillis the number of milliseconds to block initial delays for
|
|
66
|
+
* @param warmMillis the number of milliseconds to block subsequent delays for
|
|
67
|
+
* @returns a Promise that awaits for the specified time
|
|
68
|
+
*/
|
|
69
|
+
declare function createWarmupDelay(coldMillis: number, warmMillis: number): () => Promise<void>;
|
|
70
|
+
declare function delay(millis: number): Promise<void>;
|
|
71
|
+
declare const secondDelay: Delay;
|
|
72
|
+
|
|
73
|
+
type FormatArg = string | number | boolean | object | null | undefined | symbol;
|
|
74
|
+
declare function format(message: string, ...args: readonly FormatArg[]): string;
|
|
75
|
+
|
|
76
|
+
declare function errorHandlingJsonParse<T = any>(json: string, errorHandler?: (e: unknown) => void): T | null;
|
|
77
|
+
|
|
78
|
+
declare function constantPollInterval(delay: number): () => number;
|
|
79
|
+
type PollOptions = {
|
|
80
|
+
pollInterval?: (retries: number) => number;
|
|
81
|
+
retries?: number;
|
|
82
|
+
};
|
|
83
|
+
declare function poll<T>(f: () => Promise<Maybe<T>>, { pollInterval, retries, }?: PollOptions): Promise<Maybe<T>>;
|
|
84
|
+
|
|
85
|
+
declare class PreconditionFailedError extends Error {
|
|
86
|
+
constructor(message: string, ...args: readonly FormatArg[]);
|
|
87
|
+
}
|
|
88
|
+
declare function assertExistsAndReturn<T>(t: T, message: string, ...args: readonly FormatArg[]): NonNullable<T>;
|
|
89
|
+
declare function assertExists<V>(v: V, message: string, ...args: readonly FormatArg[]): asserts v is NonNullable<V>;
|
|
90
|
+
declare function assertEqual<T extends FormatArg>(a: T, b: T, message?: string, arg1?: FormatArg, arg2?: FormatArg, ...args: readonly FormatArg[]): void;
|
|
91
|
+
declare function assertState(condition: boolean, message: string, ...args: readonly FormatArg[]): asserts condition is true;
|
|
92
|
+
declare function assertIs<V, T extends V>(v: V, condition: (v: V) => v is T, message: string, ...args: readonly FormatArg[]): asserts v is T;
|
|
93
|
+
declare function checkUnary<T>(t: readonly T[], message: string, ...args: readonly FormatArg[]): T;
|
|
94
|
+
declare function checkValidNumber(n: number, message: string, ...args: readonly FormatArg[]): number;
|
|
95
|
+
|
|
96
|
+
declare function callAsPromise(f: (cb: (e?: unknown) => void) => void): Promise<void>;
|
|
97
|
+
|
|
98
|
+
declare function reverse<Key extends string | number | symbol, Value extends string | number | symbol>(obj: Record<Key, Value>): Record<Value, Key>;
|
|
99
|
+
declare function rollup<R extends Record<K, V>, K extends string | number | symbol = keyof R, V = R[K]>(...records: Partial<R>[]): R;
|
|
100
|
+
declare function union<R1 extends Readonly<Record<K1, V1>>, K1 extends string | number | symbol, V1 extends R1[K1], R2 extends Readonly<Record<K2, V2>>, K2 extends string | number | symbol, V2 extends R2[K2]>(r1: R1, r2: R2): R1 & R2;
|
|
101
|
+
declare function map<K extends string | number | symbol, V, R = V>(r: Readonly<Record<K, V>>, f: (k: K, v: V) => R): Record<K, R>;
|
|
102
|
+
declare function reduce<K extends string | number | symbol, V, A>(r: Readonly<Record<K, V>>, f: (acc: A, k: K, v: V) => A, a: A): A;
|
|
103
|
+
declare function forEach<R extends Readonly<Record<K, V>>, K extends string | number | symbol = R extends Readonly<Record<infer Kk, infer _Vv>> ? Kk : never, V = R extends Readonly<Record<infer _Kk, infer Vv>> ? Vv : never>(r: R, f: (k: K, v: R[K]) => void): void;
|
|
104
|
+
declare function toArray<K extends string | number | symbol, V>(r: Readonly<Record<K, V>>): readonly (readonly [K, V])[];
|
|
105
|
+
type Mutable<T> = {
|
|
106
|
+
-readonly [K in keyof T]: T[K];
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
export { Cache, type CacheValueFactory, type Delay, type ElementOfArray, type ExtractArrayType, type ExtractPromiseType, type FormatArg, type IsFieldReadonly, type Just, type Maybe, type Mutable, NotImplementedError, type Nothing, PreconditionFailedError, type PrintableOf, type RequiredOfRecord, type StringKeyOf, UnexpectedImplementationError, UnreachableError, assertEqual, assertExists, assertExistsAndReturn, assertIs, assertState, asyncReduce, callAsPromise, checkUnary, checkValidNumber, constantPollInterval, createDelay, createWarmupDelay, delay, errorHandlingJsonParse, expectDefined, expectDefinedAndReturn, expectEquals, expectTruthy, forEach, format, map, poll, reduce, reverse, rollup, secondDelay, toArray, union };
|