@gunshi/combinators 0.37.2 → 1.0.0-beta.0

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/lib/index.js CHANGED
@@ -1,4 +1,15 @@
1
- //#region ../../node_modules/.pnpm/args-tokens@0.28.2/node_modules/args-tokens/lib/utils.js
1
+ /**
2
+ * forked from `nodejs/node` (`pkgjs/parseargs`)
3
+ * repository url: https://github.com/nodejs/node (https://github.com/pkgjs/parseargs)
4
+ * code url: https://github.com/nodejs/node/blob/main/lib/internal/util/parse_args/parse_args.js
5
+ *
6
+ * @author kazuya kawaguchi (a.k.a. kazupon)
7
+ * @license MIT
8
+ */
9
+ "-".codePointAt(0);
10
+ "=".codePointAt(0);
11
+ //#endregion
12
+ //#region ../../node_modules/.pnpm/args-tokens@1.0.0-beta.1/node_modules/args-tokens/lib/utils.js
2
13
  /**
3
14
  * @author kazuya kawaguchi (a.k.a. kazupon)
4
15
  * @license MIT
@@ -12,18 +23,28 @@
12
23
  function formatChoices(choices) {
13
24
  return choices.map((value) => JSON.stringify(value)).join(", ");
14
25
  }
26
+ //#endregion
27
+ //#region ../../node_modules/.pnpm/args-tokens@1.0.0-beta.1/node_modules/args-tokens/lib/resolver-CAlgxe_R.js
15
28
  /**
16
- * forked from `nodejs/node` (`pkgjs/parseargs`)
17
- * repository url: https://github.com/nodejs/node (https://github.com/pkgjs/parseargs)
18
- * code url: https://github.com/nodejs/node/blob/main/lib/internal/util/parse_args/parse_args.js
29
+ * Internal definitions shared by the modules of `args-tokens`.
19
30
  *
31
+ * This module is not an entry point: it is not in the `exports` of `package.json` and `jsr.json`,
32
+ * so what it exports is not part of the public API.
33
+ */
34
+ /**
20
35
  * @author kazuya kawaguchi (a.k.a. kazupon)
21
36
  * @license MIT
22
37
  */
23
- "-".codePointAt(0);
24
- "=".codePointAt(0);
25
- //#endregion
26
- //#region ../../node_modules/.pnpm/args-tokens@0.28.2/node_modules/args-tokens/lib/resolver.js
38
+ /**
39
+ * Brand of a `parse` function that has no side effects, set by `string()`, `number()`,
40
+ * `integer()`, `float()`, `choice()` and `positional()` without a parser, so that the resolver may
41
+ * call it with a value that the option was not given, to check the value before suggesting it.
42
+ *
43
+ * The brand is looked up in the global symbol registry with `Symbol.for`, so it stays identical
44
+ * across bundled copies of `args-tokens`, where the resolver and the combinators may come from
45
+ * different copies.
46
+ */
47
+ const PURE_PARSE = Symbol.for("args-tokens.pureParse");
27
48
  /**
28
49
  * Entry point of argument options resolver.
29
50
  *
@@ -38,6 +59,10 @@ function formatChoices(choices) {
38
59
  *
39
60
  * Each code identifies a validation failure category and is also suitable as an
40
61
  * i18n resource key for localized rendering.
62
+ *
63
+ * {@link resolveArgs} and `parse()` do not report `unknownOption`. An option that is not in the
64
+ * schema takes the argument after it as its value, unless that argument is an option, and the value
65
+ * is dropped: with no `foo` in the schema, `--foo bar` gives no positional argument.
41
66
  */
42
67
  const ArgsValidationErrorKeys = {
43
68
  requiredOption: "err:arg:required-option",
@@ -45,14 +70,31 @@ const ArgsValidationErrorKeys = {
45
70
  invalidType: "err:arg:invalid-type",
46
71
  invalidChoice: "err:arg:invalid-choice",
47
72
  customParse: "err:arg:custom-parse",
48
- unknownOption: "err:arg:unknown-option"
73
+ unknownOption: "err:arg:unknown-option",
74
+ unexpectedValue: "err:arg:unexpected-value",
75
+ missingValue: "err:arg:missing-value",
76
+ conflict: "err:arg:conflict",
77
+ invalidDefault: "err:arg:invalid-default"
49
78
  };
50
79
  /**
80
+ * Brand that marks {@link ArgsValidationError} instances.
81
+ *
82
+ * The brand is looked up in the global symbol registry with `Symbol.for`, so it stays
83
+ * identical across bundled copies of this module and across realms. It lets
84
+ * {@link isArgsValidationError} recognize errors created by another copy of `args-tokens`,
85
+ * where `instanceof` cannot match.
86
+ */
87
+ const ARGS_VALIDATION_ERROR_BRAND = Symbol.for("args-tokens.ArgsValidationError");
88
+ /**
51
89
  * An error that contains structured metadata for argument validation failures.
52
90
  *
53
91
  * The `message` remains the English fallback message. Renderers can use `code`
54
92
  * and `values` to localize the error, falling back to `message` when localization
55
93
  * is unavailable.
94
+ *
95
+ * Each instance carries a non-enumerable brand keyed by
96
+ * `Symbol.for('args-tokens.ArgsValidationError')`, which {@link isArgsValidationError}
97
+ * uses to recognize instances created by another bundled copy of `args-tokens`.
56
98
  */
57
99
  var ArgsValidationError = class extends Error {
58
100
  /**
@@ -76,10 +118,16 @@ var ArgsValidationError = class extends Error {
76
118
  this.name = "ArgsValidationError";
77
119
  this.code = options.code;
78
120
  this.values = options.values ?? {};
121
+ Object.defineProperty(this, ARGS_VALIDATION_ERROR_BRAND, {
122
+ value: true,
123
+ enumerable: false,
124
+ writable: false,
125
+ configurable: false
126
+ });
79
127
  }
80
128
  };
81
129
  //#endregion
82
- //#region ../../node_modules/.pnpm/args-tokens@0.28.2/node_modules/args-tokens/lib/combinators.js
130
+ //#region ../../node_modules/.pnpm/args-tokens@1.0.0-beta.1/node_modules/args-tokens/lib/combinators.js
83
131
  /**
84
132
  * Parser combinator factory functions for composable argument schema construction.
85
133
  *
@@ -92,14 +140,10 @@ var ArgsValidationError = class extends Error {
92
140
  * integer,
93
141
  * boolean,
94
142
  * positional,
95
- * choice,
96
143
  * withDefault,
97
- * multiple,
98
144
  * required,
99
145
  * short,
100
- * map,
101
- * merge,
102
- * extend
146
+ * merge
103
147
  * } from 'args-tokens/combinators'
104
148
  *
105
149
  * // Define reusable schema groups with args()
@@ -127,8 +171,6 @@ var ArgsValidationError = class extends Error {
127
171
  * const { values } = resolveArgs(schema, tokens)
128
172
  * ```
129
173
  *
130
- * @experimental This module is experimental and may change in future versions.
131
- *
132
174
  * @module
133
175
  */
134
176
  /**
@@ -156,23 +198,21 @@ function createInvalidChoiceError(message, choices, actual) {
156
198
  });
157
199
  }
158
200
  /**
159
- * Create a string argument schema with optional validation.
160
- *
161
- * @param opts - Validation options.
162
- * @returns A combinator schema that resolves to string.
201
+ * Mark the `parse` function of a schema as free of side effects, so that the resolver may call it
202
+ * to check a value before suggesting it.
163
203
  *
164
- * @example
165
- * ```ts
166
- * const args = {
167
- * name: string({ minLength: 1, maxLength: 50 })
168
- * }
169
- * ```
204
+ * @typeParam S - The schema type.
170
205
  *
171
- * @experimental
206
+ * @param schema - A schema whose `parse` function has no side effects.
207
+ * @returns The same schema.
172
208
  */
209
+ function pureParse(schema) {
210
+ Object.defineProperty(schema.parse, PURE_PARSE, { value: true });
211
+ return schema;
212
+ }
173
213
  // @__NO_SIDE_EFFECTS__
174
214
  function string(opts) {
175
- return {
215
+ const schema = {
176
216
  type: "string",
177
217
  metavar: "string",
178
218
  ...opts?.description != null ? { description: opts.description } : {},
@@ -186,27 +226,12 @@ function string(opts) {
186
226
  return value;
187
227
  }
188
228
  };
229
+ Object.defineProperty(schema.parse, PURE_PARSE, { get: () => !opts?.pattern?.global && !opts?.pattern?.sticky });
230
+ return schema;
189
231
  }
190
- /**
191
- * Create a number argument schema with optional range validation.
192
- *
193
- * Accepts any numeric value (integer or float).
194
- *
195
- * @param opts - Range options.
196
- * @returns A combinator schema that resolves to number.
197
- *
198
- * @example
199
- * ```ts
200
- * const args = {
201
- * timeout: number({ min: 0, max: 30000 })
202
- * }
203
- * ```
204
- *
205
- * @experimental
206
- */
207
232
  // @__NO_SIDE_EFFECTS__
208
233
  function number(opts) {
209
- return {
234
+ return pureParse({
210
235
  type: "number",
211
236
  metavar: "number",
212
237
  ...opts?.description != null ? { description: opts.description } : {},
@@ -220,28 +245,11 @@ function number(opts) {
220
245
  if (opts?.max != null && n > opts.max) throw new RangeError(`Number must be <= ${opts.max}, got ${n}`);
221
246
  return n;
222
247
  }
223
- };
248
+ });
224
249
  }
225
- /**
226
- * Create an integer argument schema with optional range validation.
227
- *
228
- * Only accepts integer values (no decimals).
229
- *
230
- * @param opts - Range options.
231
- * @returns A combinator schema that resolves to number (integer).
232
- *
233
- * @example
234
- * ```ts
235
- * const args = {
236
- * retries: integer({ min: 0, max: 10 })
237
- * }
238
- * ```
239
- *
240
- * @experimental
241
- */
242
250
  // @__NO_SIDE_EFFECTS__
243
251
  function integer(opts) {
244
- return {
252
+ return pureParse({
245
253
  type: "custom",
246
254
  metavar: "integer",
247
255
  ...opts?.description != null ? { description: opts.description } : {},
@@ -256,28 +264,11 @@ function integer(opts) {
256
264
  if (opts?.max != null && n > opts.max) throw new RangeError(`Integer must be <= ${opts.max}, got ${n}`);
257
265
  return n;
258
266
  }
259
- };
267
+ });
260
268
  }
261
- /**
262
- * Create a floating-point argument schema with optional range validation.
263
- *
264
- * Rejects `NaN` and `Infinity` values.
265
- *
266
- * @param opts - Range options.
267
- * @returns A combinator schema that resolves to number (float).
268
- *
269
- * @example
270
- * ```ts
271
- * const args = {
272
- * ratio: float({ min: 0, max: 1 })
273
- * }
274
- * ```
275
- *
276
- * @experimental
277
- */
278
269
  // @__NO_SIDE_EFFECTS__
279
270
  function float(opts) {
280
- return {
271
+ return pureParse({
281
272
  type: "custom",
282
273
  metavar: "float",
283
274
  ...opts?.description != null ? { description: opts.description } : {},
@@ -293,27 +284,8 @@ function float(opts) {
293
284
  if (opts?.max != null && n > opts.max) throw new RangeError(`Float must be <= ${opts.max}, got ${n}`);
294
285
  return n;
295
286
  }
296
- };
287
+ });
297
288
  }
298
- /**
299
- * Create a boolean argument schema.
300
- *
301
- * Boolean arguments are existence-based. The resolver passes `"true"` or `"false"`
302
- * to the parse function based on the presence or negation of the flag.
303
- *
304
- * @param opts - Boolean options.
305
- * @returns A combinator schema for boolean flags.
306
- *
307
- * @example
308
- * ```ts
309
- * const args = {
310
- * color: boolean({ negatable: true })
311
- * }
312
- * // Usage: --color (true), --no-color (false)
313
- * ```
314
- *
315
- * @experimental
316
- */
317
289
  // @__NO_SIDE_EFFECTS__
318
290
  function boolean(opts) {
319
291
  return {
@@ -334,45 +306,28 @@ function positional(parser) {
334
306
  if (parser && "parse" in parser) return {
335
307
  type: "positional",
336
308
  parse: parser.parse,
337
- metavar: parser.metavar,
309
+ ...parser.metavar != null ? { metavar: parser.metavar } : {},
338
310
  ...parser.description != null ? { description: parser.description } : {},
339
311
  ...parser.hidden != null ? { hidden: parser.hidden } : {},
340
312
  ...parser.required != null ? { required: parser.required } : {},
341
- ...parser.default != null ? { default: parser.default } : {}
313
+ ...parser.default != null ? { default: parser.default } : {},
314
+ ...parser.multiple != null ? { multiple: parser.multiple } : {}
342
315
  };
343
316
  const opts = parser;
344
- return {
317
+ return pureParse({
345
318
  type: "positional",
346
319
  ...opts?.description != null ? { description: opts.description } : {},
347
320
  ...opts?.hidden != null ? { hidden: opts.hidden } : {},
348
321
  ...opts?.short != null ? { short: opts.short } : {},
349
- ...opts?.required != null ? { required: opts.required } : {}
350
- };
322
+ ...opts?.required != null ? { required: opts.required } : {},
323
+ parse(value) {
324
+ return value;
325
+ }
326
+ });
351
327
  }
352
- /**
353
- * Create an enum-like argument schema with literal type inference.
354
- *
355
- * Uses `const T` generic to infer literal union types from the values array.
356
- *
357
- * @typeParam T - The readonly array of allowed string values.
358
- *
359
- * @param values - Allowed values.
360
- * @param opts - Common options (description, short, required).
361
- * @returns A combinator schema that resolves to a union of the allowed values.
362
- *
363
- * @example
364
- * ```ts
365
- * const args = {
366
- * level: choice(['debug', 'info', 'warn', 'error'] as const)
367
- * }
368
- * // typeof values.level === 'debug' | 'info' | 'warn' | 'error'
369
- * ```
370
- *
371
- * @experimental
372
- */
373
328
  // @__NO_SIDE_EFFECTS__
374
329
  function choice(values, opts) {
375
- return {
330
+ return pureParse({
376
331
  type: "enum",
377
332
  metavar: values.join("|"),
378
333
  choices: values,
@@ -384,38 +339,8 @@ function choice(values, opts) {
384
339
  if (!values.includes(value)) throw createInvalidChoiceError(`Value must be one of: ${values.join(", ")}`, values, value);
385
340
  return value;
386
341
  }
387
- };
342
+ });
388
343
  }
389
- /**
390
- * Create a custom argument schema with a user-defined parse function.
391
- *
392
- * This is the most general custom combinator. Use it when none of the built-in
393
- * base combinators ({@link string}, {@link number}, {@link integer},
394
- * {@link float}, {@link boolean}, {@link choice}) fit your needs.
395
- *
396
- * The returned schema has `type: 'custom'`.
397
- *
398
- * @typeParam T - The parsed value type.
399
- *
400
- * @param config - Configuration with a parse function and optional metavar.
401
- * @returns A combinator schema that resolves to the parse function's return type.
402
- *
403
- * @example
404
- * ```ts
405
- * const date = combinator({
406
- * parse: (value) => {
407
- * const d = new Date(value)
408
- * if (isNaN(d.getTime())) {
409
- * throw new Error('Invalid date format')
410
- * }
411
- * return d
412
- * },
413
- * metavar: 'date'
414
- * })
415
- * ```
416
- *
417
- * @experimental
418
- */
419
344
  // @__NO_SIDE_EFFECTS__
420
345
  function combinator(config) {
421
346
  return {
@@ -428,28 +353,6 @@ function combinator(config) {
428
353
  parse: config.parse
429
354
  };
430
355
  }
431
- /**
432
- * Transform the output of a combinator schema.
433
- *
434
- * Creates a new schema that applies `transform` to the result of `schema.parse`.
435
- * The original schema is not modified.
436
- *
437
- * @typeParam T - The input schema's parsed type.
438
- * @typeParam U - The transformed type.
439
- *
440
- * @param schema - The base combinator schema.
441
- * @param transform - The transformation function.
442
- * @returns A new combinator schema that resolves to the transformed type.
443
- *
444
- * @example
445
- * ```ts
446
- * const args = {
447
- * doubled: map(integer(), n => n * 2)
448
- * }
449
- * ```
450
- *
451
- * @experimental
452
- */
453
356
  // @__NO_SIDE_EFFECTS__
454
357
  function map(schema, transform) {
455
358
  const baseParse = schema.parse;
@@ -460,26 +363,6 @@ function map(schema, transform) {
460
363
  }
461
364
  };
462
365
  }
463
- /**
464
- * Set a default value on a combinator schema.
465
- *
466
- * The original schema is not modified.
467
- *
468
- * @typeParam T - The schema's parsed type.
469
- *
470
- * @param schema - The base combinator schema.
471
- * @param defaultValue - The default value.
472
- * @returns A new schema with the default value set.
473
- *
474
- * @example
475
- * ```ts
476
- * const args = {
477
- * port: withDefault(integer({ min: 1, max: 65535 }), 8080)
478
- * }
479
- * ```
480
- *
481
- * @experimental
482
- */
483
366
  // @__NO_SIDE_EFFECTS__
484
367
  function withDefault(schema, defaultValue) {
485
368
  return {
@@ -487,26 +370,6 @@ function withDefault(schema, defaultValue) {
487
370
  default: defaultValue
488
371
  };
489
372
  }
490
- /**
491
- * Mark a combinator schema as accepting multiple values.
492
- *
493
- * The resolved value becomes an array. The original schema is not modified.
494
- * Other modifiers on `schema` (for example {@link required}) are kept.
495
- *
496
- * @typeParam S - The input combinator schema.
497
- * @param schema - The base combinator schema.
498
- * @returns A copy of `schema` with `multiple: true`.
499
- *
500
- * @example
501
- * ```ts
502
- * const args = {
503
- * tags: multiple(string())
504
- * }
505
- * // typeof values.tags === string[]
506
- * ```
507
- *
508
- * @experimental
509
- */
510
373
  // @__NO_SIDE_EFFECTS__
511
374
  function multiple(schema) {
512
375
  return {
@@ -514,26 +377,6 @@ function multiple(schema) {
514
377
  multiple: true
515
378
  };
516
379
  }
517
- /**
518
- * Mark a combinator schema as required.
519
- *
520
- * The original schema is not modified.
521
- * Other modifiers on `schema` (for example {@link multiple}) are kept.
522
- *
523
- * @typeParam S - The input combinator schema.
524
- *
525
- * @param schema - The base combinator schema.
526
- * @returns A copy of `schema` with `required: true`.
527
- *
528
- * @example
529
- * ```ts
530
- * const args = {
531
- * name: required(string())
532
- * }
533
- * ```
534
- *
535
- * @experimental
536
- */
537
380
  // @__NO_SIDE_EFFECTS__
538
381
  function required(schema) {
539
382
  return {
@@ -541,28 +384,6 @@ function required(schema) {
541
384
  required: true
542
385
  };
543
386
  }
544
- /**
545
- * Set a short alias on a combinator schema.
546
- *
547
- * The original schema is not modified.
548
- *
549
- * @typeParam T - The schema's parsed type.
550
- * @typeParam S - The short alias string literal type.
551
- *
552
- * @param schema - The base combinator schema.
553
- * @param alias - Single character short alias.
554
- * @returns A new schema with the short alias set.
555
- *
556
- * @example
557
- * ```ts
558
- * const args = {
559
- * verbose: short(boolean(), 'v')
560
- * }
561
- * // Usage: -v or --verbose
562
- * ```
563
- *
564
- * @experimental
565
- */
566
387
  // @__NO_SIDE_EFFECTS__
567
388
  function short(schema, alias) {
568
389
  return {
@@ -570,27 +391,6 @@ function short(schema, alias) {
570
391
  short: alias
571
392
  };
572
393
  }
573
- /**
574
- * Set a description on a combinator schema for help text generation.
575
- *
576
- * The original schema is not modified.
577
- *
578
- * @typeParam T - The schema's parsed type.
579
- * @typeParam D - The description string literal type.
580
- *
581
- * @param schema - The base combinator schema.
582
- * @param text - Human-readable description.
583
- * @returns A new schema with the description set.
584
- *
585
- * @example
586
- * ```ts
587
- * const args = {
588
- * port: describe(integer(), 'Port number to listen on')
589
- * }
590
- * ```
591
- *
592
- * @experimental
593
- */
594
394
  // @__NO_SIDE_EFFECTS__
595
395
  function describe(schema, text) {
596
396
  return {
@@ -598,26 +398,6 @@ function describe(schema, text) {
598
398
  description: text
599
399
  };
600
400
  }
601
- /**
602
- * Hide a combinator schema from generated help or usage output.
603
- *
604
- * The original schema is not modified. This only marks renderer metadata and
605
- * does not change parsing, validation, defaults, conflicts, or resolved values.
606
- *
607
- * @typeParam T - The schema type.
608
- *
609
- * @param schema - The base combinator schema.
610
- * @returns A new schema with `hidden: true`.
611
- *
612
- * @example
613
- * ```ts
614
- * const args = {
615
- * legacy: hidden(string())
616
- * }
617
- * ```
618
- *
619
- * @experimental
620
- */
621
401
  // @__NO_SIDE_EFFECTS__
622
402
  function hidden(schema) {
623
403
  return {
@@ -625,28 +405,6 @@ function hidden(schema) {
625
405
  hidden: true
626
406
  };
627
407
  }
628
- /**
629
- * Mark a combinator schema as not required.
630
- *
631
- * Useful for overriding a base combinator that was created with `required: true`,
632
- * or for making a positional argument explicitly optional.
633
- * The original schema is not modified.
634
- *
635
- * @typeParam T - The schema type.
636
- *
637
- * @param schema - The base combinator schema.
638
- * @returns A new schema with `required: false`.
639
- *
640
- * @example
641
- * ```ts
642
- * const args = {
643
- * name: unrequired(string({ required: true })),
644
- * query: unrequired(positional())
645
- * }
646
- * ```
647
- *
648
- * @experimental
649
- */
650
408
  // @__NO_SIDE_EFFECTS__
651
409
  function unrequired(schema) {
652
410
  return {
@@ -672,8 +430,6 @@ function unrequired(schema) {
672
430
  * help: short(boolean(), 'h')
673
431
  * })
674
432
  * ```
675
- *
676
- * @experimental
677
433
  */
678
434
  // @__NO_SIDE_EFFECTS__
679
435
  function args(fields) {
@@ -703,8 +459,6 @@ function merge(...schemas) {
703
459
  * const base = args({ port: withDefault(integer(), 8080) })
704
460
  * const strict = extend(base, { port: required(integer({ min: 1, max: 65535 })) })
705
461
  * ```
706
- *
707
- * @experimental
708
462
  */
709
463
  // @__NO_SIDE_EFFECTS__
710
464
  function extend(base, overrides) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@gunshi/combinators",
3
3
  "description": "parser combinators for gunshi argument schema",
4
- "version": "0.37.2",
4
+ "version": "1.0.0-beta.0",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -57,7 +57,7 @@
57
57
  "jsr-exports-lint": "^0.4.2",
58
58
  "publint": "^0.3.20",
59
59
  "tsdown": "0.21.0",
60
- "gunshi": "0.37.2"
60
+ "gunshi": "1.0.0-beta.0"
61
61
  },
62
62
  "scripts": {
63
63
  "build": "tsdown",