@optique/core 1.0.0-dev.745 → 1.0.0-dev.757

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.
@@ -14,11 +14,31 @@ 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";
20
21
  if (isNumberChoice) {
21
- const numberChoices = choices;
22
+ const numberChoices = (() => {
23
+ const seen = /* @__PURE__ */ new Set();
24
+ let hasPositiveZero = false;
25
+ let hasNegativeZero = false;
26
+ const result = [];
27
+ for (const v of choices) {
28
+ if (Object.is(v, -0)) {
29
+ if (hasNegativeZero) continue;
30
+ hasNegativeZero = true;
31
+ } else if (Object.is(v, 0)) {
32
+ if (hasPositiveZero) continue;
33
+ hasPositiveZero = true;
34
+ } else {
35
+ if (seen.has(v)) continue;
36
+ seen.add(v);
37
+ }
38
+ result.push(v);
39
+ }
40
+ return result;
41
+ })();
22
42
  const numberOptions = options;
23
43
  const hasNaN = numberChoices.some((v) => Number.isNaN(v));
24
44
  const validNumberChoices = hasNaN ? numberChoices.filter((v) => !Number.isNaN(v)) : numberChoices;
@@ -88,7 +108,7 @@ function choice(choices, options = {}) {
88
108
  }
89
109
  };
90
110
  }
91
- const stringChoices = choices;
111
+ const stringChoices = [...new Set(choices)];
92
112
  const stringOptions = options;
93
113
  const normalizedValues = stringOptions.caseInsensitive ? stringChoices.map((v) => v.toLowerCase()) : stringChoices;
94
114
  if (stringOptions.caseInsensitive) {
@@ -104,7 +124,7 @@ function choice(choices, options = {}) {
104
124
  return {
105
125
  $mode: "sync",
106
126
  metavar,
107
- choices,
127
+ choices: stringChoices,
108
128
  parse(input) {
109
129
  const normalizedInput = stringOptions.caseInsensitive ? input.toLowerCase() : input;
110
130
  const index = normalizedValues.indexOf(normalizedInput);
@@ -206,6 +206,7 @@ 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.
209
210
  * @throws {TypeError} If `caseInsensitive` is `true` and multiple choices
210
211
  * normalize to the same lowercase value.
211
212
  */
@@ -221,6 +222,7 @@ declare function choice<const T extends string>(choices: readonly T[], options?:
221
222
  * @param options Configuration options for the choice parser.
222
223
  * @returns A {@link ValueParser} that checks if the input matches one of the
223
224
  * specified values.
225
+ * @throws {TypeError} If the choices array is empty.
224
226
  * @since 0.9.0
225
227
  */
226
228
  declare function choice<const T extends number>(choices: readonly T[], options?: ChoiceOptionsNumber): ValueParser<"sync", T>;
@@ -206,6 +206,7 @@ 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.
209
210
  * @throws {TypeError} If `caseInsensitive` is `true` and multiple choices
210
211
  * normalize to the same lowercase value.
211
212
  */
@@ -221,6 +222,7 @@ declare function choice<const T extends string>(choices: readonly T[], options?:
221
222
  * @param options Configuration options for the choice parser.
222
223
  * @returns A {@link ValueParser} that checks if the input matches one of the
223
224
  * specified values.
225
+ * @throws {TypeError} If the choices array is empty.
224
226
  * @since 0.9.0
225
227
  */
226
228
  declare function choice<const T extends number>(choices: readonly T[], options?: ChoiceOptionsNumber): ValueParser<"sync", T>;
@@ -14,11 +14,31 @@ 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";
20
21
  if (isNumberChoice) {
21
- const numberChoices = choices;
22
+ const numberChoices = (() => {
23
+ const seen = /* @__PURE__ */ new Set();
24
+ let hasPositiveZero = false;
25
+ let hasNegativeZero = false;
26
+ const result = [];
27
+ for (const v of choices) {
28
+ if (Object.is(v, -0)) {
29
+ if (hasNegativeZero) continue;
30
+ hasNegativeZero = true;
31
+ } else if (Object.is(v, 0)) {
32
+ if (hasPositiveZero) continue;
33
+ hasPositiveZero = true;
34
+ } else {
35
+ if (seen.has(v)) continue;
36
+ seen.add(v);
37
+ }
38
+ result.push(v);
39
+ }
40
+ return result;
41
+ })();
22
42
  const numberOptions = options;
23
43
  const hasNaN = numberChoices.some((v) => Number.isNaN(v));
24
44
  const validNumberChoices = hasNaN ? numberChoices.filter((v) => !Number.isNaN(v)) : numberChoices;
@@ -88,7 +108,7 @@ function choice(choices, options = {}) {
88
108
  }
89
109
  };
90
110
  }
91
- const stringChoices = choices;
111
+ const stringChoices = [...new Set(choices)];
92
112
  const stringOptions = options;
93
113
  const normalizedValues = stringOptions.caseInsensitive ? stringChoices.map((v) => v.toLowerCase()) : stringChoices;
94
114
  if (stringOptions.caseInsensitive) {
@@ -104,7 +124,7 @@ function choice(choices, options = {}) {
104
124
  return {
105
125
  $mode: "sync",
106
126
  metavar,
107
- choices,
127
+ choices: stringChoices,
108
128
  parse(input) {
109
129
  const normalizedInput = stringOptions.caseInsensitive ? input.toLowerCase() : input;
110
130
  const index = normalizedValues.indexOf(normalizedInput);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.0.0-dev.745+a12a4fbd",
3
+ "version": "1.0.0-dev.757+6abd354f",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",