@aidc-toolkit/utility 0.9.4 → 0.9.6-beta
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/dist/index.cjs +1726 -0
- package/dist/index.d.cts +840 -0
- package/dist/index.d.ts +840 -0
- package/dist/index.js +1686 -0
- package/package.json +8 -8
- package/src/character_set.ts +34 -83
- package/src/index.ts +1 -0
- package/src/iterator_proxy.ts +177 -189
- package/src/transformer.ts +29 -103
- package/src/types.ts +46 -0
- package/test/character_set.test.ts +6 -7
- package/test/iterator_proxy.test.ts +34 -2
- package/test/record.test.ts +1 -1
- package/test/reg_exp.test.ts +1 -1
- package/test/sequencer.test.ts +5 -5
- package/test/transformer.test.ts +1 -1
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -32
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- package/.github/workflows/npm-publish.yml +0 -38
- package/.idea/inspectionProfiles/Project_Default.xml +0 -7
- package/.idea/misc.xml +0 -6
- package/.idea/modules.xml +0 -8
- package/.idea/runConfigurations/Test_all.xml +0 -12
- package/.idea/runConfigurations/Test_character_set.xml +0 -12
- package/.idea/runConfigurations/Test_iterator_proxy.xml +0 -12
- package/.idea/runConfigurations/Test_record.xml +0 -12
- package/.idea/runConfigurations/Test_regular_expression.xml +0 -12
- package/.idea/runConfigurations/Test_transformer.xml +0 -12
- package/.idea/runConfigurations/build.xml +0 -12
- package/.idea/runConfigurations/build_dev.xml +0 -12
- package/.idea/runConfigurations/eslint.xml +0 -12
- package/.idea/utility.iml +0 -9
- package/.idea/vcs.xml +0 -6
package/dist/index.js
ADDED
|
@@ -0,0 +1,1686 @@
|
|
|
1
|
+
// src/iterator_proxy.ts
|
|
2
|
+
var IteratorProxyBase = class _IteratorProxyBase {
|
|
3
|
+
/**
|
|
4
|
+
* Convert an iteration source to an iterable.
|
|
5
|
+
*
|
|
6
|
+
* @param iterationSource
|
|
7
|
+
* Iteration source.
|
|
8
|
+
*
|
|
9
|
+
* @returns
|
|
10
|
+
* Iteration source if it is already an iterable, otherwise iteration source wrapped in an iterable.
|
|
11
|
+
*/
|
|
12
|
+
static toIterable(iterationSource) {
|
|
13
|
+
return Symbol.iterator in iterationSource ? iterationSource : {
|
|
14
|
+
[Symbol.iterator]() {
|
|
15
|
+
return iterationSource;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Initial iterable.
|
|
21
|
+
*/
|
|
22
|
+
_initialIterable;
|
|
23
|
+
/**
|
|
24
|
+
* Initial iterator.
|
|
25
|
+
*/
|
|
26
|
+
_initialIterator;
|
|
27
|
+
/**
|
|
28
|
+
* Constructor.
|
|
29
|
+
*
|
|
30
|
+
* @param initialIterationSource
|
|
31
|
+
* Initial iteration source.
|
|
32
|
+
*/
|
|
33
|
+
constructor(initialIterationSource) {
|
|
34
|
+
this._initialIterable = _IteratorProxyBase.toIterable(initialIterationSource);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Get the initial iterable.
|
|
38
|
+
*/
|
|
39
|
+
get initialIterable() {
|
|
40
|
+
return this._initialIterable;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Get the initial iterator.
|
|
44
|
+
*/
|
|
45
|
+
get initialIterator() {
|
|
46
|
+
if (this._initialIterator === void 0) {
|
|
47
|
+
this._initialIterator = this.initialIterable[Symbol.iterator]();
|
|
48
|
+
}
|
|
49
|
+
return this._initialIterator;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* @inheritDoc
|
|
53
|
+
*/
|
|
54
|
+
get [Symbol.toStringTag]() {
|
|
55
|
+
return "IteratorProxy";
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* @inheritDoc
|
|
59
|
+
*/
|
|
60
|
+
[Symbol.dispose]() {
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* @inheritDoc
|
|
64
|
+
*/
|
|
65
|
+
[Symbol.iterator]() {
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Get the next result from the initial iterator.
|
|
70
|
+
*
|
|
71
|
+
* @param value
|
|
72
|
+
* Tuple value to be passed to Iterator.next().
|
|
73
|
+
*
|
|
74
|
+
* @returns
|
|
75
|
+
* Next result from the initial iterator.
|
|
76
|
+
*/
|
|
77
|
+
initialNext(...value) {
|
|
78
|
+
return this.initialIterator.next(...value);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* @inheritDoc
|
|
82
|
+
*/
|
|
83
|
+
map(callback) {
|
|
84
|
+
return new IteratorMapProxy(this, callback);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @inheritDoc
|
|
88
|
+
*/
|
|
89
|
+
flatMap(callback) {
|
|
90
|
+
return new IteratorFlatMapProxy(this, callback);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* @inheritDoc
|
|
94
|
+
*/
|
|
95
|
+
filter(predicate) {
|
|
96
|
+
return new IteratorFilterProxy(this, predicate, true);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* @inheritDoc
|
|
100
|
+
*/
|
|
101
|
+
take(limit) {
|
|
102
|
+
return new IteratorTakeProxy(this, limit);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* @inheritDoc
|
|
106
|
+
*/
|
|
107
|
+
drop(count) {
|
|
108
|
+
return new IteratorDropProxy(this, count);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* @inheritDoc
|
|
112
|
+
*/
|
|
113
|
+
reduce(callback, initialValue) {
|
|
114
|
+
let index = 0;
|
|
115
|
+
let result = initialValue;
|
|
116
|
+
for (const value of this) {
|
|
117
|
+
if (index === 0 && arguments.length === 1) {
|
|
118
|
+
result = value;
|
|
119
|
+
} else {
|
|
120
|
+
result = callback(result, value, index);
|
|
121
|
+
}
|
|
122
|
+
index++;
|
|
123
|
+
}
|
|
124
|
+
if (index === 0 && arguments.length === 1) {
|
|
125
|
+
throw new Error("reduce() of empty iterator with no initial value");
|
|
126
|
+
}
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* @inheritDoc
|
|
131
|
+
*/
|
|
132
|
+
toArray() {
|
|
133
|
+
return Array.from(this);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* @inheritDoc
|
|
137
|
+
*/
|
|
138
|
+
forEach(callback) {
|
|
139
|
+
let index = 0;
|
|
140
|
+
for (const element of this) {
|
|
141
|
+
callback(element, index++);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* @inheritDoc
|
|
146
|
+
*/
|
|
147
|
+
some(predicate) {
|
|
148
|
+
return new IteratorFilterProxy(this, predicate, true).next().done !== true;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* @inheritDoc
|
|
152
|
+
*/
|
|
153
|
+
every(predicate) {
|
|
154
|
+
return new IteratorFilterProxy(this, predicate, false).next().done === true;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* @inheritDoc
|
|
158
|
+
*/
|
|
159
|
+
find(predicate) {
|
|
160
|
+
return new IteratorFilterProxy(this, predicate, true).next().value;
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
var IteratorProxyObject = class extends IteratorProxyBase {
|
|
164
|
+
/**
|
|
165
|
+
* @inheritDoc
|
|
166
|
+
*/
|
|
167
|
+
next(...value) {
|
|
168
|
+
return this.initialNext(...value);
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
var IteratorMapProxyBase = class extends IteratorProxyBase {
|
|
172
|
+
/**
|
|
173
|
+
* Callback.
|
|
174
|
+
*/
|
|
175
|
+
_callback;
|
|
176
|
+
/**
|
|
177
|
+
* Index into initial iteration source.
|
|
178
|
+
*/
|
|
179
|
+
_index;
|
|
180
|
+
/**
|
|
181
|
+
* Constructor.
|
|
182
|
+
*
|
|
183
|
+
* @param initialIterationSource
|
|
184
|
+
* Initial iteration source.
|
|
185
|
+
*
|
|
186
|
+
* @param callback
|
|
187
|
+
* Callback.
|
|
188
|
+
*/
|
|
189
|
+
constructor(initialIterationSource, callback) {
|
|
190
|
+
super(initialIterationSource);
|
|
191
|
+
this._callback = callback;
|
|
192
|
+
this._index = 0;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Get the next result from the intermediate iterator.
|
|
196
|
+
*
|
|
197
|
+
* @param value
|
|
198
|
+
* Tuple value to be passed to Iterator.next().
|
|
199
|
+
*
|
|
200
|
+
* @returns
|
|
201
|
+
* Next result from the intermediate iterator.
|
|
202
|
+
*/
|
|
203
|
+
intermediateNext(...value) {
|
|
204
|
+
const initialResult = this.initialNext(...value);
|
|
205
|
+
return initialResult.done !== true ? {
|
|
206
|
+
value: this._callback(initialResult.value, this._index++)
|
|
207
|
+
} : {
|
|
208
|
+
done: true,
|
|
209
|
+
value: void 0
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
var IteratorMapProxy = class extends IteratorMapProxyBase {
|
|
214
|
+
/**
|
|
215
|
+
* @inheritDoc
|
|
216
|
+
*/
|
|
217
|
+
next(...value) {
|
|
218
|
+
return this.intermediateNext(...value);
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
var IteratorFlatMapProxy = class extends IteratorMapProxyBase {
|
|
222
|
+
_intermediateIterator;
|
|
223
|
+
/**
|
|
224
|
+
* @inheritDoc
|
|
225
|
+
*/
|
|
226
|
+
next(...value) {
|
|
227
|
+
let finalResult = void 0;
|
|
228
|
+
do {
|
|
229
|
+
if (this._intermediateIterator === void 0) {
|
|
230
|
+
const intermediateResult = this.intermediateNext(...value);
|
|
231
|
+
if (intermediateResult.done === true) {
|
|
232
|
+
finalResult = intermediateResult;
|
|
233
|
+
} else {
|
|
234
|
+
this._intermediateIterator = IteratorProxyBase.toIterable(intermediateResult.value)[Symbol.iterator]();
|
|
235
|
+
}
|
|
236
|
+
} else {
|
|
237
|
+
const pendingFinalResult = this._intermediateIterator.next();
|
|
238
|
+
if (pendingFinalResult.done === true) {
|
|
239
|
+
this._intermediateIterator = void 0;
|
|
240
|
+
} else {
|
|
241
|
+
finalResult = pendingFinalResult;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
} while (finalResult === void 0);
|
|
245
|
+
return finalResult;
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
var IteratorFilterProxy = class extends IteratorProxyBase {
|
|
249
|
+
/**
|
|
250
|
+
* Predicate.
|
|
251
|
+
*/
|
|
252
|
+
_predicate;
|
|
253
|
+
/**
|
|
254
|
+
* Expected truthy result of the predicate.
|
|
255
|
+
*/
|
|
256
|
+
_expectedTruthy;
|
|
257
|
+
/**
|
|
258
|
+
* Index into iteration source.
|
|
259
|
+
*/
|
|
260
|
+
_index;
|
|
261
|
+
/**
|
|
262
|
+
* Constructor.
|
|
263
|
+
*
|
|
264
|
+
* @param iterationSource
|
|
265
|
+
* Iteration source.
|
|
266
|
+
*
|
|
267
|
+
* @param predicate
|
|
268
|
+
* Predicate.
|
|
269
|
+
*
|
|
270
|
+
* @param expectedTruthy
|
|
271
|
+
* Expected truthy result of the predicate.
|
|
272
|
+
*/
|
|
273
|
+
constructor(iterationSource, predicate, expectedTruthy) {
|
|
274
|
+
super(iterationSource);
|
|
275
|
+
this._predicate = predicate;
|
|
276
|
+
this._expectedTruthy = expectedTruthy;
|
|
277
|
+
this._index = 0;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* @inheritDoc
|
|
281
|
+
*/
|
|
282
|
+
next(...value) {
|
|
283
|
+
let result;
|
|
284
|
+
const expectedTruthy = this._expectedTruthy;
|
|
285
|
+
do {
|
|
286
|
+
result = this.initialNext(...value);
|
|
287
|
+
} while (result.done !== true && Boolean(this._predicate(result.value, this._index++)) !== expectedTruthy);
|
|
288
|
+
return result;
|
|
289
|
+
}
|
|
290
|
+
};
|
|
291
|
+
var IteratorCountProxyBase = class extends IteratorProxyObject {
|
|
292
|
+
/**
|
|
293
|
+
* Count.
|
|
294
|
+
*/
|
|
295
|
+
_count;
|
|
296
|
+
/**
|
|
297
|
+
* Constructor.
|
|
298
|
+
*
|
|
299
|
+
* @param initialIterationSource
|
|
300
|
+
* Initial iteration source.
|
|
301
|
+
*
|
|
302
|
+
* @param count
|
|
303
|
+
* Count.
|
|
304
|
+
*/
|
|
305
|
+
constructor(initialIterationSource, count) {
|
|
306
|
+
super(initialIterationSource);
|
|
307
|
+
if (!Number.isInteger(count) || count < 0) {
|
|
308
|
+
throw new RangeError("Count must be a positive integer");
|
|
309
|
+
}
|
|
310
|
+
this._count = count;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Determine if iterator is exhausted (by count or by iterator itself).
|
|
314
|
+
*/
|
|
315
|
+
get exhausted() {
|
|
316
|
+
return this._count <= 0;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* @inheritDoc
|
|
320
|
+
*/
|
|
321
|
+
next(...value) {
|
|
322
|
+
const result = super.next(...value);
|
|
323
|
+
if (result.done !== true) {
|
|
324
|
+
this._count--;
|
|
325
|
+
} else {
|
|
326
|
+
this._count = 0;
|
|
327
|
+
}
|
|
328
|
+
return result;
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
var IteratorTakeProxy = class extends IteratorCountProxyBase {
|
|
332
|
+
/**
|
|
333
|
+
* @inheritDoc
|
|
334
|
+
*/
|
|
335
|
+
next(...value) {
|
|
336
|
+
return !this.exhausted ? super.next(...value) : {
|
|
337
|
+
done: true,
|
|
338
|
+
value: void 0
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
var IteratorDropProxy = class extends IteratorCountProxyBase {
|
|
343
|
+
/**
|
|
344
|
+
* @inheritDoc
|
|
345
|
+
*/
|
|
346
|
+
next(...value) {
|
|
347
|
+
while (!this.exhausted) {
|
|
348
|
+
super.next(...value);
|
|
349
|
+
}
|
|
350
|
+
return super.next(...value);
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
function iteratorProxy() {
|
|
354
|
+
let supported;
|
|
355
|
+
try {
|
|
356
|
+
supported = process.env["NODE_ENV"] !== "test";
|
|
357
|
+
} catch (_e) {
|
|
358
|
+
supported = true;
|
|
359
|
+
}
|
|
360
|
+
if (supported) {
|
|
361
|
+
try {
|
|
362
|
+
Iterator.from([]);
|
|
363
|
+
} catch (_e) {
|
|
364
|
+
supported = false;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return supported ? Iterator : {
|
|
368
|
+
/**
|
|
369
|
+
* @inheritDoc
|
|
370
|
+
*/
|
|
371
|
+
from(value) {
|
|
372
|
+
return value instanceof IteratorProxyBase ? value : new IteratorProxyObject(value);
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
var IteratorProxy = iteratorProxy();
|
|
377
|
+
|
|
378
|
+
// src/sequencer.ts
|
|
379
|
+
var Sequencer = class {
|
|
380
|
+
/**
|
|
381
|
+
* Start value (inclusive).
|
|
382
|
+
*/
|
|
383
|
+
_startValue;
|
|
384
|
+
/**
|
|
385
|
+
* End value (exclusive).
|
|
386
|
+
*/
|
|
387
|
+
_endValue;
|
|
388
|
+
/**
|
|
389
|
+
* Count of values.
|
|
390
|
+
*/
|
|
391
|
+
_count;
|
|
392
|
+
/**
|
|
393
|
+
* Delta to the next value; equal to the sign of the count.
|
|
394
|
+
*/
|
|
395
|
+
_nextDelta;
|
|
396
|
+
/**
|
|
397
|
+
* Minimum value (inclusive).
|
|
398
|
+
*/
|
|
399
|
+
_minValue;
|
|
400
|
+
/**
|
|
401
|
+
* Maximum value (inclusive).
|
|
402
|
+
*/
|
|
403
|
+
_maxValue;
|
|
404
|
+
/**
|
|
405
|
+
* Next value.
|
|
406
|
+
*/
|
|
407
|
+
_nextValue;
|
|
408
|
+
/**
|
|
409
|
+
* Constructor.
|
|
410
|
+
*
|
|
411
|
+
* @param startValue
|
|
412
|
+
* Start value.
|
|
413
|
+
*
|
|
414
|
+
* @param count
|
|
415
|
+
* Count of values. If count is zero or positive, iteration ascends from start value, otherwise it descends from
|
|
416
|
+
* start value.
|
|
417
|
+
*/
|
|
418
|
+
constructor(startValue, count) {
|
|
419
|
+
this._startValue = BigInt(startValue);
|
|
420
|
+
this._endValue = this._startValue + BigInt(count);
|
|
421
|
+
this._count = count;
|
|
422
|
+
const ascending = count >= 0;
|
|
423
|
+
if (ascending) {
|
|
424
|
+
this._nextDelta = 1n;
|
|
425
|
+
this._minValue = this._startValue;
|
|
426
|
+
this._maxValue = this._endValue - 1n;
|
|
427
|
+
} else {
|
|
428
|
+
this._nextDelta = -1n;
|
|
429
|
+
this._minValue = this._endValue + 1n;
|
|
430
|
+
this._maxValue = this._startValue;
|
|
431
|
+
}
|
|
432
|
+
this._nextValue = this._startValue;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Get the start value (inclusive).
|
|
436
|
+
*/
|
|
437
|
+
get startValue() {
|
|
438
|
+
return this._startValue;
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Get the end value (exclusive).
|
|
442
|
+
*/
|
|
443
|
+
get endValue() {
|
|
444
|
+
return this._endValue;
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Get the count of values.
|
|
448
|
+
*/
|
|
449
|
+
get count() {
|
|
450
|
+
return this._count;
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Get the minimum value (inclusive).
|
|
454
|
+
*/
|
|
455
|
+
get minValue() {
|
|
456
|
+
return this._minValue;
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Get the maximum value (inclusive).
|
|
460
|
+
*/
|
|
461
|
+
get maxValue() {
|
|
462
|
+
return this._maxValue;
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Iterable implementation.
|
|
466
|
+
*
|
|
467
|
+
* @returns
|
|
468
|
+
* this
|
|
469
|
+
*/
|
|
470
|
+
[Symbol.iterator]() {
|
|
471
|
+
return this;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Iterator implementation.
|
|
475
|
+
*
|
|
476
|
+
* @returns
|
|
477
|
+
* Iterator result. If iterator is exhausted, the value is absolute value of the count.
|
|
478
|
+
*/
|
|
479
|
+
next() {
|
|
480
|
+
const done = this._nextValue === this._endValue;
|
|
481
|
+
let result;
|
|
482
|
+
if (!done) {
|
|
483
|
+
result = {
|
|
484
|
+
value: this._nextValue
|
|
485
|
+
};
|
|
486
|
+
this._nextValue += this._nextDelta;
|
|
487
|
+
} else {
|
|
488
|
+
result = {
|
|
489
|
+
done: true,
|
|
490
|
+
value: Math.abs(this._count)
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
return result;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Reset the iterator.
|
|
497
|
+
*/
|
|
498
|
+
reset() {
|
|
499
|
+
this._nextValue = this._startValue;
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
// src/locale/i18n.ts
|
|
504
|
+
import { i18nAddResourceBundle, i18nAssertValidResources, i18next } from "@aidc-toolkit/core";
|
|
505
|
+
|
|
506
|
+
// src/locale/en/locale_strings.ts
|
|
507
|
+
var localeStrings = {
|
|
508
|
+
Transformer: {
|
|
509
|
+
domainMustBeGreaterThanZero: "Domain {{domain}} must be greater than 0",
|
|
510
|
+
tweakMustBeGreaterThanOrEqualToZero: "Tweak {{tweak}} must be greater than or equal to 0",
|
|
511
|
+
valueMustBeGreaterThanOrEqualToZero: "Value {{value}} must be greater than or equal to 0",
|
|
512
|
+
valueMustBeLessThan: "Value {{value}} must be less than {{domain}}",
|
|
513
|
+
minValueMustBeGreaterThanOrEqualToZero: "Minimum value {{minValue}} must be greater than or equal to 0",
|
|
514
|
+
maxValueMustBeLessThan: "Maximum value {{maxValue}} must be less than {{domain}}"
|
|
515
|
+
},
|
|
516
|
+
RegExpValidator: {
|
|
517
|
+
stringDoesNotMatchPattern: "String {{s}} does not match pattern"
|
|
518
|
+
},
|
|
519
|
+
CharacterSetValidator: {
|
|
520
|
+
firstZeroFirstCharacter: "Character set must support zero as first character",
|
|
521
|
+
allNumericAllNumericCharacters: "Character set must support all numeric characters in sequence",
|
|
522
|
+
stringMustNotBeAllNumeric: "String must not be all numeric",
|
|
523
|
+
lengthMustBeGreaterThanOrEqualTo: "Length {{length}} must be greater than or equal to {{minimumLength}}",
|
|
524
|
+
lengthMustBeLessThanOrEqualTo: "Length {{length}} must be less than or equal to {{maximumLength}}",
|
|
525
|
+
lengthMustBeEqualTo: "Length {{length}} must be equal to {{exactLength}}",
|
|
526
|
+
lengthOfComponentMustBeGreaterThanOrEqualTo: "Length {{length}} of {{component}} must be greater than or equal to {{minimumLength}}",
|
|
527
|
+
lengthOfComponentMustBeLessThanOrEqualTo: "Length {{length}} of {{component}} must be less than or equal to {{maximumLength}}",
|
|
528
|
+
lengthOfComponentMustBeEqualTo: "Length {{length}} of {{component}} must be equal to {{exactLength}}",
|
|
529
|
+
invalidCharacterAtPosition: "Invalid character '{{c}}' at position {{position}}",
|
|
530
|
+
invalidCharacterAtPositionOfComponent: "Invalid character '{{c}}' at position {{position}} of {{component}}",
|
|
531
|
+
exclusionNotSupported: "Exclusion value of {{exclusion}} is not supported",
|
|
532
|
+
invalidTweakWithAllNumericExclusion: "Tweak must not be used with all-numeric exclusion",
|
|
533
|
+
endSequenceValueMustBeLessThanOrEqualTo: "End sequence value (start sequence value + count - 1) must be less than {{domain}}"
|
|
534
|
+
},
|
|
535
|
+
RecordValidator: {
|
|
536
|
+
typeNameKeyNotFound: '{{typeName}} "{{key}}" not found'
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
|
|
540
|
+
// src/locale/fr/locale_strings.ts
|
|
541
|
+
var localeStrings2 = {
|
|
542
|
+
Transformer: {
|
|
543
|
+
domainMustBeGreaterThanZero: "Le domaine {{domain}} doit \xEAtre sup\xE9rieur \xE0 0",
|
|
544
|
+
tweakMustBeGreaterThanOrEqualToZero: "Le r\xE9glage {{tweak}} doit \xEAtre sup\xE9rieur ou \xE9gal \xE0 0",
|
|
545
|
+
valueMustBeGreaterThanOrEqualToZero: "La valeur {{value}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 0",
|
|
546
|
+
valueMustBeLessThan: "La valeur {{value}} doit \xEAtre inf\xE9rieure \xE0 {{domain}}",
|
|
547
|
+
minValueMustBeGreaterThanOrEqualToZero: "La valeur minimale {{minValue}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 0",
|
|
548
|
+
maxValueMustBeLessThan: "La valeur maximale {{maxValue}} doit \xEAtre inf\xE9rieure \xE0 {{domain}}"
|
|
549
|
+
},
|
|
550
|
+
RegExpValidator: {
|
|
551
|
+
stringDoesNotMatchPattern: "La cha\xEEne {{s}} ne correspond pas au mod\xE8le"
|
|
552
|
+
},
|
|
553
|
+
CharacterSetValidator: {
|
|
554
|
+
firstZeroFirstCharacter: "Le jeu de caract\xE8res doit prendre en charge z\xE9ro comme premier caract\xE8re",
|
|
555
|
+
allNumericAllNumericCharacters: "Le jeu de caract\xE8res doit prendre en charge tous les caract\xE8res num\xE9riques en s\xE9quence",
|
|
556
|
+
stringMustNotBeAllNumeric: "La cha\xEEne ne doit pas \xEAtre enti\xE8rement num\xE9rique",
|
|
557
|
+
lengthMustBeGreaterThanOrEqualTo: "La longueur {{length}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 {{minimumLength}}",
|
|
558
|
+
lengthMustBeLessThanOrEqualTo: "La longueur {{length}} doit \xEAtre inf\xE9rieure ou \xE9gale \xE0 {{maximumLength}}",
|
|
559
|
+
lengthMustBeEqualTo: "La longueur {{length}} doit \xEAtre \xE9gale \xE0 {{exactLength}}",
|
|
560
|
+
lengthOfComponentMustBeGreaterThanOrEqualTo: "La longueur {{length}} de {{component}} doit \xEAtre sup\xE9rieure ou \xE9gale \xE0 {{minimumLength}}",
|
|
561
|
+
lengthOfComponentMustBeLessThanOrEqualTo: "La longueur {{length}} de {{component}} doit \xEAtre inf\xE9rieure ou \xE9gale \xE0 {{maximumLength}}",
|
|
562
|
+
lengthOfComponentMustBeEqualTo: "La longueur {{length}} de {{component}} doit \xEAtre \xE9gale \xE0 {{exactLength}}",
|
|
563
|
+
invalidCharacterAtPosition: "Caract\xE8re non valide '{{c}}' \xE0 la position {{position}}",
|
|
564
|
+
invalidCharacterAtPositionOfComponent: "Caract\xE8re non valide '{{c}}' \xE0 la position {{position}} de {{component}}",
|
|
565
|
+
exclusionNotSupported: "La valeur d'exclusion de {{exclusion}} n'est pas prise en charge",
|
|
566
|
+
invalidTweakWithAllNumericExclusion: "Le r\xE9glage ne doit pas \xEAtre utilis\xE9 avec une exclusion enti\xE8rement num\xE9rique",
|
|
567
|
+
endSequenceValueMustBeLessThanOrEqualTo: "La valeur de la s\xE9quence de fin (valeur de la s\xE9quence de d\xE9but + nombre - 1) doit \xEAtre inf\xE9rieure \xE0 {{domaine}}"
|
|
568
|
+
},
|
|
569
|
+
RecordValidator: {
|
|
570
|
+
typeNameKeyNotFound: '{{typeName}} "{{key}}" introuvable'
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
|
|
574
|
+
// src/locale/i18n.ts
|
|
575
|
+
var utilityNS = "aidct_utility";
|
|
576
|
+
i18nAssertValidResources(localeStrings, "fr", localeStrings2);
|
|
577
|
+
i18nAddResourceBundle("en", utilityNS, localeStrings);
|
|
578
|
+
i18nAddResourceBundle("fr", utilityNS, localeStrings2);
|
|
579
|
+
var i18n_default = i18next;
|
|
580
|
+
|
|
581
|
+
// src/transformer.ts
|
|
582
|
+
var Transformer = class _Transformer {
|
|
583
|
+
/**
|
|
584
|
+
* Transformers cache, mapping a domain to another map, which maps an optional tweak to a transformer.
|
|
585
|
+
*/
|
|
586
|
+
static TRANSFORMER_MAPS_MAP = /* @__PURE__ */ new Map();
|
|
587
|
+
/**
|
|
588
|
+
* Domain.
|
|
589
|
+
*/
|
|
590
|
+
_domain;
|
|
591
|
+
/**
|
|
592
|
+
* Constructor.
|
|
593
|
+
*
|
|
594
|
+
* @param domain
|
|
595
|
+
* Domain.
|
|
596
|
+
*/
|
|
597
|
+
constructor(domain) {
|
|
598
|
+
this._domain = BigInt(domain);
|
|
599
|
+
if (this._domain <= 0n) {
|
|
600
|
+
throw new RangeError(i18n_default.t("Transformer.domainMustBeGreaterThanZero", {
|
|
601
|
+
ns: utilityNS,
|
|
602
|
+
domain
|
|
603
|
+
}));
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Get a transformer, constructing it if necessary. The type returned is {@link IdentityTransformer} if tweak is
|
|
608
|
+
* undefined, {@link EncryptionTransformer} if tweak is defined. Note that although an {@link EncryptionTransformer}
|
|
609
|
+
* with a zero tweak operates as an {@link IdentityTransformer}, {@link EncryptionTransformer} is still the type
|
|
610
|
+
* returned if a zero tweak is explicitly specified.
|
|
611
|
+
*
|
|
612
|
+
* @param domain
|
|
613
|
+
* Domain.
|
|
614
|
+
*
|
|
615
|
+
* @param tweak
|
|
616
|
+
* Tweak.
|
|
617
|
+
*
|
|
618
|
+
* @returns
|
|
619
|
+
* {@link IdentityTransformer} if tweak is undefined, {@link EncryptionTransformer} if tweak is defined.
|
|
620
|
+
*/
|
|
621
|
+
static get(domain, tweak) {
|
|
622
|
+
const domainN = BigInt(domain);
|
|
623
|
+
let transformersMap = _Transformer.TRANSFORMER_MAPS_MAP.get(domainN);
|
|
624
|
+
if (transformersMap === void 0) {
|
|
625
|
+
transformersMap = /* @__PURE__ */ new Map();
|
|
626
|
+
_Transformer.TRANSFORMER_MAPS_MAP.set(domainN, transformersMap);
|
|
627
|
+
}
|
|
628
|
+
const tweakN = tweak === void 0 ? void 0 : BigInt(tweak);
|
|
629
|
+
let transformer = transformersMap.get(tweakN);
|
|
630
|
+
if (transformer === void 0) {
|
|
631
|
+
transformer = tweakN === void 0 ? new IdentityTransformer(domainN) : new EncryptionTransformer(domainN, tweakN);
|
|
632
|
+
transformersMap.set(tweakN, transformer);
|
|
633
|
+
}
|
|
634
|
+
return transformer;
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Get the domain.
|
|
638
|
+
*/
|
|
639
|
+
get domain() {
|
|
640
|
+
return this._domain;
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Validate that a value is within the domain.
|
|
644
|
+
*
|
|
645
|
+
* @param value
|
|
646
|
+
* Value.
|
|
647
|
+
*/
|
|
648
|
+
validate(value) {
|
|
649
|
+
if (value < 0n) {
|
|
650
|
+
throw new RangeError(i18n_default.t("Transformer.valueMustBeGreaterThanOrEqualToZero", {
|
|
651
|
+
ns: utilityNS,
|
|
652
|
+
value
|
|
653
|
+
}));
|
|
654
|
+
}
|
|
655
|
+
if (value >= this.domain) {
|
|
656
|
+
throw new RangeError(i18n_default.t("Transformer.valueMustBeLessThan", {
|
|
657
|
+
ns: utilityNS,
|
|
658
|
+
value,
|
|
659
|
+
domain: this.domain
|
|
660
|
+
}));
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
// eslint-disable-next-line jsdoc/require-jsdoc -- Implementation of overloaded signatures.
|
|
664
|
+
forward(valueOrValues, transformerCallback) {
|
|
665
|
+
let result;
|
|
666
|
+
if (typeof valueOrValues !== "object") {
|
|
667
|
+
const valueN = BigInt(valueOrValues);
|
|
668
|
+
this.validate(valueN);
|
|
669
|
+
const transformedValue = this.doForward(valueN);
|
|
670
|
+
result = transformerCallback === void 0 ? transformedValue : transformerCallback(transformedValue, 0);
|
|
671
|
+
} else if (valueOrValues instanceof Sequencer) {
|
|
672
|
+
if (valueOrValues.minValue < 0n) {
|
|
673
|
+
throw new RangeError(i18n_default.t("Transformer.minValueMustBeGreaterThanOrEqualToZero", {
|
|
674
|
+
ns: utilityNS,
|
|
675
|
+
minValue: valueOrValues.minValue
|
|
676
|
+
}));
|
|
677
|
+
}
|
|
678
|
+
if (valueOrValues.maxValue >= this.domain) {
|
|
679
|
+
throw new RangeError(i18n_default.t("Transformer.maxValueMustBeLessThan", {
|
|
680
|
+
ns: utilityNS,
|
|
681
|
+
maxValue: valueOrValues.maxValue,
|
|
682
|
+
domain: this.domain
|
|
683
|
+
}));
|
|
684
|
+
}
|
|
685
|
+
result = transformerCallback === void 0 ? IteratorProxy.from(valueOrValues).map((value) => this.doForward(value)) : IteratorProxy.from(valueOrValues).map((value, index) => transformerCallback(this.doForward(value), index));
|
|
686
|
+
} else {
|
|
687
|
+
result = transformerCallback === void 0 ? IteratorProxy.from(valueOrValues).map((value) => {
|
|
688
|
+
const valueN = BigInt(value);
|
|
689
|
+
this.validate(valueN);
|
|
690
|
+
return this.doForward(valueN);
|
|
691
|
+
}) : IteratorProxy.from(valueOrValues).map((value, index) => {
|
|
692
|
+
const valueN = BigInt(value);
|
|
693
|
+
this.validate(valueN);
|
|
694
|
+
return transformerCallback(this.doForward(valueN), index);
|
|
695
|
+
});
|
|
696
|
+
}
|
|
697
|
+
return result;
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* Transform a value in reverse.
|
|
701
|
+
*
|
|
702
|
+
* @param transformedValue
|
|
703
|
+
* Transformed value.
|
|
704
|
+
*
|
|
705
|
+
* @returns
|
|
706
|
+
* Value.
|
|
707
|
+
*/
|
|
708
|
+
reverse(transformedValue) {
|
|
709
|
+
const transformedValueN = BigInt(transformedValue);
|
|
710
|
+
this.validate(transformedValueN);
|
|
711
|
+
return this.doReverse(transformedValueN);
|
|
712
|
+
}
|
|
713
|
+
};
|
|
714
|
+
var IdentityTransformer = class extends Transformer {
|
|
715
|
+
/**
|
|
716
|
+
* @inheritDoc
|
|
717
|
+
*/
|
|
718
|
+
doForward(value) {
|
|
719
|
+
return value;
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* @inheritDoc
|
|
723
|
+
*/
|
|
724
|
+
doReverse(transformedValue) {
|
|
725
|
+
return transformedValue;
|
|
726
|
+
}
|
|
727
|
+
};
|
|
728
|
+
var EncryptionTransformer = class _EncryptionTransformer extends Transformer {
|
|
729
|
+
/**
|
|
730
|
+
* Individual bits, pre-calculated for performance.
|
|
731
|
+
*/
|
|
732
|
+
static BITS = new Uint8Array([
|
|
733
|
+
1,
|
|
734
|
+
2,
|
|
735
|
+
4,
|
|
736
|
+
8,
|
|
737
|
+
16,
|
|
738
|
+
32,
|
|
739
|
+
64,
|
|
740
|
+
128
|
|
741
|
+
]);
|
|
742
|
+
/**
|
|
743
|
+
* Inverse individual bits, pre-calculated for performance.
|
|
744
|
+
*/
|
|
745
|
+
static INVERSE_BITS = new Uint8Array([
|
|
746
|
+
254,
|
|
747
|
+
253,
|
|
748
|
+
251,
|
|
749
|
+
247,
|
|
750
|
+
239,
|
|
751
|
+
223,
|
|
752
|
+
191,
|
|
753
|
+
127
|
|
754
|
+
]);
|
|
755
|
+
/**
|
|
756
|
+
* Number of bytes covered by the domain.
|
|
757
|
+
*/
|
|
758
|
+
_domainBytes;
|
|
759
|
+
/**
|
|
760
|
+
* Tweak.
|
|
761
|
+
*/
|
|
762
|
+
_tweak;
|
|
763
|
+
/**
|
|
764
|
+
* Xor bytes array generated from the domain and tweak.
|
|
765
|
+
*/
|
|
766
|
+
_xorBytes;
|
|
767
|
+
/**
|
|
768
|
+
* Bits array generated from the domain and tweak.
|
|
769
|
+
*/
|
|
770
|
+
_bits;
|
|
771
|
+
/**
|
|
772
|
+
* Inverse bits array generated from the domain and tweak.
|
|
773
|
+
*/
|
|
774
|
+
_inverseBits;
|
|
775
|
+
/**
|
|
776
|
+
* Number of rounds (length of arrays) generated from the domain and tweak.
|
|
777
|
+
*/
|
|
778
|
+
_rounds;
|
|
779
|
+
/**
|
|
780
|
+
* Constructor.
|
|
781
|
+
*
|
|
782
|
+
* @param domain
|
|
783
|
+
* Domain.
|
|
784
|
+
*
|
|
785
|
+
* @param tweak
|
|
786
|
+
* Tweak.
|
|
787
|
+
*/
|
|
788
|
+
constructor(domain, tweak) {
|
|
789
|
+
super(domain);
|
|
790
|
+
if (tweak < 0n) {
|
|
791
|
+
throw new RangeError(i18n_default.t("Transformer.tweakMustBeGreaterThanOrEqualToZero", {
|
|
792
|
+
ns: utilityNS,
|
|
793
|
+
tweak
|
|
794
|
+
}));
|
|
795
|
+
}
|
|
796
|
+
let domainBytes = 0;
|
|
797
|
+
for (let reducedDomainMinusOne = this.domain - 1n; reducedDomainMinusOne !== 0n; reducedDomainMinusOne = reducedDomainMinusOne >> 8n) {
|
|
798
|
+
domainBytes++;
|
|
799
|
+
}
|
|
800
|
+
this._domainBytes = domainBytes;
|
|
801
|
+
this._tweak = BigInt(tweak);
|
|
802
|
+
const xorBytes = new Array();
|
|
803
|
+
const bits = new Array();
|
|
804
|
+
const inverseBits = new Array();
|
|
805
|
+
for (let reducedKey = this.domain * this.tweak * 603868999n; reducedKey !== 0n; reducedKey = reducedKey >> 8n) {
|
|
806
|
+
const keyByte = Number(reducedKey & 0xFFn);
|
|
807
|
+
xorBytes.unshift(keyByte);
|
|
808
|
+
const bitNumber = keyByte & 7;
|
|
809
|
+
bits.push(_EncryptionTransformer.BITS[bitNumber]);
|
|
810
|
+
inverseBits.push(_EncryptionTransformer.INVERSE_BITS[bitNumber]);
|
|
811
|
+
}
|
|
812
|
+
if (domainBytes === 1) {
|
|
813
|
+
const domainMask = _EncryptionTransformer.BITS.filter((bit) => bit < domain).reduce((accumulator, bit) => accumulator | bit, 0);
|
|
814
|
+
this._xorBytes = new Uint8Array([xorBytes.reduce((accumulator, xorByte) => accumulator ^ xorByte, 0) & domainMask]);
|
|
815
|
+
this._bits = new Uint8Array([_EncryptionTransformer.BITS[0]]);
|
|
816
|
+
this._inverseBits = new Uint8Array([_EncryptionTransformer.INVERSE_BITS[0]]);
|
|
817
|
+
this._rounds = 1;
|
|
818
|
+
} else {
|
|
819
|
+
this._xorBytes = new Uint8Array(xorBytes);
|
|
820
|
+
this._bits = new Uint8Array(bits);
|
|
821
|
+
this._inverseBits = new Uint8Array(inverseBits);
|
|
822
|
+
this._rounds = xorBytes.length;
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Get the tweak.
|
|
827
|
+
*/
|
|
828
|
+
get tweak() {
|
|
829
|
+
return this._tweak;
|
|
830
|
+
}
|
|
831
|
+
/**
|
|
832
|
+
* Convert a value to a byte array big enough to handle the entire domain.
|
|
833
|
+
*
|
|
834
|
+
* @param value
|
|
835
|
+
* Value.
|
|
836
|
+
*
|
|
837
|
+
* @returns
|
|
838
|
+
* Big-endian byte array equivalent to the value.
|
|
839
|
+
*/
|
|
840
|
+
valueToBytes(value) {
|
|
841
|
+
const bytes = new Uint8Array(this._domainBytes);
|
|
842
|
+
let reducedValue = value;
|
|
843
|
+
for (let index = this._domainBytes - 1; index >= 0; index--) {
|
|
844
|
+
bytes[index] = Number(reducedValue & 0xFFn);
|
|
845
|
+
reducedValue = reducedValue >> 8n;
|
|
846
|
+
}
|
|
847
|
+
return bytes;
|
|
848
|
+
}
|
|
849
|
+
/**
|
|
850
|
+
* Convert a byte array to a value.
|
|
851
|
+
*
|
|
852
|
+
* @param bytes
|
|
853
|
+
* Big-endian byte array equivalent to the value.
|
|
854
|
+
*
|
|
855
|
+
* @returns
|
|
856
|
+
* Value.
|
|
857
|
+
*/
|
|
858
|
+
static bytesToValue(bytes) {
|
|
859
|
+
return bytes.reduce((accumulator, byte) => accumulator << 8n | BigInt(byte), 0n);
|
|
860
|
+
}
|
|
861
|
+
/**
|
|
862
|
+
* Shuffle a byte array.
|
|
863
|
+
*
|
|
864
|
+
* The input array to the forward operation (output from the reverse operation) is `bytes` and the output array from
|
|
865
|
+
* the forward operation (input to the reverse operation) is `bytes'`.
|
|
866
|
+
*
|
|
867
|
+
* The shuffle operation starts by testing the bit at `bits[round]` for each `byte` in `bytes`. The indexes for all
|
|
868
|
+
* bytes with that bit set are put into one array (`shuffleIndexes1`) and the rest are put into another
|
|
869
|
+
* (`shuffleIndexes0`). The two arrays are concatenated and used to shuffle the input array, using their values
|
|
870
|
+
* (`shuffleIndex`) and the indexes of those values (`index`) in the concatenated array.
|
|
871
|
+
*
|
|
872
|
+
* Forward shuffling moves the entry at `shuffleIndex` to the `index` position.
|
|
873
|
+
*
|
|
874
|
+
* Reverse shuffling moves the entry at `index` to the `shuffleIndex` position.
|
|
875
|
+
*
|
|
876
|
+
* As each byte is moved, the bit at `bits[round]` is preserved in its original position. This ensures that the
|
|
877
|
+
* process is reversible.
|
|
878
|
+
*
|
|
879
|
+
* @param bytes
|
|
880
|
+
* Byte array.
|
|
881
|
+
*
|
|
882
|
+
* @param round
|
|
883
|
+
* Round number.
|
|
884
|
+
*
|
|
885
|
+
* @param forward
|
|
886
|
+
* True if operating forward (encrypting), false if operating in reverse (decrypting).
|
|
887
|
+
*
|
|
888
|
+
* @returns
|
|
889
|
+
* Shuffled byte array.
|
|
890
|
+
*/
|
|
891
|
+
shuffle(bytes, round, forward) {
|
|
892
|
+
const bytesLength = bytes.length;
|
|
893
|
+
const determinants = new Uint8Array(bytesLength);
|
|
894
|
+
const shuffleIndexes1 = new Array();
|
|
895
|
+
const shuffleIndexes0 = new Array();
|
|
896
|
+
const bit = this._bits[round];
|
|
897
|
+
bytes.forEach((byte, index) => {
|
|
898
|
+
const determinant = byte & bit;
|
|
899
|
+
determinants[index] = determinant;
|
|
900
|
+
(determinant !== 0 ? shuffleIndexes1 : shuffleIndexes0).push(index);
|
|
901
|
+
});
|
|
902
|
+
const inverseBit = this._inverseBits[round];
|
|
903
|
+
const shuffleBytes = new Uint8Array(bytesLength);
|
|
904
|
+
[...shuffleIndexes1, ...shuffleIndexes0].forEach((shuffleIndex, index) => {
|
|
905
|
+
if (forward) {
|
|
906
|
+
shuffleBytes[index] = bytes[shuffleIndex] & inverseBit | determinants[index];
|
|
907
|
+
} else {
|
|
908
|
+
shuffleBytes[shuffleIndex] = bytes[index] & inverseBit | determinants[shuffleIndex];
|
|
909
|
+
}
|
|
910
|
+
});
|
|
911
|
+
return shuffleBytes;
|
|
912
|
+
}
|
|
913
|
+
/**
|
|
914
|
+
* Xor a byte array.
|
|
915
|
+
*
|
|
916
|
+
* The input array to the forward operation (output from the reverse operation) is `bytes` and the output array from
|
|
917
|
+
* the forward operation (input to the reverse operation) is `bytes'`.
|
|
918
|
+
*
|
|
919
|
+
* Forward:
|
|
920
|
+
* - `bytes'[0] = bytes[0] ^ xorBytes[round]`
|
|
921
|
+
* - `bytes'[1] = bytes[1] ^ bytes'[0]`
|
|
922
|
+
* - `bytes'[2] = bytes[2] ^ bytes'[1]`
|
|
923
|
+
* - `...`
|
|
924
|
+
* - `bytes'[domainBytes - 1] = bytes[domainBytes - 1] ^ bytes'[domainBytes - 2]`
|
|
925
|
+
*
|
|
926
|
+
* Reverse:
|
|
927
|
+
* - `bytes[0] = bytes'[0] ^ xorBytes[round]`
|
|
928
|
+
* - `bytes[1] = bytes'[1] ^ bytes'[0]`
|
|
929
|
+
* - `bytes[2] = bytes'[2] ^ bytes'[1]`
|
|
930
|
+
* - `...`
|
|
931
|
+
* - `bytes[domainBytes - 1] = bytes'[domainBytes - 1] ^ bytes'[domainBytes - 2]`
|
|
932
|
+
*
|
|
933
|
+
* @param bytes
|
|
934
|
+
* Byte array.
|
|
935
|
+
*
|
|
936
|
+
* @param round
|
|
937
|
+
* Round number.
|
|
938
|
+
*
|
|
939
|
+
* @param forward
|
|
940
|
+
* True if operating forward (encrypting), false if operating in reverse (decrypting).
|
|
941
|
+
*
|
|
942
|
+
* @returns
|
|
943
|
+
* Xored byte array.
|
|
944
|
+
*/
|
|
945
|
+
xor(bytes, round, forward) {
|
|
946
|
+
let cumulativeXorByte = this._xorBytes[round];
|
|
947
|
+
return bytes.map((byte) => {
|
|
948
|
+
const xorByte = byte ^ cumulativeXorByte;
|
|
949
|
+
cumulativeXorByte = forward ? xorByte : byte;
|
|
950
|
+
return xorByte;
|
|
951
|
+
});
|
|
952
|
+
}
|
|
953
|
+
/**
|
|
954
|
+
* @inheritDoc
|
|
955
|
+
*/
|
|
956
|
+
doForward(value) {
|
|
957
|
+
let bytes = this.valueToBytes(value);
|
|
958
|
+
let transformedValue;
|
|
959
|
+
do {
|
|
960
|
+
for (let round = 0; round < this._rounds; round++) {
|
|
961
|
+
bytes = this.xor(this.shuffle(bytes, round, true), round, true);
|
|
962
|
+
}
|
|
963
|
+
transformedValue = _EncryptionTransformer.bytesToValue(bytes);
|
|
964
|
+
} while (transformedValue >= this.domain);
|
|
965
|
+
return transformedValue;
|
|
966
|
+
}
|
|
967
|
+
/**
|
|
968
|
+
* @inheritDoc
|
|
969
|
+
*/
|
|
970
|
+
doReverse(transformedValue) {
|
|
971
|
+
let bytes = this.valueToBytes(transformedValue);
|
|
972
|
+
let value;
|
|
973
|
+
do {
|
|
974
|
+
for (let round = this._rounds - 1; round >= 0; round--) {
|
|
975
|
+
bytes = this.shuffle(this.xor(bytes, round, false), round, false);
|
|
976
|
+
}
|
|
977
|
+
value = _EncryptionTransformer.bytesToValue(bytes);
|
|
978
|
+
} while (value >= this.domain);
|
|
979
|
+
return value;
|
|
980
|
+
}
|
|
981
|
+
};
|
|
982
|
+
|
|
983
|
+
// src/reg_exp.ts
|
|
984
|
+
var RegExpValidator = class {
|
|
985
|
+
/**
|
|
986
|
+
* Regular expression.
|
|
987
|
+
*/
|
|
988
|
+
_regExp;
|
|
989
|
+
/**
|
|
990
|
+
* Constructor.
|
|
991
|
+
*
|
|
992
|
+
* @param regExp
|
|
993
|
+
* Regular expression. See {@link RegExpValidator | class documentation} for notes.
|
|
994
|
+
*/
|
|
995
|
+
constructor(regExp) {
|
|
996
|
+
this._regExp = regExp;
|
|
997
|
+
}
|
|
998
|
+
/**
|
|
999
|
+
* Get the regular expression.
|
|
1000
|
+
*/
|
|
1001
|
+
get regExp() {
|
|
1002
|
+
return this._regExp;
|
|
1003
|
+
}
|
|
1004
|
+
/**
|
|
1005
|
+
* Create an error message for a string. The generic error message is sufficient for many use cases but a more
|
|
1006
|
+
* domain-specific error message, possibly including the pattern itself, is often required.
|
|
1007
|
+
*
|
|
1008
|
+
* @param s
|
|
1009
|
+
* String.
|
|
1010
|
+
*
|
|
1011
|
+
* @returns
|
|
1012
|
+
* Error message.
|
|
1013
|
+
*/
|
|
1014
|
+
createErrorMessage(s) {
|
|
1015
|
+
return i18n_default.t("RegExpValidator.stringDoesNotMatchPattern", {
|
|
1016
|
+
ns: utilityNS,
|
|
1017
|
+
s
|
|
1018
|
+
});
|
|
1019
|
+
}
|
|
1020
|
+
/**
|
|
1021
|
+
* @inheritDoc
|
|
1022
|
+
*/
|
|
1023
|
+
validate(s) {
|
|
1024
|
+
if (!this._regExp.test(s)) {
|
|
1025
|
+
throw new RangeError(this.createErrorMessage(s));
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
};
|
|
1029
|
+
|
|
1030
|
+
// src/record.ts
|
|
1031
|
+
var RecordValidator = class {
|
|
1032
|
+
/**
|
|
1033
|
+
* Type name for error message.
|
|
1034
|
+
*/
|
|
1035
|
+
_typeName;
|
|
1036
|
+
/**
|
|
1037
|
+
* Record in which to look up keys.
|
|
1038
|
+
*/
|
|
1039
|
+
_record;
|
|
1040
|
+
/**
|
|
1041
|
+
* Constructor.
|
|
1042
|
+
*
|
|
1043
|
+
* @param typeName
|
|
1044
|
+
* Type name for error message.
|
|
1045
|
+
*
|
|
1046
|
+
* @param record
|
|
1047
|
+
* Record in which to look up keys.
|
|
1048
|
+
*/
|
|
1049
|
+
constructor(typeName, record) {
|
|
1050
|
+
this._typeName = typeName;
|
|
1051
|
+
this._record = record;
|
|
1052
|
+
}
|
|
1053
|
+
/**
|
|
1054
|
+
* Get the type name.
|
|
1055
|
+
*/
|
|
1056
|
+
get typeName() {
|
|
1057
|
+
return this._typeName;
|
|
1058
|
+
}
|
|
1059
|
+
/**
|
|
1060
|
+
* Get the record.
|
|
1061
|
+
*/
|
|
1062
|
+
get record() {
|
|
1063
|
+
return this._record;
|
|
1064
|
+
}
|
|
1065
|
+
/**
|
|
1066
|
+
* Validate a key by looking it up in the record.
|
|
1067
|
+
*
|
|
1068
|
+
* @param key
|
|
1069
|
+
* Record key.
|
|
1070
|
+
*/
|
|
1071
|
+
validate(key) {
|
|
1072
|
+
if (this.record[key] === void 0) {
|
|
1073
|
+
throw new RangeError(i18n_default.t("RecordValidator.typeNameKeyNotFound", {
|
|
1074
|
+
ns: utilityNS,
|
|
1075
|
+
typeName: this.typeName,
|
|
1076
|
+
key
|
|
1077
|
+
}));
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
};
|
|
1081
|
+
|
|
1082
|
+
// src/character_set.ts
|
|
1083
|
+
var Exclusion = /* @__PURE__ */ ((Exclusion2) => {
|
|
1084
|
+
Exclusion2[Exclusion2["None"] = 0] = "None";
|
|
1085
|
+
Exclusion2[Exclusion2["FirstZero"] = 1] = "FirstZero";
|
|
1086
|
+
Exclusion2[Exclusion2["AllNumeric"] = 2] = "AllNumeric";
|
|
1087
|
+
return Exclusion2;
|
|
1088
|
+
})(Exclusion || {});
|
|
1089
|
+
var CharacterSetValidator = class _CharacterSetValidator {
|
|
1090
|
+
static NOT_ALL_NUMERIC_VALIDATOR = new class extends RegExpValidator {
|
|
1091
|
+
/**
|
|
1092
|
+
* Create an error message for an all-numeric string.
|
|
1093
|
+
*
|
|
1094
|
+
* @param _s
|
|
1095
|
+
* String.
|
|
1096
|
+
*
|
|
1097
|
+
* @returns
|
|
1098
|
+
* Error message.
|
|
1099
|
+
*/
|
|
1100
|
+
createErrorMessage(_s) {
|
|
1101
|
+
return i18n_default.t("CharacterSetValidator.stringMustNotBeAllNumeric", {
|
|
1102
|
+
ns: utilityNS
|
|
1103
|
+
});
|
|
1104
|
+
}
|
|
1105
|
+
}(/\D/);
|
|
1106
|
+
/**
|
|
1107
|
+
* Character set.
|
|
1108
|
+
*/
|
|
1109
|
+
_characterSet;
|
|
1110
|
+
/**
|
|
1111
|
+
* Character set map, mapping each character in the character set to its index such that
|
|
1112
|
+
* `_characterSetMap.get(_characterSet[index]) === index`.
|
|
1113
|
+
*/
|
|
1114
|
+
_characterSetMap;
|
|
1115
|
+
/**
|
|
1116
|
+
* Exclusions supported by the character set.
|
|
1117
|
+
*/
|
|
1118
|
+
_exclusionSupport;
|
|
1119
|
+
/**
|
|
1120
|
+
* Constructor.
|
|
1121
|
+
*
|
|
1122
|
+
* @param characterSet
|
|
1123
|
+
* Character set. Each element is a single-character string, unique within the array, that defines the character
|
|
1124
|
+
* set.
|
|
1125
|
+
*
|
|
1126
|
+
* @param exclusionSupport
|
|
1127
|
+
* Exclusions supported by the character set. All character sets implicitly support {@link Exclusion.None}.
|
|
1128
|
+
*/
|
|
1129
|
+
constructor(characterSet, ...exclusionSupport) {
|
|
1130
|
+
this._characterSet = characterSet;
|
|
1131
|
+
const characterSetMap = /* @__PURE__ */ new Map();
|
|
1132
|
+
characterSet.forEach((c, index) => {
|
|
1133
|
+
characterSetMap.set(c, index);
|
|
1134
|
+
});
|
|
1135
|
+
this._characterSetMap = characterSetMap;
|
|
1136
|
+
this._exclusionSupport = exclusionSupport;
|
|
1137
|
+
}
|
|
1138
|
+
/**
|
|
1139
|
+
* Get the character set.
|
|
1140
|
+
*/
|
|
1141
|
+
get characterSet() {
|
|
1142
|
+
return this._characterSet;
|
|
1143
|
+
}
|
|
1144
|
+
/**
|
|
1145
|
+
* Get the character set size.
|
|
1146
|
+
*/
|
|
1147
|
+
get characterSetSize() {
|
|
1148
|
+
return this._characterSet.length;
|
|
1149
|
+
}
|
|
1150
|
+
/**
|
|
1151
|
+
* Get the exclusions supported by the character set.
|
|
1152
|
+
*/
|
|
1153
|
+
get exclusionSupport() {
|
|
1154
|
+
return this._exclusionSupport;
|
|
1155
|
+
}
|
|
1156
|
+
/**
|
|
1157
|
+
* Get the character at an index.
|
|
1158
|
+
*
|
|
1159
|
+
* @param index
|
|
1160
|
+
* Index into the character set.
|
|
1161
|
+
*
|
|
1162
|
+
* @returns
|
|
1163
|
+
* Character at the index.
|
|
1164
|
+
*/
|
|
1165
|
+
character(index) {
|
|
1166
|
+
return this._characterSet[index];
|
|
1167
|
+
}
|
|
1168
|
+
/**
|
|
1169
|
+
* Get the index for a character.
|
|
1170
|
+
*
|
|
1171
|
+
* @param c
|
|
1172
|
+
* Character.
|
|
1173
|
+
*
|
|
1174
|
+
* @returns
|
|
1175
|
+
* Index for the character or undefined if the character is not in the character set.
|
|
1176
|
+
*/
|
|
1177
|
+
characterIndex(c) {
|
|
1178
|
+
return this._characterSetMap.get(c);
|
|
1179
|
+
}
|
|
1180
|
+
/**
|
|
1181
|
+
* Get the indexes for all characters in a string.
|
|
1182
|
+
*
|
|
1183
|
+
* @param s
|
|
1184
|
+
* String.
|
|
1185
|
+
*
|
|
1186
|
+
* @returns
|
|
1187
|
+
* Array of indexes for each character or undefined if the character is not in the character set.
|
|
1188
|
+
*/
|
|
1189
|
+
characterIndexes(s) {
|
|
1190
|
+
return [...s].map((c) => this._characterSetMap.get(c));
|
|
1191
|
+
}
|
|
1192
|
+
/**
|
|
1193
|
+
* Convert a component definition to a string or undefined. Checks the type of the component and makes the callback
|
|
1194
|
+
* if required.
|
|
1195
|
+
*
|
|
1196
|
+
* @param component
|
|
1197
|
+
* Component definition as a string, callback, or undefined.
|
|
1198
|
+
*
|
|
1199
|
+
* @returns
|
|
1200
|
+
* Component as a string or undefined.
|
|
1201
|
+
*/
|
|
1202
|
+
static componentToString(component) {
|
|
1203
|
+
return typeof component === "function" ? component() : component;
|
|
1204
|
+
}
|
|
1205
|
+
/**
|
|
1206
|
+
* Validate that an exclusion is supported. If not, an error is thrown.
|
|
1207
|
+
*
|
|
1208
|
+
* @param exclusion
|
|
1209
|
+
* Exclusion.
|
|
1210
|
+
*/
|
|
1211
|
+
validateExclusion(exclusion) {
|
|
1212
|
+
if (exclusion !== 0 /* None */ && !this._exclusionSupport.includes(exclusion)) {
|
|
1213
|
+
throw new RangeError(i18n_default.t("CharacterSetValidator.exclusionNotSupported", {
|
|
1214
|
+
ns: utilityNS,
|
|
1215
|
+
exclusion
|
|
1216
|
+
}));
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
/**
|
|
1220
|
+
* Validate a string. If the string violates the character set or any of the character set validation parameters, an
|
|
1221
|
+
* error is thrown.
|
|
1222
|
+
*
|
|
1223
|
+
* @param s
|
|
1224
|
+
* String.
|
|
1225
|
+
*
|
|
1226
|
+
* @param validation
|
|
1227
|
+
* Character set validation parameters.
|
|
1228
|
+
*/
|
|
1229
|
+
validate(s, validation) {
|
|
1230
|
+
const length = s.length;
|
|
1231
|
+
const minimumLength = validation?.minimumLength;
|
|
1232
|
+
const maximumLength = validation?.maximumLength;
|
|
1233
|
+
if (minimumLength !== void 0 && length < minimumLength) {
|
|
1234
|
+
let errorMessage;
|
|
1235
|
+
if (maximumLength !== void 0 && maximumLength === minimumLength) {
|
|
1236
|
+
errorMessage = i18n_default.t(validation?.component === void 0 ? "CharacterSetValidator.lengthMustBeEqualTo" : "CharacterSetValidator.lengthOfComponentMustBeEqualTo", {
|
|
1237
|
+
ns: utilityNS,
|
|
1238
|
+
component: _CharacterSetValidator.componentToString(validation?.component),
|
|
1239
|
+
length,
|
|
1240
|
+
exactLength: minimumLength
|
|
1241
|
+
});
|
|
1242
|
+
} else {
|
|
1243
|
+
errorMessage = i18n_default.t(validation?.component === void 0 ? "CharacterSetValidator.lengthMustBeGreaterThanOrEqualTo" : "CharacterSetValidator.lengthOfComponentMustBeGreaterThanOrEqualTo", {
|
|
1244
|
+
ns: utilityNS,
|
|
1245
|
+
component: _CharacterSetValidator.componentToString(validation?.component),
|
|
1246
|
+
length,
|
|
1247
|
+
minimumLength
|
|
1248
|
+
});
|
|
1249
|
+
}
|
|
1250
|
+
throw new RangeError(errorMessage);
|
|
1251
|
+
}
|
|
1252
|
+
if (maximumLength !== void 0 && length > maximumLength) {
|
|
1253
|
+
throw new RangeError(i18n_default.t(validation?.component === void 0 ? "CharacterSetValidator.lengthMustBeLessThanOrEqualTo" : "CharacterSetValidator.lengthOfComponentMustBeLessThanOrEqualTo", {
|
|
1254
|
+
ns: utilityNS,
|
|
1255
|
+
component: _CharacterSetValidator.componentToString(validation?.component),
|
|
1256
|
+
length,
|
|
1257
|
+
maximumLength
|
|
1258
|
+
}));
|
|
1259
|
+
}
|
|
1260
|
+
const index = this.characterIndexes(s).findIndex((characterIndex) => characterIndex === void 0);
|
|
1261
|
+
if (index !== -1) {
|
|
1262
|
+
throw new RangeError(i18n_default.t(validation?.component === void 0 ? "CharacterSetValidator.invalidCharacterAtPosition" : "CharacterSetValidator.invalidCharacterAtPositionOfComponent", {
|
|
1263
|
+
ns: utilityNS,
|
|
1264
|
+
component: _CharacterSetValidator.componentToString(validation?.component),
|
|
1265
|
+
c: s.charAt(index),
|
|
1266
|
+
position: index + (validation?.positionOffset ?? 0) + 1
|
|
1267
|
+
}));
|
|
1268
|
+
}
|
|
1269
|
+
if (validation?.exclusion !== void 0) {
|
|
1270
|
+
this.validateExclusion(validation.exclusion);
|
|
1271
|
+
switch (validation.exclusion) {
|
|
1272
|
+
case 0 /* None */:
|
|
1273
|
+
break;
|
|
1274
|
+
case 1 /* FirstZero */:
|
|
1275
|
+
if (s.startsWith("0")) {
|
|
1276
|
+
throw new RangeError(i18n_default.t(validation.component === void 0 ? "CharacterSetValidator.invalidCharacterAtPosition" : "CharacterSetValidator.invalidCharacterAtPositionOfComponent", {
|
|
1277
|
+
ns: utilityNS,
|
|
1278
|
+
component: _CharacterSetValidator.componentToString(validation.component),
|
|
1279
|
+
c: "0",
|
|
1280
|
+
position: (validation.positionOffset ?? 0) + 1
|
|
1281
|
+
}));
|
|
1282
|
+
}
|
|
1283
|
+
break;
|
|
1284
|
+
case 2 /* AllNumeric */:
|
|
1285
|
+
_CharacterSetValidator.NOT_ALL_NUMERIC_VALIDATOR.validate(s);
|
|
1286
|
+
break;
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
};
|
|
1291
|
+
var CharacterSetCreator = class _CharacterSetCreator extends CharacterSetValidator {
|
|
1292
|
+
/**
|
|
1293
|
+
* Maximum string length supported.
|
|
1294
|
+
*/
|
|
1295
|
+
static MAXIMUM_STRING_LENGTH = 40;
|
|
1296
|
+
/**
|
|
1297
|
+
* Powers of 10 from 1 (`10**0`) to `10**MAXIMUM_STRING_LENGTH`.
|
|
1298
|
+
*/
|
|
1299
|
+
static _powersOf10 = _CharacterSetCreator.createPowersOf(10);
|
|
1300
|
+
/**
|
|
1301
|
+
* Create powers of a given base from 1 (`base**0`) to `base**MAXIMUM_STRING_LENGTH`.
|
|
1302
|
+
*
|
|
1303
|
+
* @param base
|
|
1304
|
+
* Number base.
|
|
1305
|
+
*
|
|
1306
|
+
* @returns
|
|
1307
|
+
* Array of powers of base.
|
|
1308
|
+
*/
|
|
1309
|
+
static createPowersOf(base) {
|
|
1310
|
+
const powersOf = new Array(this.MAXIMUM_STRING_LENGTH + 1);
|
|
1311
|
+
const baseN = BigInt(base);
|
|
1312
|
+
for (let index = 0, powerOf = 1n; index <= this.MAXIMUM_STRING_LENGTH; index++, powerOf *= baseN) {
|
|
1313
|
+
powersOf[index] = powerOf;
|
|
1314
|
+
}
|
|
1315
|
+
return powersOf;
|
|
1316
|
+
}
|
|
1317
|
+
/**
|
|
1318
|
+
* Get a power of 10.
|
|
1319
|
+
*
|
|
1320
|
+
* @param power
|
|
1321
|
+
* Power.
|
|
1322
|
+
*
|
|
1323
|
+
* @returns
|
|
1324
|
+
* `10**power`.
|
|
1325
|
+
*/
|
|
1326
|
+
static powerOf10(power) {
|
|
1327
|
+
return this._powersOf10[power];
|
|
1328
|
+
}
|
|
1329
|
+
/**
|
|
1330
|
+
* Character set size as big integer, cached for performance purposes.
|
|
1331
|
+
*/
|
|
1332
|
+
_characterSetSizeN;
|
|
1333
|
+
/**
|
|
1334
|
+
* Character set size minus 1 as big integer, cached for performance purposes.
|
|
1335
|
+
*/
|
|
1336
|
+
_characterSetSizeMinusOneN;
|
|
1337
|
+
/**
|
|
1338
|
+
* Domains for every length for every supported {@link Exclusion}.
|
|
1339
|
+
*/
|
|
1340
|
+
_exclusionDomains;
|
|
1341
|
+
/**
|
|
1342
|
+
* Values that would generate all zeros in the created string.
|
|
1343
|
+
*/
|
|
1344
|
+
_allZerosValues;
|
|
1345
|
+
/**
|
|
1346
|
+
* Constructor.
|
|
1347
|
+
*
|
|
1348
|
+
* @param characterSet
|
|
1349
|
+
* Character set. Each element is a single-character string, unique within the array, that defines the character
|
|
1350
|
+
* set.
|
|
1351
|
+
*
|
|
1352
|
+
* @param exclusionSupport
|
|
1353
|
+
* Exclusions supported by the character set. All character sets implicitly support {@link Exclusion.None}.
|
|
1354
|
+
*/
|
|
1355
|
+
constructor(characterSet, ...exclusionSupport) {
|
|
1356
|
+
super(characterSet, ...exclusionSupport);
|
|
1357
|
+
this._characterSetSizeN = BigInt(this.characterSetSize);
|
|
1358
|
+
this._characterSetSizeMinusOneN = BigInt(this.characterSetSize - 1);
|
|
1359
|
+
const exclusionDomains = [];
|
|
1360
|
+
const exclusionNoneDomains = _CharacterSetCreator.createPowersOf(this.characterSetSize);
|
|
1361
|
+
exclusionDomains[0 /* None */] = exclusionNoneDomains;
|
|
1362
|
+
if (exclusionSupport.includes(1 /* FirstZero */)) {
|
|
1363
|
+
if (characterSet[0] !== "0") {
|
|
1364
|
+
throw new RangeError(i18n_default.t("CharacterSetValidator.firstZeroFirstCharacter", {
|
|
1365
|
+
ns: utilityNS
|
|
1366
|
+
}));
|
|
1367
|
+
}
|
|
1368
|
+
const exclusionFirstZeroDomains = new Array(_CharacterSetCreator.MAXIMUM_STRING_LENGTH + 1);
|
|
1369
|
+
exclusionFirstZeroDomains[0] = 0n;
|
|
1370
|
+
for (let index = 1; index <= _CharacterSetCreator.MAXIMUM_STRING_LENGTH; index++) {
|
|
1371
|
+
exclusionFirstZeroDomains[index] = this._characterSetSizeMinusOneN * exclusionNoneDomains[index - 1];
|
|
1372
|
+
}
|
|
1373
|
+
exclusionDomains[1 /* FirstZero */] = exclusionFirstZeroDomains;
|
|
1374
|
+
}
|
|
1375
|
+
if (exclusionSupport.includes(2 /* AllNumeric */)) {
|
|
1376
|
+
let validateNumberIndexes2 = function(numberIndexes2) {
|
|
1377
|
+
let expectedNumberIndex = numberIndexes2[0];
|
|
1378
|
+
for (const numberIndex of numberIndexes2) {
|
|
1379
|
+
if (numberIndex === void 0 || numberIndex !== expectedNumberIndex) {
|
|
1380
|
+
throw new RangeError(i18n_default.t("CharacterSetValidator.allNumericAllNumericCharacters", {
|
|
1381
|
+
ns: utilityNS
|
|
1382
|
+
}));
|
|
1383
|
+
}
|
|
1384
|
+
expectedNumberIndex = numberIndex + 1;
|
|
1385
|
+
}
|
|
1386
|
+
};
|
|
1387
|
+
var validateNumberIndexes = validateNumberIndexes2;
|
|
1388
|
+
const exclusionAllNumericDomains = new Array(_CharacterSetCreator.MAXIMUM_STRING_LENGTH + 1);
|
|
1389
|
+
const numberIndexes = this.characterIndexes("0123456789");
|
|
1390
|
+
validateNumberIndexes2(numberIndexes);
|
|
1391
|
+
const zeroIndex = BigInt(numberIndexes[0]);
|
|
1392
|
+
const allZerosValues = new Array(_CharacterSetCreator.MAXIMUM_STRING_LENGTH + 1);
|
|
1393
|
+
let allZerosValue = 0n;
|
|
1394
|
+
for (let index = 0; index <= _CharacterSetCreator.MAXIMUM_STRING_LENGTH; index++) {
|
|
1395
|
+
exclusionAllNumericDomains[index] = exclusionNoneDomains[index] - _CharacterSetCreator.powerOf10(index);
|
|
1396
|
+
allZerosValues[index] = allZerosValue;
|
|
1397
|
+
allZerosValue = allZerosValue * this._characterSetSizeN + zeroIndex;
|
|
1398
|
+
}
|
|
1399
|
+
this._allZerosValues = allZerosValues;
|
|
1400
|
+
exclusionDomains[2 /* AllNumeric */] = exclusionAllNumericDomains;
|
|
1401
|
+
} else {
|
|
1402
|
+
this._allZerosValues = [];
|
|
1403
|
+
}
|
|
1404
|
+
this._exclusionDomains = exclusionDomains;
|
|
1405
|
+
}
|
|
1406
|
+
/**
|
|
1407
|
+
* Get a power of character set size.
|
|
1408
|
+
*
|
|
1409
|
+
* @param power
|
|
1410
|
+
* Power.
|
|
1411
|
+
*
|
|
1412
|
+
* @returns
|
|
1413
|
+
* `characterSetSize**power`.
|
|
1414
|
+
*/
|
|
1415
|
+
powerOfSize(power) {
|
|
1416
|
+
return this._exclusionDomains[0 /* None */][power];
|
|
1417
|
+
}
|
|
1418
|
+
/**
|
|
1419
|
+
* Determine the shift required to skip all all-numeric strings up to the value.
|
|
1420
|
+
*
|
|
1421
|
+
* @param shiftForward
|
|
1422
|
+
* True to shift forward (value to string), false to shift backward (string to value).
|
|
1423
|
+
*
|
|
1424
|
+
* @param length
|
|
1425
|
+
* Length of string for which to get the all-numeric shift.
|
|
1426
|
+
*
|
|
1427
|
+
* @param value
|
|
1428
|
+
* Value for which to get the all-numeric shift.
|
|
1429
|
+
*
|
|
1430
|
+
* @returns
|
|
1431
|
+
* Shift required to skip all all-numeric strings.
|
|
1432
|
+
*/
|
|
1433
|
+
allNumericShift(shiftForward, length, value) {
|
|
1434
|
+
let shift;
|
|
1435
|
+
if (length === 0) {
|
|
1436
|
+
if (!shiftForward && value < 10n) {
|
|
1437
|
+
throw new RangeError(i18n_default.t("CharacterSetValidator.stringMustNotBeAllNumeric", {
|
|
1438
|
+
ns: utilityNS
|
|
1439
|
+
}));
|
|
1440
|
+
}
|
|
1441
|
+
shift = 10n;
|
|
1442
|
+
} else {
|
|
1443
|
+
const powerOfSize = this.powerOfSize(length);
|
|
1444
|
+
const powerOf10 = _CharacterSetCreator.powerOf10(length);
|
|
1445
|
+
const gap = shiftForward ? powerOfSize - powerOf10 : powerOfSize;
|
|
1446
|
+
const gaps = value / gap;
|
|
1447
|
+
if (gaps >= 10n) {
|
|
1448
|
+
shift = _CharacterSetCreator.powerOf10(length + 1);
|
|
1449
|
+
} else {
|
|
1450
|
+
shift = gaps * powerOf10 + this.allNumericShift(shiftForward, length - 1, value - gaps * gap);
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
return shift;
|
|
1454
|
+
}
|
|
1455
|
+
/**
|
|
1456
|
+
* Validate that a length is less than or equal to {@link MAXIMUM_STRING_LENGTH}. If not, an error is thrown.
|
|
1457
|
+
*
|
|
1458
|
+
* @param length
|
|
1459
|
+
* Length.
|
|
1460
|
+
*/
|
|
1461
|
+
validateLength(length) {
|
|
1462
|
+
if (length < 0) {
|
|
1463
|
+
throw new RangeError(i18n_default.t("CharacterSetValidator.lengthMustBeGreaterThanOrEqualTo", {
|
|
1464
|
+
ns: utilityNS,
|
|
1465
|
+
length,
|
|
1466
|
+
minimumLength: 0
|
|
1467
|
+
}));
|
|
1468
|
+
}
|
|
1469
|
+
if (length > _CharacterSetCreator.MAXIMUM_STRING_LENGTH) {
|
|
1470
|
+
throw new RangeError(i18n_default.t("CharacterSetValidator.lengthMustBeLessThanOrEqualTo", {
|
|
1471
|
+
ns: utilityNS,
|
|
1472
|
+
length,
|
|
1473
|
+
maximumLength: _CharacterSetCreator.MAXIMUM_STRING_LENGTH
|
|
1474
|
+
}));
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
/**
|
|
1478
|
+
* Create string(s) by mapping value(s) to the equivalent characters in the character set across the length of the
|
|
1479
|
+
* string.
|
|
1480
|
+
*
|
|
1481
|
+
* @param length
|
|
1482
|
+
* Required string length.
|
|
1483
|
+
*
|
|
1484
|
+
* @param valueOrValues
|
|
1485
|
+
* Numeric value(s) of the string(s).
|
|
1486
|
+
*
|
|
1487
|
+
* @param exclusion
|
|
1488
|
+
* String(s) to be excluded from the range of outputs. See {@link Exclusion} for possible values and their meaning.
|
|
1489
|
+
*
|
|
1490
|
+
* @param tweak
|
|
1491
|
+
* If provided, the numerical value of the string(s) is/are "tweaked" using an {@link EncryptionTransformer |
|
|
1492
|
+
* encryption transformer}.
|
|
1493
|
+
*
|
|
1494
|
+
* @param creatorCallback
|
|
1495
|
+
* If provided, called after each string is constructed to create the final value.
|
|
1496
|
+
*
|
|
1497
|
+
* @returns
|
|
1498
|
+
* String(s) created from the value(s).
|
|
1499
|
+
*/
|
|
1500
|
+
create(length, valueOrValues, exclusion = 0 /* None */, tweak, creatorCallback) {
|
|
1501
|
+
this.validateLength(length);
|
|
1502
|
+
this.validateExclusion(exclusion);
|
|
1503
|
+
const allZerosValue = exclusion === 2 /* AllNumeric */ ? this._allZerosValues[length] : 0n;
|
|
1504
|
+
const transformer = Transformer.get(this._exclusionDomains[exclusion][length], tweak);
|
|
1505
|
+
return transformer.forward(valueOrValues, (transformedValue, index) => {
|
|
1506
|
+
let s = "";
|
|
1507
|
+
if (length !== 0) {
|
|
1508
|
+
let convertValue = transformedValue;
|
|
1509
|
+
if (exclusion === 2 /* AllNumeric */ && convertValue >= allZerosValue) {
|
|
1510
|
+
convertValue = convertValue + this.allNumericShift(true, length, convertValue - allZerosValue);
|
|
1511
|
+
}
|
|
1512
|
+
for (let position = length - 1; position > 0; position--) {
|
|
1513
|
+
const nextConvertValue = convertValue / this._characterSetSizeN;
|
|
1514
|
+
s = this.character(Number(convertValue - nextConvertValue * this._characterSetSizeN)) + s;
|
|
1515
|
+
convertValue = nextConvertValue;
|
|
1516
|
+
}
|
|
1517
|
+
s = this.character(exclusion === 1 /* FirstZero */ ? Number(convertValue % this._characterSetSizeMinusOneN) + 1 : Number(convertValue % this._characterSetSizeN)) + s;
|
|
1518
|
+
}
|
|
1519
|
+
return creatorCallback !== void 0 ? creatorCallback(s, index) : s;
|
|
1520
|
+
});
|
|
1521
|
+
}
|
|
1522
|
+
/**
|
|
1523
|
+
* Determine the value for a string.
|
|
1524
|
+
*
|
|
1525
|
+
* @param s
|
|
1526
|
+
* String.
|
|
1527
|
+
*
|
|
1528
|
+
* @param exclusion
|
|
1529
|
+
* Strings excluded from the range of inputs. See {@link Exclusion} for possible values and their meaning.
|
|
1530
|
+
*
|
|
1531
|
+
* @param tweak
|
|
1532
|
+
* If provided, the numerical value of the string was "tweaked" using an {@link EncryptionTransformer | encryption
|
|
1533
|
+
* transformer}.
|
|
1534
|
+
*
|
|
1535
|
+
* @returns
|
|
1536
|
+
* Numeric value of the string.
|
|
1537
|
+
*/
|
|
1538
|
+
valueFor(s, exclusion = 0 /* None */, tweak) {
|
|
1539
|
+
const length = s.length;
|
|
1540
|
+
this.validateLength(length);
|
|
1541
|
+
this.validateExclusion(exclusion);
|
|
1542
|
+
const characterSetSizeN = BigInt(this.characterSetSize);
|
|
1543
|
+
let value = this.characterIndexes(s).reduce((accumulator, characterIndex, index) => {
|
|
1544
|
+
if (characterIndex === void 0) {
|
|
1545
|
+
throw new RangeError(i18n_default.t("CharacterSetValidator.invalidCharacterAtPosition", {
|
|
1546
|
+
ns: utilityNS,
|
|
1547
|
+
c: s.charAt(index),
|
|
1548
|
+
position: index + 1
|
|
1549
|
+
}));
|
|
1550
|
+
}
|
|
1551
|
+
let value2;
|
|
1552
|
+
if (index === 0 && exclusion === 1 /* FirstZero */) {
|
|
1553
|
+
if (characterIndex === 0) {
|
|
1554
|
+
throw new RangeError(i18n_default.t("CharacterSetValidator.invalidCharacterAtPosition", {
|
|
1555
|
+
ns: utilityNS,
|
|
1556
|
+
c: "0",
|
|
1557
|
+
position: 1
|
|
1558
|
+
}));
|
|
1559
|
+
}
|
|
1560
|
+
value2 = BigInt(characterIndex - 1);
|
|
1561
|
+
} else {
|
|
1562
|
+
value2 = accumulator * characterSetSizeN + BigInt(characterIndex);
|
|
1563
|
+
}
|
|
1564
|
+
return value2;
|
|
1565
|
+
}, 0n);
|
|
1566
|
+
if (exclusion === 2 /* AllNumeric */) {
|
|
1567
|
+
const allZerosValue = this._allZerosValues[length];
|
|
1568
|
+
if (value >= allZerosValue) {
|
|
1569
|
+
value -= this.allNumericShift(false, length, value - allZerosValue);
|
|
1570
|
+
}
|
|
1571
|
+
}
|
|
1572
|
+
return Transformer.get(this._exclusionDomains[exclusion][length], tweak).reverse(value);
|
|
1573
|
+
}
|
|
1574
|
+
};
|
|
1575
|
+
var NUMERIC_CREATOR = new CharacterSetCreator([
|
|
1576
|
+
"0",
|
|
1577
|
+
"1",
|
|
1578
|
+
"2",
|
|
1579
|
+
"3",
|
|
1580
|
+
"4",
|
|
1581
|
+
"5",
|
|
1582
|
+
"6",
|
|
1583
|
+
"7",
|
|
1584
|
+
"8",
|
|
1585
|
+
"9"
|
|
1586
|
+
], 1 /* FirstZero */);
|
|
1587
|
+
var HEXADECIMAL_CREATOR = new CharacterSetCreator([
|
|
1588
|
+
"0",
|
|
1589
|
+
"1",
|
|
1590
|
+
"2",
|
|
1591
|
+
"3",
|
|
1592
|
+
"4",
|
|
1593
|
+
"5",
|
|
1594
|
+
"6",
|
|
1595
|
+
"7",
|
|
1596
|
+
"8",
|
|
1597
|
+
"9",
|
|
1598
|
+
"A",
|
|
1599
|
+
"B",
|
|
1600
|
+
"C",
|
|
1601
|
+
"D",
|
|
1602
|
+
"E",
|
|
1603
|
+
"F"
|
|
1604
|
+
], 1 /* FirstZero */, 2 /* AllNumeric */);
|
|
1605
|
+
var ALPHABETIC_CREATOR = new CharacterSetCreator([
|
|
1606
|
+
"A",
|
|
1607
|
+
"B",
|
|
1608
|
+
"C",
|
|
1609
|
+
"D",
|
|
1610
|
+
"E",
|
|
1611
|
+
"F",
|
|
1612
|
+
"G",
|
|
1613
|
+
"H",
|
|
1614
|
+
"I",
|
|
1615
|
+
"J",
|
|
1616
|
+
"K",
|
|
1617
|
+
"L",
|
|
1618
|
+
"M",
|
|
1619
|
+
"N",
|
|
1620
|
+
"O",
|
|
1621
|
+
"P",
|
|
1622
|
+
"Q",
|
|
1623
|
+
"R",
|
|
1624
|
+
"S",
|
|
1625
|
+
"T",
|
|
1626
|
+
"U",
|
|
1627
|
+
"V",
|
|
1628
|
+
"W",
|
|
1629
|
+
"X",
|
|
1630
|
+
"Y",
|
|
1631
|
+
"Z"
|
|
1632
|
+
]);
|
|
1633
|
+
var ALPHANUMERIC_CREATOR = new CharacterSetCreator([
|
|
1634
|
+
"0",
|
|
1635
|
+
"1",
|
|
1636
|
+
"2",
|
|
1637
|
+
"3",
|
|
1638
|
+
"4",
|
|
1639
|
+
"5",
|
|
1640
|
+
"6",
|
|
1641
|
+
"7",
|
|
1642
|
+
"8",
|
|
1643
|
+
"9",
|
|
1644
|
+
"A",
|
|
1645
|
+
"B",
|
|
1646
|
+
"C",
|
|
1647
|
+
"D",
|
|
1648
|
+
"E",
|
|
1649
|
+
"F",
|
|
1650
|
+
"G",
|
|
1651
|
+
"H",
|
|
1652
|
+
"I",
|
|
1653
|
+
"J",
|
|
1654
|
+
"K",
|
|
1655
|
+
"L",
|
|
1656
|
+
"M",
|
|
1657
|
+
"N",
|
|
1658
|
+
"O",
|
|
1659
|
+
"P",
|
|
1660
|
+
"Q",
|
|
1661
|
+
"R",
|
|
1662
|
+
"S",
|
|
1663
|
+
"T",
|
|
1664
|
+
"U",
|
|
1665
|
+
"V",
|
|
1666
|
+
"W",
|
|
1667
|
+
"X",
|
|
1668
|
+
"Y",
|
|
1669
|
+
"Z"
|
|
1670
|
+
], 1 /* FirstZero */, 2 /* AllNumeric */);
|
|
1671
|
+
export {
|
|
1672
|
+
ALPHABETIC_CREATOR,
|
|
1673
|
+
ALPHANUMERIC_CREATOR,
|
|
1674
|
+
CharacterSetCreator,
|
|
1675
|
+
CharacterSetValidator,
|
|
1676
|
+
EncryptionTransformer,
|
|
1677
|
+
Exclusion,
|
|
1678
|
+
HEXADECIMAL_CREATOR,
|
|
1679
|
+
IdentityTransformer,
|
|
1680
|
+
IteratorProxy,
|
|
1681
|
+
NUMERIC_CREATOR,
|
|
1682
|
+
RecordValidator,
|
|
1683
|
+
RegExpValidator,
|
|
1684
|
+
Sequencer,
|
|
1685
|
+
Transformer
|
|
1686
|
+
};
|