@optique/core 1.0.0-dev.741 → 1.0.0-dev.751
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/valueparser.cjs +11 -0
- package/dist/valueparser.d.cts +4 -0
- package/dist/valueparser.d.ts +4 -0
- package/dist/valueparser.js +11 -0
- package/package.json +1 -1
package/dist/valueparser.cjs
CHANGED
|
@@ -14,6 +14,7 @@ function isValueParser(object) {
|
|
|
14
14
|
* Implementation of the choice parser for both string and number types.
|
|
15
15
|
*/
|
|
16
16
|
function choice(choices, options = {}) {
|
|
17
|
+
if (choices.length < 1) throw new TypeError("Expected at least one choice, but got an empty array.");
|
|
17
18
|
const metavar = options.metavar ?? "TYPE";
|
|
18
19
|
require_nonempty.ensureNonEmptyString(metavar);
|
|
19
20
|
const isNumberChoice = choices.length > 0 && typeof choices[0] === "number";
|
|
@@ -91,6 +92,16 @@ function choice(choices, options = {}) {
|
|
|
91
92
|
const stringChoices = choices;
|
|
92
93
|
const stringOptions = options;
|
|
93
94
|
const normalizedValues = stringOptions.caseInsensitive ? stringChoices.map((v) => v.toLowerCase()) : stringChoices;
|
|
95
|
+
if (stringOptions.caseInsensitive) {
|
|
96
|
+
const seen = /* @__PURE__ */ new Map();
|
|
97
|
+
for (let i = 0; i < stringChoices.length; i++) {
|
|
98
|
+
const nv = normalizedValues[i];
|
|
99
|
+
const original = stringChoices[i];
|
|
100
|
+
const prev = seen.get(nv);
|
|
101
|
+
if (prev !== void 0 && prev !== original) throw new TypeError(`Ambiguous choices for case-insensitive matching: ${JSON.stringify(prev)} and ${JSON.stringify(original)} both normalize to ${JSON.stringify(nv)}.`);
|
|
102
|
+
seen.set(nv, original);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
94
105
|
return {
|
|
95
106
|
$mode: "sync",
|
|
96
107
|
metavar,
|
package/dist/valueparser.d.cts
CHANGED
|
@@ -206,6 +206,9 @@ declare function isValueParser<M extends Mode, T>(object: unknown): object is Va
|
|
|
206
206
|
* @param options Configuration options for the choice parser.
|
|
207
207
|
* @returns A {@link ValueParser} that checks if the input matches one of the
|
|
208
208
|
* specified values.
|
|
209
|
+
* @throws {TypeError} If the choices array is empty.
|
|
210
|
+
* @throws {TypeError} If `caseInsensitive` is `true` and multiple choices
|
|
211
|
+
* normalize to the same lowercase value.
|
|
209
212
|
*/
|
|
210
213
|
declare function choice<const T extends string>(choices: readonly T[], options?: ChoiceOptionsString): ValueParser<"sync", T>;
|
|
211
214
|
/**
|
|
@@ -219,6 +222,7 @@ declare function choice<const T extends string>(choices: readonly T[], options?:
|
|
|
219
222
|
* @param options Configuration options for the choice parser.
|
|
220
223
|
* @returns A {@link ValueParser} that checks if the input matches one of the
|
|
221
224
|
* specified values.
|
|
225
|
+
* @throws {TypeError} If the choices array is empty.
|
|
222
226
|
* @since 0.9.0
|
|
223
227
|
*/
|
|
224
228
|
declare function choice<const T extends number>(choices: readonly T[], options?: ChoiceOptionsNumber): ValueParser<"sync", T>;
|
package/dist/valueparser.d.ts
CHANGED
|
@@ -206,6 +206,9 @@ declare function isValueParser<M extends Mode, T>(object: unknown): object is Va
|
|
|
206
206
|
* @param options Configuration options for the choice parser.
|
|
207
207
|
* @returns A {@link ValueParser} that checks if the input matches one of the
|
|
208
208
|
* specified values.
|
|
209
|
+
* @throws {TypeError} If the choices array is empty.
|
|
210
|
+
* @throws {TypeError} If `caseInsensitive` is `true` and multiple choices
|
|
211
|
+
* normalize to the same lowercase value.
|
|
209
212
|
*/
|
|
210
213
|
declare function choice<const T extends string>(choices: readonly T[], options?: ChoiceOptionsString): ValueParser<"sync", T>;
|
|
211
214
|
/**
|
|
@@ -219,6 +222,7 @@ declare function choice<const T extends string>(choices: readonly T[], options?:
|
|
|
219
222
|
* @param options Configuration options for the choice parser.
|
|
220
223
|
* @returns A {@link ValueParser} that checks if the input matches one of the
|
|
221
224
|
* specified values.
|
|
225
|
+
* @throws {TypeError} If the choices array is empty.
|
|
222
226
|
* @since 0.9.0
|
|
223
227
|
*/
|
|
224
228
|
declare function choice<const T extends number>(choices: readonly T[], options?: ChoiceOptionsNumber): ValueParser<"sync", T>;
|
package/dist/valueparser.js
CHANGED
|
@@ -14,6 +14,7 @@ function isValueParser(object) {
|
|
|
14
14
|
* Implementation of the choice parser for both string and number types.
|
|
15
15
|
*/
|
|
16
16
|
function choice(choices, options = {}) {
|
|
17
|
+
if (choices.length < 1) throw new TypeError("Expected at least one choice, but got an empty array.");
|
|
17
18
|
const metavar = options.metavar ?? "TYPE";
|
|
18
19
|
ensureNonEmptyString(metavar);
|
|
19
20
|
const isNumberChoice = choices.length > 0 && typeof choices[0] === "number";
|
|
@@ -91,6 +92,16 @@ function choice(choices, options = {}) {
|
|
|
91
92
|
const stringChoices = choices;
|
|
92
93
|
const stringOptions = options;
|
|
93
94
|
const normalizedValues = stringOptions.caseInsensitive ? stringChoices.map((v) => v.toLowerCase()) : stringChoices;
|
|
95
|
+
if (stringOptions.caseInsensitive) {
|
|
96
|
+
const seen = /* @__PURE__ */ new Map();
|
|
97
|
+
for (let i = 0; i < stringChoices.length; i++) {
|
|
98
|
+
const nv = normalizedValues[i];
|
|
99
|
+
const original = stringChoices[i];
|
|
100
|
+
const prev = seen.get(nv);
|
|
101
|
+
if (prev !== void 0 && prev !== original) throw new TypeError(`Ambiguous choices for case-insensitive matching: ${JSON.stringify(prev)} and ${JSON.stringify(original)} both normalize to ${JSON.stringify(nv)}.`);
|
|
102
|
+
seen.set(nv, original);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
94
105
|
return {
|
|
95
106
|
$mode: "sync",
|
|
96
107
|
metavar,
|