@optique/core 1.2.0-dev.2246 → 1.2.0-dev.2248
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/suggestion.cjs +36 -18
- package/dist/suggestion.d.cts +20 -5
- package/dist/suggestion.d.ts +20 -5
- package/dist/suggestion.js +36 -19
- package/dist/valueparser.cjs +254 -266
- package/dist/valueparser.js +256 -268
- package/package.json +2 -2
package/dist/valueparser.cjs
CHANGED
|
@@ -112,8 +112,8 @@ function choice(choices, options = {}) {
|
|
|
112
112
|
error: formatNumberChoiceError(input, numberChoices, numberChoices, numberInvalidChoice)
|
|
113
113
|
};
|
|
114
114
|
},
|
|
115
|
-
format(value
|
|
116
|
-
return Object.is(value
|
|
115
|
+
format(value) {
|
|
116
|
+
return Object.is(value, -0) ? "-0" : String(value);
|
|
117
117
|
},
|
|
118
118
|
suggest(prefix) {
|
|
119
119
|
return numberStrings.filter((valueStr, i) => !Number.isNaN(numberChoices[i]) && valueStr.startsWith(prefix)).map((valueStr) => ({
|
|
@@ -172,17 +172,17 @@ function choice(choices, options = {}) {
|
|
|
172
172
|
value: stringChoices[index]
|
|
173
173
|
};
|
|
174
174
|
},
|
|
175
|
-
format(value
|
|
176
|
-
return String(value
|
|
175
|
+
format(value) {
|
|
176
|
+
return String(value);
|
|
177
177
|
},
|
|
178
178
|
suggest(prefix) {
|
|
179
179
|
const normalizedPrefix = caseInsensitive ? prefix.toLowerCase() : prefix;
|
|
180
|
-
return stringChoices.filter((value
|
|
181
|
-
const normalizedValue = caseInsensitive ? value
|
|
180
|
+
return stringChoices.filter((value) => {
|
|
181
|
+
const normalizedValue = caseInsensitive ? value.toLowerCase() : value;
|
|
182
182
|
return normalizedValue.startsWith(normalizedPrefix);
|
|
183
|
-
}).map((value
|
|
183
|
+
}).map((value) => ({
|
|
184
184
|
kind: "literal",
|
|
185
|
-
text: value
|
|
185
|
+
text: value
|
|
186
186
|
}));
|
|
187
187
|
}
|
|
188
188
|
};
|
|
@@ -198,8 +198,8 @@ function choice(choices, options = {}) {
|
|
|
198
198
|
* @since 1.0.0
|
|
199
199
|
*/
|
|
200
200
|
function checkBooleanOption(options, key) {
|
|
201
|
-
const value
|
|
202
|
-
if (value
|
|
201
|
+
const value = options?.[key];
|
|
202
|
+
if (value !== void 0 && typeof value !== "boolean") throw new TypeError(`Expected ${String(key)} to be a boolean, but got ${typeof value}: ${String(value)}.`);
|
|
203
203
|
}
|
|
204
204
|
/**
|
|
205
205
|
* Validates that an option value, if present, is one of the allowed values.
|
|
@@ -215,10 +215,10 @@ function checkBooleanOption(options, key) {
|
|
|
215
215
|
* @since 1.0.0
|
|
216
216
|
*/
|
|
217
217
|
function checkEnumOption(options, key, allowed) {
|
|
218
|
-
const value
|
|
219
|
-
if (value
|
|
220
|
-
const rendered = typeof value
|
|
221
|
-
throw new TypeError(`Expected ${String(key)} to be one of ${allowed.map((v) => JSON.stringify(v)).join(", ")}, but got ${typeof value
|
|
218
|
+
const value = options?.[key];
|
|
219
|
+
if (value !== void 0 && (typeof value !== "string" || !allowed.includes(value))) {
|
|
220
|
+
const rendered = typeof value === "string" ? JSON.stringify(value) : typeof value === "symbol" ? value.toString() : String(value);
|
|
221
|
+
throw new TypeError(`Expected ${String(key)} to be one of ${allowed.map((v) => JSON.stringify(v)).join(", ")}, but got ${typeof value}: ${rendered}.`);
|
|
222
222
|
}
|
|
223
223
|
}
|
|
224
224
|
/**
|
|
@@ -278,20 +278,8 @@ function formatStringChoiceError(input, choices, invalidChoice, suggest) {
|
|
|
278
278
|
if (suggest === "nearest") return require_suggestion.appendValueHint(base, input, choices);
|
|
279
279
|
if (typeof suggest === "function") {
|
|
280
280
|
const hints = suggest(input, choices);
|
|
281
|
-
if (!Array.isArray(hints)
|
|
282
|
-
|
|
283
|
-
if (hints.length === 1) suggestionMsg = require_message.message`Did you mean ${require_message.value(hints[0])}?`;
|
|
284
|
-
else {
|
|
285
|
-
const parts = [require_message.text("Did you mean one of these?")];
|
|
286
|
-
for (const hint of hints) parts.push(require_message.text("\n "), require_message.value(hint));
|
|
287
|
-
suggestionMsg = parts;
|
|
288
|
-
}
|
|
289
|
-
return [
|
|
290
|
-
...base,
|
|
291
|
-
require_message.lineBreak(),
|
|
292
|
-
require_message.lineBreak(),
|
|
293
|
-
...suggestionMsg
|
|
294
|
-
];
|
|
281
|
+
if (!Array.isArray(hints)) return base;
|
|
282
|
+
return require_suggestion.appendValueSuggestions(base, hints);
|
|
295
283
|
}
|
|
296
284
|
return require_suggestion.appendValueHint(base, input, choices, suggest);
|
|
297
285
|
}
|
|
@@ -357,8 +345,8 @@ function string(options = {}) {
|
|
|
357
345
|
value: input
|
|
358
346
|
};
|
|
359
347
|
},
|
|
360
|
-
format(value
|
|
361
|
-
return value
|
|
348
|
+
format(value) {
|
|
349
|
+
return value;
|
|
362
350
|
}
|
|
363
351
|
};
|
|
364
352
|
}
|
|
@@ -410,12 +398,12 @@ function keyValue(options = {}) {
|
|
|
410
398
|
if (custom != null) return typeof custom === "function" ? custom(error) : custom;
|
|
411
399
|
return [require_message.text("Invalid value: "), ...require_message.cloneMessage(error)];
|
|
412
400
|
}
|
|
413
|
-
function validateParts(input, key, value
|
|
401
|
+
function validateParts(input, key, value) {
|
|
414
402
|
if (!allowEmptyKey && key === "") return {
|
|
415
403
|
success: false,
|
|
416
404
|
error: emptyKeyError(input)
|
|
417
405
|
};
|
|
418
|
-
if (!allowEmptyValue && value
|
|
406
|
+
if (!allowEmptyValue && value === "") return {
|
|
419
407
|
success: false,
|
|
420
408
|
error: emptyValueError(input)
|
|
421
409
|
};
|
|
@@ -424,7 +412,7 @@ function keyValue(options = {}) {
|
|
|
424
412
|
success: false,
|
|
425
413
|
error: invalidKeyError(keyResult.error)
|
|
426
414
|
};
|
|
427
|
-
const valueResult = valueParser.parse(value
|
|
415
|
+
const valueResult = valueParser.parse(value);
|
|
428
416
|
if (!valueResult.success) return {
|
|
429
417
|
success: false,
|
|
430
418
|
error: invalidValueError(valueResult.error)
|
|
@@ -446,14 +434,14 @@ function keyValue(options = {}) {
|
|
|
446
434
|
if (!allowEmptyKey && key === "") return false;
|
|
447
435
|
return partsRoundTrip(key, "");
|
|
448
436
|
}
|
|
449
|
-
function fallbackInput(key, value
|
|
450
|
-
return `${typeof key === "string" ? key : ""}${separator}${typeof value
|
|
437
|
+
function fallbackInput(key, value) {
|
|
438
|
+
return `${typeof key === "string" ? key : ""}${separator}${typeof value === "string" ? value : ""}`;
|
|
451
439
|
}
|
|
452
|
-
function partsRoundTrip(key, value
|
|
453
|
-
const input = `${key}${separator}${value
|
|
440
|
+
function partsRoundTrip(key, value) {
|
|
441
|
+
const input = `${key}${separator}${value}`;
|
|
454
442
|
const index = findSeparator(input);
|
|
455
443
|
if (index < 0) return false;
|
|
456
|
-
return input.slice(0, index) === key && input.slice(index + separator.length) === value
|
|
444
|
+
return input.slice(0, index) === key && input.slice(index + separator.length) === value;
|
|
457
445
|
}
|
|
458
446
|
function validateResultParts(input, keyResult, valueResult) {
|
|
459
447
|
if (!allowEmptyKey && keyResult.value === "") return {
|
|
@@ -503,26 +491,26 @@ function keyValue(options = {}) {
|
|
|
503
491
|
}
|
|
504
492
|
return makeKeyValueSuccess(keyResult, valueResult);
|
|
505
493
|
}
|
|
506
|
-
function validateTuple(value
|
|
507
|
-
if (!allowEmptyKey && value
|
|
494
|
+
function validateTuple(value) {
|
|
495
|
+
if (!allowEmptyKey && value[0] === "") return {
|
|
508
496
|
success: false,
|
|
509
|
-
error: emptyKeyError(fallbackInput(value
|
|
497
|
+
error: emptyKeyError(fallbackInput(value[0], value[1]))
|
|
510
498
|
};
|
|
511
|
-
if (!allowEmptyValue && value
|
|
499
|
+
if (!allowEmptyValue && value[1] === "") return {
|
|
512
500
|
success: false,
|
|
513
|
-
error: emptyValueError(fallbackInput(value
|
|
501
|
+
error: emptyValueError(fallbackInput(value[0], value[1]))
|
|
514
502
|
};
|
|
515
|
-
const keyResult = validateValueParserValue(keyParser, value
|
|
503
|
+
const keyResult = validateValueParserValue(keyParser, value[0]);
|
|
516
504
|
if (!keyResult.success) return {
|
|
517
505
|
success: false,
|
|
518
506
|
error: invalidKeyError(keyResult.error)
|
|
519
507
|
};
|
|
520
|
-
const valueResult = validateValueParserValue(valueParser, value
|
|
508
|
+
const valueResult = validateValueParserValue(valueParser, value[1]);
|
|
521
509
|
if (!valueResult.success) return {
|
|
522
510
|
success: false,
|
|
523
511
|
error: invalidValueError(valueResult.error)
|
|
524
512
|
};
|
|
525
|
-
return validateResultParts(fallbackInput(value
|
|
513
|
+
return validateResultParts(fallbackInput(value[0], value[1]), keyResult, valueResult);
|
|
526
514
|
}
|
|
527
515
|
return {
|
|
528
516
|
mode: "sync",
|
|
@@ -535,23 +523,23 @@ function keyValue(options = {}) {
|
|
|
535
523
|
error: missingSeparatorError(input)
|
|
536
524
|
};
|
|
537
525
|
const key = input.slice(0, index);
|
|
538
|
-
const value
|
|
539
|
-
return validateParts(input, key, value
|
|
526
|
+
const value = input.slice(index + separator.length);
|
|
527
|
+
return validateParts(input, key, value);
|
|
540
528
|
},
|
|
541
|
-
format(value
|
|
542
|
-
return `${keyParser.format(value
|
|
529
|
+
format(value) {
|
|
530
|
+
return `${keyParser.format(value[0])}${separator}${valueParser.format(value[1])}`;
|
|
543
531
|
},
|
|
544
|
-
validate(value
|
|
545
|
-
if (!isKeyValueTuple(value
|
|
532
|
+
validate(value) {
|
|
533
|
+
if (!isKeyValueTuple(value)) return {
|
|
546
534
|
success: false,
|
|
547
535
|
error: require_message.message`Expected a key-value tuple.`
|
|
548
536
|
};
|
|
549
|
-
return validateTuple(value
|
|
537
|
+
return validateTuple(value);
|
|
550
538
|
},
|
|
551
|
-
...hasNormalize ? { normalize(value
|
|
552
|
-
if (!isKeyValueTuple(value
|
|
553
|
-
const normalized = [typeof keyParser.normalize === "function" ? keyParser.normalize(value
|
|
554
|
-
return validateTuple(normalized).success ? normalized : value
|
|
539
|
+
...hasNormalize ? { normalize(value) {
|
|
540
|
+
if (!isKeyValueTuple(value)) return value;
|
|
541
|
+
const normalized = [typeof keyParser.normalize === "function" ? keyParser.normalize(value[0]) : value[0], typeof valueParser.normalize === "function" ? valueParser.normalize(value[1]) : value[1]];
|
|
542
|
+
return validateTuple(normalized).success ? normalized : value;
|
|
555
543
|
} } : {},
|
|
556
544
|
...hasSuggest ? { *suggest(prefix) {
|
|
557
545
|
const index = findSeparator(prefix);
|
|
@@ -575,9 +563,9 @@ function keyValue(options = {}) {
|
|
|
575
563
|
}
|
|
576
564
|
const valuePrefix = prefix.slice(index + separator.length);
|
|
577
565
|
for (const suggestion of valueParser.suggest(valuePrefix)) {
|
|
578
|
-
const value
|
|
579
|
-
if (!partsRoundTrip(key, value
|
|
580
|
-
if (suggestion.kind === "literal" && !validateParts(`${key}${separator}${value
|
|
566
|
+
const value = suggestionTextValue(suggestion);
|
|
567
|
+
if (!partsRoundTrip(key, value)) continue;
|
|
568
|
+
if (suggestion.kind === "literal" && !validateParts(`${key}${separator}${value}`, key, value).success) continue;
|
|
581
569
|
suggestions.push(prefixKeyValueSuggestion(suggestion, `${key}${separator}`));
|
|
582
570
|
}
|
|
583
571
|
}
|
|
@@ -590,15 +578,15 @@ function checkKeyValueChildParser(role, parser) {
|
|
|
590
578
|
if (parser.mode !== "sync") throw new TypeError(`keyValue() only supports sync ${role} parsers, but an async one was given.`);
|
|
591
579
|
if (require_internal_dependency.isDerivedValueParser(parser)) throw new TypeError(`keyValue() does not support dependency-derived ${role} parsers (created via deriveFrom() or dependency().derive()); pass the derived parser directly to option() or argument() instead.`);
|
|
592
580
|
}
|
|
593
|
-
function validateValueParserValue(parser, value
|
|
594
|
-
if (typeof parser.validate === "function") return parser.validate(value
|
|
581
|
+
function validateValueParserValue(parser, value) {
|
|
582
|
+
if (typeof parser.validate === "function") return parser.validate(value);
|
|
595
583
|
let formatted;
|
|
596
584
|
try {
|
|
597
|
-
formatted = parser.format(value
|
|
585
|
+
formatted = parser.format(value);
|
|
598
586
|
} catch {
|
|
599
587
|
return {
|
|
600
588
|
success: true,
|
|
601
|
-
value
|
|
589
|
+
value
|
|
602
590
|
};
|
|
603
591
|
}
|
|
604
592
|
if (typeof formatted !== "string") return {
|
|
@@ -607,10 +595,10 @@ function validateValueParserValue(parser, value$1) {
|
|
|
607
595
|
};
|
|
608
596
|
return parser.parse(formatted);
|
|
609
597
|
}
|
|
610
|
-
function formatValueParserValue(parser, value
|
|
598
|
+
function formatValueParserValue(parser, value) {
|
|
611
599
|
let formatted;
|
|
612
600
|
try {
|
|
613
|
-
formatted = parser.format(value
|
|
601
|
+
formatted = parser.format(value);
|
|
614
602
|
} catch {
|
|
615
603
|
return {
|
|
616
604
|
success: true,
|
|
@@ -648,8 +636,8 @@ function prefixKeyValueSuggestion(suggestion, prefix, suffix = "") {
|
|
|
648
636
|
description: suggestion.description
|
|
649
637
|
};
|
|
650
638
|
}
|
|
651
|
-
function isKeyValueTuple(value
|
|
652
|
-
return Array.isArray(value
|
|
639
|
+
function isKeyValueTuple(value) {
|
|
640
|
+
return Array.isArray(value) && value.length === 2 && 0 in value && 1 in value;
|
|
653
641
|
}
|
|
654
642
|
function makeKeyValueSuccess(keyResult, valueResult) {
|
|
655
643
|
const deferredKeys = /* @__PURE__ */ new Map();
|
|
@@ -735,22 +723,22 @@ function integer(options) {
|
|
|
735
723
|
success: false,
|
|
736
724
|
error: options.errors?.invalidInteger ? typeof options.errors.invalidInteger === "function" ? options.errors.invalidInteger(input) : options.errors.invalidInteger : require_message.message`Expected a valid integer, but got ${input}.`
|
|
737
725
|
};
|
|
738
|
-
const value
|
|
739
|
-
if (options.min != null && value
|
|
726
|
+
const value = BigInt(input);
|
|
727
|
+
if (options.min != null && value < options.min) return {
|
|
740
728
|
success: false,
|
|
741
|
-
error: options.errors?.belowMinimum ? typeof options.errors.belowMinimum === "function" ? options.errors.belowMinimum(value
|
|
729
|
+
error: options.errors?.belowMinimum ? typeof options.errors.belowMinimum === "function" ? options.errors.belowMinimum(value, options.min) : options.errors.belowMinimum : require_message.message`Expected a value greater than or equal to ${require_message.text(options.min.toLocaleString("en"))}, but got ${input}.`
|
|
742
730
|
};
|
|
743
|
-
else if (options.max != null && value
|
|
731
|
+
else if (options.max != null && value > options.max) return {
|
|
744
732
|
success: false,
|
|
745
|
-
error: options.errors?.aboveMaximum ? typeof options.errors.aboveMaximum === "function" ? options.errors.aboveMaximum(value
|
|
733
|
+
error: options.errors?.aboveMaximum ? typeof options.errors.aboveMaximum === "function" ? options.errors.aboveMaximum(value, options.max) : options.errors.aboveMaximum : require_message.message`Expected a value less than or equal to ${require_message.text(options.max.toLocaleString("en"))}, but got ${input}.`
|
|
746
734
|
};
|
|
747
735
|
return {
|
|
748
736
|
success: true,
|
|
749
|
-
value
|
|
737
|
+
value
|
|
750
738
|
};
|
|
751
739
|
},
|
|
752
|
-
format(value
|
|
753
|
-
return value
|
|
740
|
+
format(value) {
|
|
741
|
+
return value.toString();
|
|
754
742
|
}
|
|
755
743
|
};
|
|
756
744
|
}
|
|
@@ -786,22 +774,22 @@ function integer(options) {
|
|
|
786
774
|
return makeUnsafeIntegerError(input);
|
|
787
775
|
}
|
|
788
776
|
if (n > maxSafe || n < minSafe) return makeUnsafeIntegerError(input);
|
|
789
|
-
const value
|
|
790
|
-
if (options?.min != null && value
|
|
777
|
+
const value = Number(input);
|
|
778
|
+
if (options?.min != null && value < options.min) return {
|
|
791
779
|
success: false,
|
|
792
|
-
error: options.errors?.belowMinimum ? typeof options.errors.belowMinimum === "function" ? options.errors.belowMinimum(value
|
|
780
|
+
error: options.errors?.belowMinimum ? typeof options.errors.belowMinimum === "function" ? options.errors.belowMinimum(value, options.min) : options.errors.belowMinimum : require_message.message`Expected a value greater than or equal to ${require_message.text(options.min.toLocaleString("en"))}, but got ${input}.`
|
|
793
781
|
};
|
|
794
|
-
else if (options?.max != null && value
|
|
782
|
+
else if (options?.max != null && value > options.max) return {
|
|
795
783
|
success: false,
|
|
796
|
-
error: options.errors?.aboveMaximum ? typeof options.errors.aboveMaximum === "function" ? options.errors.aboveMaximum(value
|
|
784
|
+
error: options.errors?.aboveMaximum ? typeof options.errors.aboveMaximum === "function" ? options.errors.aboveMaximum(value, options.max) : options.errors.aboveMaximum : require_message.message`Expected a value less than or equal to ${require_message.text(options.max.toLocaleString("en"))}, but got ${input}.`
|
|
797
785
|
};
|
|
798
786
|
return {
|
|
799
787
|
success: true,
|
|
800
|
-
value
|
|
788
|
+
value
|
|
801
789
|
};
|
|
802
790
|
},
|
|
803
|
-
format(value
|
|
804
|
-
return value
|
|
791
|
+
format(value) {
|
|
792
|
+
return value.toString();
|
|
805
793
|
}
|
|
806
794
|
};
|
|
807
795
|
}
|
|
@@ -830,31 +818,31 @@ function float(options = {}) {
|
|
|
830
818
|
success: false,
|
|
831
819
|
error: options.errors?.invalidNumber ? typeof options.errors.invalidNumber === "function" ? options.errors.invalidNumber(i) : options.errors.invalidNumber : require_message.message`Expected a valid number, but got ${i}.`
|
|
832
820
|
});
|
|
833
|
-
let value
|
|
821
|
+
let value;
|
|
834
822
|
const lowerInput = input.toLowerCase();
|
|
835
|
-
if (lowerInput === "nan" && options.allowNaN) value
|
|
836
|
-
else if ((lowerInput === "infinity" || lowerInput === "+infinity") && options.allowInfinity) value
|
|
837
|
-
else if (lowerInput === "-infinity" && options.allowInfinity) value
|
|
823
|
+
if (lowerInput === "nan" && options.allowNaN) value = NaN;
|
|
824
|
+
else if ((lowerInput === "infinity" || lowerInput === "+infinity") && options.allowInfinity) value = Infinity;
|
|
825
|
+
else if (lowerInput === "-infinity" && options.allowInfinity) value = -Infinity;
|
|
838
826
|
else if (floatRegex.test(input)) {
|
|
839
|
-
value
|
|
840
|
-
if (!Number.isFinite(value
|
|
841
|
-
if (Number.isNaN(value
|
|
827
|
+
value = Number(input);
|
|
828
|
+
if (!Number.isFinite(value) && !options.allowInfinity) return invalidNumber(input);
|
|
829
|
+
if (Number.isNaN(value)) return invalidNumber(input);
|
|
842
830
|
} else return invalidNumber(input);
|
|
843
|
-
if (options.min != null && value
|
|
831
|
+
if (options.min != null && value < options.min) return {
|
|
844
832
|
success: false,
|
|
845
|
-
error: options.errors?.belowMinimum ? typeof options.errors.belowMinimum === "function" ? options.errors.belowMinimum(value
|
|
833
|
+
error: options.errors?.belowMinimum ? typeof options.errors.belowMinimum === "function" ? options.errors.belowMinimum(value, options.min) : options.errors.belowMinimum : require_message.message`Expected a value greater than or equal to ${require_message.text(options.min.toLocaleString("en"))}, but got ${input}.`
|
|
846
834
|
};
|
|
847
|
-
else if (options.max != null && value
|
|
835
|
+
else if (options.max != null && value > options.max) return {
|
|
848
836
|
success: false,
|
|
849
|
-
error: options.errors?.aboveMaximum ? typeof options.errors.aboveMaximum === "function" ? options.errors.aboveMaximum(value
|
|
837
|
+
error: options.errors?.aboveMaximum ? typeof options.errors.aboveMaximum === "function" ? options.errors.aboveMaximum(value, options.max) : options.errors.aboveMaximum : require_message.message`Expected a value less than or equal to ${require_message.text(options.max.toLocaleString("en"))}, but got ${input}.`
|
|
850
838
|
};
|
|
851
839
|
return {
|
|
852
840
|
success: true,
|
|
853
|
-
value
|
|
841
|
+
value
|
|
854
842
|
};
|
|
855
843
|
},
|
|
856
|
-
format(value
|
|
857
|
-
return value
|
|
844
|
+
format(value) {
|
|
845
|
+
return value.toString();
|
|
858
846
|
}
|
|
859
847
|
};
|
|
860
848
|
}
|
|
@@ -991,17 +979,17 @@ const BIGINT_FORMAT_UNITS_SI_AS_BINARY = [
|
|
|
991
979
|
* Formats a bigint byte count using the most readable unit. Falls back to
|
|
992
980
|
* `"${value}B"`. Tries exact integers, then 1 and 2 decimal places.
|
|
993
981
|
*/
|
|
994
|
-
function formatBigIntBytes(value
|
|
995
|
-
if (value
|
|
996
|
-
const absValue = value
|
|
982
|
+
function formatBigIntBytes(value, units) {
|
|
983
|
+
if (value === 0n) return "0B";
|
|
984
|
+
const absValue = value < 0n ? -value : value;
|
|
997
985
|
for (const [unit, size] of units) {
|
|
998
986
|
if (absValue < size) continue;
|
|
999
|
-
if (value
|
|
1000
|
-
const v = value
|
|
987
|
+
if (value % size === 0n) {
|
|
988
|
+
const v = value / size;
|
|
1001
989
|
const absV = v < 0n ? -v : v;
|
|
1002
990
|
if (absV >= 1n && absV < 1000n) return `${v}${unit}`;
|
|
1003
991
|
}
|
|
1004
|
-
const v100 = value
|
|
992
|
+
const v100 = value * 100n;
|
|
1005
993
|
if (v100 % size === 0n) {
|
|
1006
994
|
const q = v100 / size;
|
|
1007
995
|
const absQ = q < 0n ? -q : q;
|
|
@@ -1013,7 +1001,7 @@ function formatBigIntBytes(value$1, units) {
|
|
|
1013
1001
|
}
|
|
1014
1002
|
}
|
|
1015
1003
|
}
|
|
1016
|
-
return `${value
|
|
1004
|
+
return `${value}B`;
|
|
1017
1005
|
}
|
|
1018
1006
|
/**
|
|
1019
1007
|
* Core computation: parses `numStr` as a decimal rational, multiplies by
|
|
@@ -1143,10 +1131,10 @@ function fileSize(options = {}) {
|
|
|
1143
1131
|
value: bytes
|
|
1144
1132
|
};
|
|
1145
1133
|
},
|
|
1146
|
-
format(value
|
|
1134
|
+
format(value) {
|
|
1147
1135
|
const maxSafe = BigInt(Number.MAX_SAFE_INTEGER);
|
|
1148
|
-
if (value
|
|
1149
|
-
return formatBigIntBytes(value
|
|
1136
|
+
if (value >= -maxSafe && value <= maxSafe) return formatWithUnits(Number(value), numberFormatUnits);
|
|
1137
|
+
return formatBigIntBytes(value, bigintFormatUnits);
|
|
1150
1138
|
}
|
|
1151
1139
|
};
|
|
1152
1140
|
}
|
|
@@ -1184,8 +1172,8 @@ function fileSize(options = {}) {
|
|
|
1184
1172
|
value: bytes
|
|
1185
1173
|
};
|
|
1186
1174
|
},
|
|
1187
|
-
format(value
|
|
1188
|
-
return formatWithUnits(value
|
|
1175
|
+
format(value) {
|
|
1176
|
+
return formatWithUnits(value, formatUnits);
|
|
1189
1177
|
}
|
|
1190
1178
|
};
|
|
1191
1179
|
}
|
|
@@ -2277,8 +2265,8 @@ function color(options = {}) {
|
|
|
2277
2265
|
}
|
|
2278
2266
|
return invalidFormatError(input);
|
|
2279
2267
|
},
|
|
2280
|
-
format(value
|
|
2281
|
-
const { r, g, b, a } = value
|
|
2268
|
+
format(value) {
|
|
2269
|
+
const { r, g, b, a } = value;
|
|
2282
2270
|
if (!Number.isInteger(r) || r < 0 || r > 255 || !Number.isInteger(g) || g < 0 || g > 255 || !Number.isInteger(b) || b < 0 || b > 255 || !Number.isFinite(a) || a < 0 || a > 1) throw new RangeError(`Color components out of range: r=${r}, g=${g}, b=${b}, a=${a}.`);
|
|
2283
2271
|
const aStr = parseFloat(a.toFixed(4));
|
|
2284
2272
|
if (allowHex) {
|
|
@@ -2300,11 +2288,11 @@ function color(options = {}) {
|
|
|
2300
2288
|
for (const [name, c] of Object.entries(CSS_NAMED_COLORS)) if (c.r === r && c.g === g && c.b === b && c.a === a) return name;
|
|
2301
2289
|
throw new RangeError(`No CSS named color matches { r: ${r}, g: ${g}, b: ${b}, a: ${a} }.`);
|
|
2302
2290
|
},
|
|
2303
|
-
normalize(value
|
|
2304
|
-
if (!Number.isInteger(value
|
|
2305
|
-
const a = Math.round(value
|
|
2306
|
-
return a === value
|
|
2307
|
-
...value
|
|
2291
|
+
normalize(value) {
|
|
2292
|
+
if (!Number.isInteger(value.r) || value.r < 0 || value.r > 255 || !Number.isInteger(value.g) || value.g < 0 || value.g > 255 || !Number.isInteger(value.b) || value.b < 0 || value.b > 255 || !Number.isFinite(value.a) || value.a < 0 || value.a > 1) return value;
|
|
2293
|
+
const a = Math.round(value.a * 255) / 255;
|
|
2294
|
+
return a === value.a ? value : {
|
|
2295
|
+
...value,
|
|
2308
2296
|
a
|
|
2309
2297
|
};
|
|
2310
2298
|
},
|
|
@@ -2409,8 +2397,8 @@ function url(options = {}) {
|
|
|
2409
2397
|
value: url$1
|
|
2410
2398
|
};
|
|
2411
2399
|
},
|
|
2412
|
-
format(value
|
|
2413
|
-
return value
|
|
2400
|
+
format(value) {
|
|
2401
|
+
return value.href;
|
|
2414
2402
|
},
|
|
2415
2403
|
*suggest(prefix) {
|
|
2416
2404
|
if (allowedProtocols && prefix.length > 0 && !prefix.includes(":")) for (const protocol of allowedProtocols) {
|
|
@@ -2459,8 +2447,8 @@ function locale(options = {}) {
|
|
|
2459
2447
|
value: locale$1
|
|
2460
2448
|
};
|
|
2461
2449
|
},
|
|
2462
|
-
format(value
|
|
2463
|
-
return value
|
|
2450
|
+
format(value) {
|
|
2451
|
+
return value.toString();
|
|
2464
2452
|
},
|
|
2465
2453
|
*suggest(prefix) {
|
|
2466
2454
|
const commonLocales = [
|
|
@@ -2789,8 +2777,8 @@ function uuid(options = {}) {
|
|
|
2789
2777
|
value: input
|
|
2790
2778
|
};
|
|
2791
2779
|
},
|
|
2792
|
-
format(value
|
|
2793
|
-
return value
|
|
2780
|
+
format(value) {
|
|
2781
|
+
return value;
|
|
2794
2782
|
}
|
|
2795
2783
|
};
|
|
2796
2784
|
}
|
|
@@ -2852,26 +2840,26 @@ function port(options) {
|
|
|
2852
2840
|
success: false,
|
|
2853
2841
|
error: options.errors?.invalidPort ? typeof options.errors.invalidPort === "function" ? options.errors.invalidPort(input) : options.errors.invalidPort : require_message.message`Expected a valid port number, but got ${input}.`
|
|
2854
2842
|
};
|
|
2855
|
-
const value
|
|
2856
|
-
if (value
|
|
2843
|
+
const value = BigInt(input);
|
|
2844
|
+
if (value < min$1) return {
|
|
2857
2845
|
success: false,
|
|
2858
|
-
error: options.errors?.belowMinimum ? typeof options.errors.belowMinimum === "function" ? options.errors.belowMinimum(value
|
|
2846
|
+
error: options.errors?.belowMinimum ? typeof options.errors.belowMinimum === "function" ? options.errors.belowMinimum(value, min$1) : options.errors.belowMinimum : require_message.message`Expected a port number greater than or equal to ${require_message.text(min$1.toLocaleString("en"))}, but got ${input}.`
|
|
2859
2847
|
};
|
|
2860
|
-
if (value
|
|
2848
|
+
if (value > max$1) return {
|
|
2861
2849
|
success: false,
|
|
2862
|
-
error: options.errors?.aboveMaximum ? typeof options.errors.aboveMaximum === "function" ? options.errors.aboveMaximum(value
|
|
2850
|
+
error: options.errors?.aboveMaximum ? typeof options.errors.aboveMaximum === "function" ? options.errors.aboveMaximum(value, max$1) : options.errors.aboveMaximum : require_message.message`Expected a port number less than or equal to ${require_message.text(max$1.toLocaleString("en"))}, but got ${input}.`
|
|
2863
2851
|
};
|
|
2864
|
-
if (options.disallowWellKnown && value
|
|
2852
|
+
if (options.disallowWellKnown && value >= 1n && value <= 1023n) return {
|
|
2865
2853
|
success: false,
|
|
2866
|
-
error: options.errors?.wellKnownNotAllowed ? typeof options.errors.wellKnownNotAllowed === "function" ? options.errors.wellKnownNotAllowed(value
|
|
2854
|
+
error: options.errors?.wellKnownNotAllowed ? typeof options.errors.wellKnownNotAllowed === "function" ? options.errors.wellKnownNotAllowed(value) : options.errors.wellKnownNotAllowed : require_message.message`Port ${value.toLocaleString("en")} is a well-known port (1-1023) and may require elevated privileges.`
|
|
2867
2855
|
};
|
|
2868
2856
|
return {
|
|
2869
2857
|
success: true,
|
|
2870
|
-
value
|
|
2858
|
+
value
|
|
2871
2859
|
};
|
|
2872
2860
|
},
|
|
2873
|
-
format(value
|
|
2874
|
-
return value
|
|
2861
|
+
format(value) {
|
|
2862
|
+
return value.toString();
|
|
2875
2863
|
}
|
|
2876
2864
|
};
|
|
2877
2865
|
}
|
|
@@ -2892,26 +2880,26 @@ function port(options) {
|
|
|
2892
2880
|
success: false,
|
|
2893
2881
|
error: options?.errors?.invalidPort ? typeof options.errors.invalidPort === "function" ? options.errors.invalidPort(input) : options.errors.invalidPort : require_message.message`Expected a valid port number, but got ${input}.`
|
|
2894
2882
|
};
|
|
2895
|
-
const value
|
|
2896
|
-
if (value
|
|
2883
|
+
const value = Number.parseInt(input);
|
|
2884
|
+
if (value < min) return {
|
|
2897
2885
|
success: false,
|
|
2898
|
-
error: options?.errors?.belowMinimum ? typeof options.errors.belowMinimum === "function" ? options.errors.belowMinimum(value
|
|
2886
|
+
error: options?.errors?.belowMinimum ? typeof options.errors.belowMinimum === "function" ? options.errors.belowMinimum(value, min) : options.errors.belowMinimum : require_message.message`Expected a port number greater than or equal to ${require_message.text(min.toLocaleString("en"))}, but got ${input}.`
|
|
2899
2887
|
};
|
|
2900
|
-
if (value
|
|
2888
|
+
if (value > max) return {
|
|
2901
2889
|
success: false,
|
|
2902
|
-
error: options?.errors?.aboveMaximum ? typeof options.errors.aboveMaximum === "function" ? options.errors.aboveMaximum(value
|
|
2890
|
+
error: options?.errors?.aboveMaximum ? typeof options.errors.aboveMaximum === "function" ? options.errors.aboveMaximum(value, max) : options.errors.aboveMaximum : require_message.message`Expected a port number less than or equal to ${require_message.text(max.toLocaleString("en"))}, but got ${input}.`
|
|
2903
2891
|
};
|
|
2904
|
-
if (options?.disallowWellKnown && value
|
|
2892
|
+
if (options?.disallowWellKnown && value >= 1 && value <= 1023) return {
|
|
2905
2893
|
success: false,
|
|
2906
|
-
error: options.errors?.wellKnownNotAllowed ? typeof options.errors.wellKnownNotAllowed === "function" ? options.errors.wellKnownNotAllowed(value
|
|
2894
|
+
error: options.errors?.wellKnownNotAllowed ? typeof options.errors.wellKnownNotAllowed === "function" ? options.errors.wellKnownNotAllowed(value) : options.errors.wellKnownNotAllowed : require_message.message`Port ${value.toLocaleString("en")} is a well-known port (1-1023) and may require elevated privileges.`
|
|
2907
2895
|
};
|
|
2908
2896
|
return {
|
|
2909
2897
|
success: true,
|
|
2910
|
-
value
|
|
2898
|
+
value
|
|
2911
2899
|
};
|
|
2912
2900
|
},
|
|
2913
|
-
format(value
|
|
2914
|
-
return value
|
|
2901
|
+
format(value) {
|
|
2902
|
+
return value.toString();
|
|
2915
2903
|
}
|
|
2916
2904
|
};
|
|
2917
2905
|
}
|
|
@@ -3045,8 +3033,8 @@ function ipv4(options) {
|
|
|
3045
3033
|
value: ipAddress
|
|
3046
3034
|
};
|
|
3047
3035
|
},
|
|
3048
|
-
format(value
|
|
3049
|
-
return value
|
|
3036
|
+
format(value) {
|
|
3037
|
+
return value;
|
|
3050
3038
|
}
|
|
3051
3039
|
};
|
|
3052
3040
|
}
|
|
@@ -3208,8 +3196,8 @@ function hostname(options) {
|
|
|
3208
3196
|
value: input
|
|
3209
3197
|
};
|
|
3210
3198
|
},
|
|
3211
|
-
format(value
|
|
3212
|
-
return value
|
|
3199
|
+
format(value) {
|
|
3200
|
+
return value;
|
|
3213
3201
|
}
|
|
3214
3202
|
};
|
|
3215
3203
|
}
|
|
@@ -3430,9 +3418,9 @@ function email(options) {
|
|
|
3430
3418
|
};
|
|
3431
3419
|
}
|
|
3432
3420
|
},
|
|
3433
|
-
format(value
|
|
3434
|
-
if (Array.isArray(value
|
|
3435
|
-
return value
|
|
3421
|
+
format(value) {
|
|
3422
|
+
if (Array.isArray(value)) return value.join(", ");
|
|
3423
|
+
return value;
|
|
3436
3424
|
}
|
|
3437
3425
|
};
|
|
3438
3426
|
}
|
|
@@ -3665,13 +3653,13 @@ function socketAddress(options) {
|
|
|
3665
3653
|
};
|
|
3666
3654
|
return makeInvalidFormatError(input);
|
|
3667
3655
|
}
|
|
3668
|
-
function isSocketAddressValue(value
|
|
3669
|
-
return typeof value
|
|
3656
|
+
function isSocketAddressValue(value) {
|
|
3657
|
+
return typeof value === "object" && value !== null && "host" in value && typeof value.host === "string" && "port" in value && typeof value.port === "number";
|
|
3670
3658
|
}
|
|
3671
|
-
function formatSocketAddressValue(value
|
|
3672
|
-
const ipv6Host = parseAndNormalizeIpv6(value
|
|
3673
|
-
if (separator === ":" && ipv6Host !== null) return `[${ipv6Host}]${separator}${value
|
|
3674
|
-
return `${value
|
|
3659
|
+
function formatSocketAddressValue(value) {
|
|
3660
|
+
const ipv6Host = parseAndNormalizeIpv6(value.host);
|
|
3661
|
+
if (separator === ":" && ipv6Host !== null) return `[${ipv6Host}]${separator}${value.port}`;
|
|
3662
|
+
return `${value.host}${separator}${value.port}`;
|
|
3675
3663
|
}
|
|
3676
3664
|
const parser = {
|
|
3677
3665
|
mode: "sync",
|
|
@@ -3904,16 +3892,16 @@ function socketAddress(options) {
|
|
|
3904
3892
|
error: require_message.message`Expected a socket address in format ${require_message.text(formatExample)}, but got ${input}.`
|
|
3905
3893
|
};
|
|
3906
3894
|
},
|
|
3907
|
-
format(value
|
|
3908
|
-
return formatSocketAddressValue(value
|
|
3895
|
+
format(value) {
|
|
3896
|
+
return formatSocketAddressValue(value);
|
|
3909
3897
|
},
|
|
3910
|
-
normalize(value
|
|
3911
|
-
if (!isSocketAddressValue(value
|
|
3898
|
+
normalize(value) {
|
|
3899
|
+
if (!isSocketAddressValue(value)) return value;
|
|
3912
3900
|
try {
|
|
3913
|
-
const result = parser.parse(formatSocketAddressValue(value
|
|
3914
|
-
return result.success ? result.value : value
|
|
3901
|
+
const result = parser.parse(formatSocketAddressValue(value));
|
|
3902
|
+
return result.success ? result.value : value;
|
|
3915
3903
|
} catch {
|
|
3916
|
-
return value
|
|
3904
|
+
return value;
|
|
3917
3905
|
}
|
|
3918
3906
|
}
|
|
3919
3907
|
};
|
|
@@ -4044,8 +4032,8 @@ function portRange(options) {
|
|
|
4044
4032
|
};
|
|
4045
4033
|
}
|
|
4046
4034
|
},
|
|
4047
|
-
format(value
|
|
4048
|
-
return `${value
|
|
4035
|
+
format(value) {
|
|
4036
|
+
return `${value.start}${separator}${value.end}`;
|
|
4049
4037
|
}
|
|
4050
4038
|
};
|
|
4051
4039
|
}
|
|
@@ -4123,11 +4111,11 @@ function macAddress(options) {
|
|
|
4123
4111
|
return formatted.join(sep);
|
|
4124
4112
|
}
|
|
4125
4113
|
let macParsing = false;
|
|
4126
|
-
function normalizeMacValue(value
|
|
4127
|
-
if (macParsing) return value
|
|
4114
|
+
function normalizeMacValue(value, fallback) {
|
|
4115
|
+
if (macParsing) return value;
|
|
4128
4116
|
macParsing = true;
|
|
4129
4117
|
try {
|
|
4130
|
-
const result = parserObj.parse(value
|
|
4118
|
+
const result = parserObj.parse(value);
|
|
4131
4119
|
return result.success ? result.value : fallback;
|
|
4132
4120
|
} catch {
|
|
4133
4121
|
return fallback;
|
|
@@ -4218,13 +4206,13 @@ function macAddress(options) {
|
|
|
4218
4206
|
value: joinOctets(octets, finalSeparator)
|
|
4219
4207
|
};
|
|
4220
4208
|
},
|
|
4221
|
-
format(value
|
|
4222
|
-
if (typeof value
|
|
4223
|
-
return normalizeMacValue(value
|
|
4209
|
+
format(value) {
|
|
4210
|
+
if (typeof value !== "string") return metavar$1;
|
|
4211
|
+
return normalizeMacValue(value, value);
|
|
4224
4212
|
},
|
|
4225
|
-
normalize(value
|
|
4226
|
-
if (typeof value
|
|
4227
|
-
return normalizeMacValue(value
|
|
4213
|
+
normalize(value) {
|
|
4214
|
+
if (typeof value !== "string") return value;
|
|
4215
|
+
return normalizeMacValue(value, value);
|
|
4228
4216
|
}
|
|
4229
4217
|
};
|
|
4230
4218
|
return parserObj;
|
|
@@ -4506,10 +4494,10 @@ function domain(options) {
|
|
|
4506
4494
|
value: result
|
|
4507
4495
|
};
|
|
4508
4496
|
},
|
|
4509
|
-
format(value
|
|
4510
|
-
if (typeof value
|
|
4511
|
-
if (!lowercase) return value
|
|
4512
|
-
return value
|
|
4497
|
+
format(value) {
|
|
4498
|
+
if (typeof value !== "string") return metavar$1;
|
|
4499
|
+
if (!lowercase) return value;
|
|
4500
|
+
return value.split(".").length >= minLabels ? value.toLowerCase() : value;
|
|
4513
4501
|
}
|
|
4514
4502
|
};
|
|
4515
4503
|
if (lowercase) {
|
|
@@ -4736,9 +4724,9 @@ function ipv6(options) {
|
|
|
4736
4724
|
value: normalized
|
|
4737
4725
|
};
|
|
4738
4726
|
},
|
|
4739
|
-
format(value
|
|
4740
|
-
if (typeof value
|
|
4741
|
-
return parseAndNormalizeIpv6(value
|
|
4727
|
+
format(value) {
|
|
4728
|
+
if (typeof value !== "string") return metavar$1;
|
|
4729
|
+
return parseAndNormalizeIpv6(value) ?? value;
|
|
4742
4730
|
}
|
|
4743
4731
|
};
|
|
4744
4732
|
let ipv6Parsing = false;
|
|
@@ -5122,9 +5110,9 @@ function ip(options) {
|
|
|
5122
5110
|
error: msg
|
|
5123
5111
|
};
|
|
5124
5112
|
},
|
|
5125
|
-
format(value
|
|
5126
|
-
if (typeof value
|
|
5127
|
-
return parseAndNormalizeIpv6(value
|
|
5113
|
+
format(value) {
|
|
5114
|
+
if (typeof value !== "string") return metavar$1;
|
|
5115
|
+
return parseAndNormalizeIpv6(value) ?? value;
|
|
5128
5116
|
}
|
|
5129
5117
|
};
|
|
5130
5118
|
let ipParsing = false;
|
|
@@ -5616,16 +5604,16 @@ function cidr(options) {
|
|
|
5616
5604
|
};
|
|
5617
5605
|
let cidrParsing = false;
|
|
5618
5606
|
Object.defineProperty(cidrParserObj, "format", {
|
|
5619
|
-
value(value
|
|
5620
|
-
if (typeof value
|
|
5621
|
-
if (cidrParsing) return `${value
|
|
5607
|
+
value(value) {
|
|
5608
|
+
if (typeof value !== "object" || value == null || !("address" in value) || !("prefix" in value) || !("version" in value)) return metavar$1;
|
|
5609
|
+
if (cidrParsing) return `${value.address}/${value.prefix}`;
|
|
5622
5610
|
cidrParsing = true;
|
|
5623
5611
|
try {
|
|
5624
|
-
const raw = `${value
|
|
5612
|
+
const raw = `${value.address}/${value.prefix}`;
|
|
5625
5613
|
const result = cidrParserObj.parse(raw);
|
|
5626
|
-
return result.success && result.value.version === value
|
|
5614
|
+
return result.success && result.value.version === value.version ? `${result.value.address}/${result.value.prefix}` : raw;
|
|
5627
5615
|
} catch {
|
|
5628
|
-
return `${value
|
|
5616
|
+
return `${value.address}/${value.prefix}`;
|
|
5629
5617
|
} finally {
|
|
5630
5618
|
cidrParsing = false;
|
|
5631
5619
|
}
|
|
@@ -5707,10 +5695,10 @@ function semVer(options = {}) {
|
|
|
5707
5695
|
value: result
|
|
5708
5696
|
};
|
|
5709
5697
|
},
|
|
5710
|
-
format(value
|
|
5711
|
-
let s = `${value
|
|
5712
|
-
if (value
|
|
5713
|
-
if (value
|
|
5698
|
+
format(value) {
|
|
5699
|
+
let s = `${value.major}.${value.minor}.${value.patch}`;
|
|
5700
|
+
if (value.preRelease != null) s += `-${value.preRelease}`;
|
|
5701
|
+
if (value.metadata != null) s += `+${value.metadata}`;
|
|
5714
5702
|
return s;
|
|
5715
5703
|
},
|
|
5716
5704
|
*suggest(prefix) {
|
|
@@ -5734,8 +5722,8 @@ function semVer(options = {}) {
|
|
|
5734
5722
|
value: canonical
|
|
5735
5723
|
};
|
|
5736
5724
|
},
|
|
5737
|
-
format(value
|
|
5738
|
-
return value
|
|
5725
|
+
format(value) {
|
|
5726
|
+
return value;
|
|
5739
5727
|
},
|
|
5740
5728
|
*suggest(prefix) {
|
|
5741
5729
|
for (const s of suggestions) if (s.startsWith(prefix)) yield {
|
|
@@ -5803,9 +5791,9 @@ function json(options) {
|
|
|
5803
5791
|
metavar: metavar$1,
|
|
5804
5792
|
placeholder,
|
|
5805
5793
|
parse(input) {
|
|
5806
|
-
let value
|
|
5794
|
+
let value;
|
|
5807
5795
|
try {
|
|
5808
|
-
value
|
|
5796
|
+
value = JSON.parse(input);
|
|
5809
5797
|
} catch (e) {
|
|
5810
5798
|
const err = e instanceof Error ? e : new Error(String(e));
|
|
5811
5799
|
const error = invalidJsonError instanceof Function ? invalidJsonError(input) : invalidJsonError ?? [require_message.text(`Not a valid JSON: ${err.message}`)];
|
|
@@ -5814,7 +5802,7 @@ function json(options) {
|
|
|
5814
5802
|
error
|
|
5815
5803
|
};
|
|
5816
5804
|
}
|
|
5817
|
-
const nonFinite = findNonFiniteNumber(value
|
|
5805
|
+
const nonFinite = findNonFiniteNumber(value);
|
|
5818
5806
|
if (nonFinite !== void 0) {
|
|
5819
5807
|
const error = invalidJsonError instanceof Function ? invalidJsonError(input) : invalidJsonError ?? [require_message.text(`Not a valid JSON: number out of range.`)];
|
|
5820
5808
|
return {
|
|
@@ -5823,9 +5811,9 @@ function json(options) {
|
|
|
5823
5811
|
};
|
|
5824
5812
|
}
|
|
5825
5813
|
if (rootType != null) {
|
|
5826
|
-
const actual = jsonTypeOf(value
|
|
5814
|
+
const actual = jsonTypeOf(value);
|
|
5827
5815
|
if (actual !== rootType) {
|
|
5828
|
-
const error = invalidRootTypeError instanceof Function ? invalidRootTypeError(value
|
|
5816
|
+
const error = invalidRootTypeError instanceof Function ? invalidRootTypeError(value, rootType) : invalidRootTypeError ?? [require_message.text(`Expected JSON ${rootType}, but got ${actual}.`)];
|
|
5829
5817
|
return {
|
|
5830
5818
|
success: false,
|
|
5831
5819
|
error
|
|
@@ -5834,20 +5822,20 @@ function json(options) {
|
|
|
5834
5822
|
}
|
|
5835
5823
|
return {
|
|
5836
5824
|
success: true,
|
|
5837
|
-
value
|
|
5825
|
+
value
|
|
5838
5826
|
};
|
|
5839
5827
|
},
|
|
5840
|
-
format(value
|
|
5841
|
-
const nonFinite = findNonFiniteNumber(value
|
|
5828
|
+
format(value) {
|
|
5829
|
+
const nonFinite = findNonFiniteNumber(value);
|
|
5842
5830
|
if (nonFinite !== void 0) throw new TypeError(`Expected a finite JSON number, but got ${String(nonFinite)}.`);
|
|
5843
|
-
return JSON.stringify(value
|
|
5831
|
+
return JSON.stringify(value);
|
|
5844
5832
|
}
|
|
5845
5833
|
};
|
|
5846
5834
|
}
|
|
5847
|
-
function jsonTypeOf(value
|
|
5848
|
-
if (value
|
|
5849
|
-
if (Array.isArray(value
|
|
5850
|
-
return typeof value
|
|
5835
|
+
function jsonTypeOf(value) {
|
|
5836
|
+
if (value === null) return "null";
|
|
5837
|
+
if (Array.isArray(value)) return "array";
|
|
5838
|
+
return typeof value;
|
|
5851
5839
|
}
|
|
5852
5840
|
/**
|
|
5853
5841
|
* Finds the first non-finite number (`NaN`, `Infinity`, or `-Infinity`)
|
|
@@ -5864,17 +5852,17 @@ function findNonFiniteNumber(root) {
|
|
|
5864
5852
|
const stack = [root];
|
|
5865
5853
|
const seen = /* @__PURE__ */ new Set();
|
|
5866
5854
|
while (stack.length > 0) {
|
|
5867
|
-
const value
|
|
5868
|
-
if (typeof value
|
|
5869
|
-
if (!Number.isFinite(value
|
|
5870
|
-
} else if (Array.isArray(value
|
|
5871
|
-
if (seen.has(value
|
|
5872
|
-
seen.add(value
|
|
5873
|
-
for (const item of value
|
|
5874
|
-
} else if (value
|
|
5875
|
-
if (seen.has(value
|
|
5876
|
-
seen.add(value
|
|
5877
|
-
for (const item of Object.values(value
|
|
5855
|
+
const value = stack.pop();
|
|
5856
|
+
if (typeof value === "number") {
|
|
5857
|
+
if (!Number.isFinite(value)) return value;
|
|
5858
|
+
} else if (Array.isArray(value)) {
|
|
5859
|
+
if (seen.has(value)) continue;
|
|
5860
|
+
seen.add(value);
|
|
5861
|
+
for (const item of value) stack.push(item);
|
|
5862
|
+
} else if (value !== null && typeof value === "object") {
|
|
5863
|
+
if (seen.has(value)) continue;
|
|
5864
|
+
seen.add(value);
|
|
5865
|
+
for (const item of Object.values(value)) stack.push(item);
|
|
5878
5866
|
}
|
|
5879
5867
|
}
|
|
5880
5868
|
return void 0;
|
|
@@ -6007,21 +5995,21 @@ function cron(options = {}) {
|
|
|
6007
5995
|
...years ? { year: "1970" } : {}
|
|
6008
5996
|
},
|
|
6009
5997
|
parse: parseCron,
|
|
6010
|
-
format(value
|
|
5998
|
+
format(value) {
|
|
6011
5999
|
return [
|
|
6012
|
-
...seconds ? [value
|
|
6013
|
-
value
|
|
6014
|
-
value
|
|
6015
|
-
value
|
|
6016
|
-
value
|
|
6017
|
-
value
|
|
6018
|
-
...years ? [value
|
|
6000
|
+
...seconds ? [value.second ?? "0"] : [],
|
|
6001
|
+
value.minute,
|
|
6002
|
+
value.hour,
|
|
6003
|
+
value.dayOfMonth,
|
|
6004
|
+
value.month,
|
|
6005
|
+
value.dayOfWeek,
|
|
6006
|
+
...years ? [value.year ?? "1970"] : []
|
|
6019
6007
|
].join(" ");
|
|
6020
6008
|
},
|
|
6021
|
-
validate(value
|
|
6022
|
-
if (seconds && value
|
|
6023
|
-
if (!seconds && value
|
|
6024
|
-
const result = parseCron(this.format(value
|
|
6009
|
+
validate(value) {
|
|
6010
|
+
if (seconds && value.second == null || years && value.year == null) return makeError(this.format(value));
|
|
6011
|
+
if (!seconds && value.second != null || !years && value.year != null) return makeError(this.format(value));
|
|
6012
|
+
const result = parseCron(this.format(value));
|
|
6025
6013
|
return result.success ? {
|
|
6026
6014
|
success: true,
|
|
6027
6015
|
value: result.value
|
|
@@ -6053,10 +6041,10 @@ function isQuartzCronField(field, spec, quartz) {
|
|
|
6053
6041
|
}
|
|
6054
6042
|
return false;
|
|
6055
6043
|
}
|
|
6056
|
-
function parseQuartzDayOfWeekSuffixValue(value
|
|
6057
|
-
if (/^[1-7]$/u.test(value
|
|
6058
|
-
if (!/^[A-Z]{3}$/u.test(value
|
|
6059
|
-
return CRON_DAY_NAMES.has(value
|
|
6044
|
+
function parseQuartzDayOfWeekSuffixValue(value) {
|
|
6045
|
+
if (/^[1-7]$/u.test(value)) return true;
|
|
6046
|
+
if (!/^[A-Z]{3}$/u.test(value)) return false;
|
|
6047
|
+
return CRON_DAY_NAMES.has(value);
|
|
6060
6048
|
}
|
|
6061
6049
|
function isValidCronFieldPart(part, spec) {
|
|
6062
6050
|
const stepParts = part.split("/");
|
|
@@ -6077,18 +6065,18 @@ function isValidCronFieldPart(part, spec) {
|
|
|
6077
6065
|
function isPositiveCronStep(step) {
|
|
6078
6066
|
return /^\d+$/u.test(step) && Number.parseInt(step, 10) > 0;
|
|
6079
6067
|
}
|
|
6080
|
-
function isCronValueInRange(value
|
|
6081
|
-
return parseCronValue(value
|
|
6068
|
+
function isCronValueInRange(value, spec) {
|
|
6069
|
+
return parseCronValue(value, spec) !== void 0;
|
|
6082
6070
|
}
|
|
6083
6071
|
function parseCronRangeEnd(start, end, spec) {
|
|
6084
6072
|
if (spec.kind === "dayOfWeek" && start.toUpperCase() !== "SUN" && (end.toUpperCase() === "SUN" || end === "0")) return 7;
|
|
6085
6073
|
return parseCronValue(end, spec);
|
|
6086
6074
|
}
|
|
6087
|
-
function parseCronValue(value
|
|
6088
|
-
const named = spec.names?.get(value
|
|
6075
|
+
function parseCronValue(value, spec) {
|
|
6076
|
+
const named = spec.names?.get(value.toUpperCase());
|
|
6089
6077
|
if (named !== void 0) return named;
|
|
6090
|
-
if (!/^\d+$/u.test(value
|
|
6091
|
-
const number = Number.parseInt(value
|
|
6078
|
+
if (!/^\d+$/u.test(value)) return void 0;
|
|
6079
|
+
const number = Number.parseInt(value, 10);
|
|
6092
6080
|
return number >= spec.min && number <= spec.max ? number : void 0;
|
|
6093
6081
|
}
|
|
6094
6082
|
/**
|
|
@@ -6119,14 +6107,14 @@ function firstOf(...rawArgs) {
|
|
|
6119
6107
|
}
|
|
6120
6108
|
const metavar$1 = options.metavar ?? parsers.map((parser) => parser.metavar).join("|");
|
|
6121
6109
|
require_nonempty.ensureNonEmptyString(metavar$1);
|
|
6122
|
-
const mergedChoices = parsers.every((parser) => parser.choices != null) ? Object.freeze(parsers.flatMap((parser) => [...parser.choices]).filter((value
|
|
6123
|
-
function findOwner(value
|
|
6110
|
+
const mergedChoices = parsers.every((parser) => parser.choices != null) ? Object.freeze(parsers.flatMap((parser) => [...parser.choices]).filter((value, index, all) => all.findIndex((other) => Object.is(other, value)) === index)) : void 0;
|
|
6111
|
+
function findOwner(value) {
|
|
6124
6112
|
let canonicalizing;
|
|
6125
6113
|
for (const parser of parsers) {
|
|
6126
6114
|
if (typeof parser.validate === "function") {
|
|
6127
6115
|
let result$1;
|
|
6128
6116
|
try {
|
|
6129
|
-
result$1 = parser.validate(value
|
|
6117
|
+
result$1 = parser.validate(value);
|
|
6130
6118
|
} catch {
|
|
6131
6119
|
continue;
|
|
6132
6120
|
}
|
|
@@ -6138,18 +6126,18 @@ function firstOf(...rawArgs) {
|
|
|
6138
6126
|
}
|
|
6139
6127
|
let formatted;
|
|
6140
6128
|
try {
|
|
6141
|
-
formatted = parser.format(value
|
|
6129
|
+
formatted = parser.format(value);
|
|
6142
6130
|
} catch {
|
|
6143
6131
|
continue;
|
|
6144
6132
|
}
|
|
6145
6133
|
if (typeof formatted !== "string") continue;
|
|
6146
6134
|
const result = parser.parse(formatted);
|
|
6147
6135
|
if (!result.success) continue;
|
|
6148
|
-
if (ownsRoundTrippedValue(parser, result.value, value
|
|
6136
|
+
if (ownsRoundTrippedValue(parser, result.value, value)) return {
|
|
6149
6137
|
parser,
|
|
6150
6138
|
result
|
|
6151
6139
|
};
|
|
6152
|
-
if (canonicalizing == null && value
|
|
6140
|
+
if (canonicalizing == null && value !== null && typeof value !== "object" && typeof result.value === typeof value) canonicalizing = {
|
|
6153
6141
|
parser,
|
|
6154
6142
|
result
|
|
6155
6143
|
};
|
|
@@ -6176,7 +6164,7 @@ function firstOf(...rawArgs) {
|
|
|
6176
6164
|
error: noMatch == null ? firstOfNoMatchError(errors) : typeof noMatch === "function" ? noMatch(input, errors) : noMatch
|
|
6177
6165
|
};
|
|
6178
6166
|
},
|
|
6179
|
-
format(value
|
|
6167
|
+
format(value) {
|
|
6180
6168
|
let canonicalizing;
|
|
6181
6169
|
let fallback;
|
|
6182
6170
|
let firstError;
|
|
@@ -6184,7 +6172,7 @@ function firstOf(...rawArgs) {
|
|
|
6184
6172
|
for (const parser of parsers) {
|
|
6185
6173
|
let formatted;
|
|
6186
6174
|
try {
|
|
6187
|
-
formatted = parser.format(value
|
|
6175
|
+
formatted = parser.format(value);
|
|
6188
6176
|
} catch (e) {
|
|
6189
6177
|
if (!hasError) {
|
|
6190
6178
|
firstError = e;
|
|
@@ -6196,14 +6184,14 @@ function firstOf(...rawArgs) {
|
|
|
6196
6184
|
if (typeof parser.validate === "function") {
|
|
6197
6185
|
let owned = false;
|
|
6198
6186
|
try {
|
|
6199
|
-
owned = parser.validate(value
|
|
6187
|
+
owned = parser.validate(value).success;
|
|
6200
6188
|
} catch {}
|
|
6201
6189
|
if (owned) return formatted;
|
|
6202
6190
|
} else {
|
|
6203
6191
|
const result = parser.parse(formatted);
|
|
6204
6192
|
if (result.success) {
|
|
6205
|
-
if (ownsRoundTrippedValue(parser, result.value, value
|
|
6206
|
-
if (canonicalizing == null && value
|
|
6193
|
+
if (ownsRoundTrippedValue(parser, result.value, value)) return formatted;
|
|
6194
|
+
if (canonicalizing == null && value !== null && typeof value !== "object" && typeof result.value === typeof value) canonicalizing = formatted;
|
|
6207
6195
|
}
|
|
6208
6196
|
}
|
|
6209
6197
|
fallback ??= formatted;
|
|
@@ -6213,18 +6201,18 @@ function firstOf(...rawArgs) {
|
|
|
6213
6201
|
if (hasError) throw firstError;
|
|
6214
6202
|
throw new TypeError("No constituent parser could format the given value.");
|
|
6215
6203
|
},
|
|
6216
|
-
validate(value
|
|
6217
|
-
const owner = findOwner(value
|
|
6204
|
+
validate(value) {
|
|
6205
|
+
const owner = findOwner(value);
|
|
6218
6206
|
if (owner == null) return {
|
|
6219
6207
|
success: false,
|
|
6220
6208
|
error: require_message.message`Expected a value matching ${require_message.metavar(metavar$1)}.`
|
|
6221
6209
|
};
|
|
6222
6210
|
return owner.result;
|
|
6223
6211
|
},
|
|
6224
|
-
...hasNormalize ? { normalize(value
|
|
6225
|
-
const owner = findOwner(value
|
|
6226
|
-
if (owner == null || typeof owner.parser.normalize !== "function") return value
|
|
6227
|
-
return owner.parser.normalize(value
|
|
6212
|
+
...hasNormalize ? { normalize(value) {
|
|
6213
|
+
const owner = findOwner(value);
|
|
6214
|
+
if (owner == null || typeof owner.parser.normalize !== "function") return value;
|
|
6215
|
+
return owner.parser.normalize(value);
|
|
6228
6216
|
} } : {},
|
|
6229
6217
|
...hasSuggest ? { *suggest(prefix) {
|
|
6230
6218
|
const collected = [];
|
|
@@ -6267,19 +6255,19 @@ function firstOfNoMatchError(errors) {
|
|
|
6267
6255
|
* canonicalization from data loss (e.g. a lossy `color()` `format()`
|
|
6268
6256
|
* dropping fields that belong to a later, more faithful constituent).
|
|
6269
6257
|
*/
|
|
6270
|
-
function ownsRoundTrippedValue(parser, roundTripped, value
|
|
6271
|
-
if (valuesEqual(roundTripped, value
|
|
6258
|
+
function ownsRoundTrippedValue(parser, roundTripped, value) {
|
|
6259
|
+
if (valuesEqual(roundTripped, value)) return true;
|
|
6272
6260
|
if (typeof parser.normalize === "function") {
|
|
6273
6261
|
let normalized;
|
|
6274
6262
|
try {
|
|
6275
|
-
normalized = parser.normalize(value
|
|
6263
|
+
normalized = parser.normalize(value);
|
|
6276
6264
|
} catch {
|
|
6277
6265
|
return false;
|
|
6278
6266
|
}
|
|
6279
6267
|
if (valuesEqual(roundTripped, normalized)) return true;
|
|
6280
6268
|
}
|
|
6281
|
-
if (typeof value
|
|
6282
|
-
if (typeof value
|
|
6269
|
+
if (typeof value === "string" && typeof roundTripped === "string") return value.toLowerCase() === roundTripped.toLowerCase();
|
|
6270
|
+
if (typeof value === "number" && typeof roundTripped === "number") return value === roundTripped;
|
|
6283
6271
|
return false;
|
|
6284
6272
|
}
|
|
6285
6273
|
/**
|
|
@@ -6323,9 +6311,9 @@ function valuesEqual(a, b) {
|
|
|
6323
6311
|
* Checks whether an object provides a `toString()` implementation other
|
|
6324
6312
|
* than the generic `Object.prototype.toString`.
|
|
6325
6313
|
*/
|
|
6326
|
-
function hasCustomToString(value
|
|
6314
|
+
function hasCustomToString(value) {
|
|
6327
6315
|
try {
|
|
6328
|
-
const toString = value
|
|
6316
|
+
const toString = value.toString;
|
|
6329
6317
|
return typeof toString === "function" && toString !== Object.prototype.toString;
|
|
6330
6318
|
} catch {
|
|
6331
6319
|
return false;
|