@gunshi/combinators 0.37.3 → 1.0.0-beta.1

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.29.0/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.29.0/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,7 +70,11 @@ 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
  /**
51
80
  * Brand that marks {@link ArgsValidationError} instances.
@@ -98,7 +127,7 @@ var ArgsValidationError = class extends Error {
98
127
  }
99
128
  };
100
129
  //#endregion
101
- //#region ../../node_modules/.pnpm/args-tokens@0.29.0/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
102
131
  /**
103
132
  * Parser combinator factory functions for composable argument schema construction.
104
133
  *
@@ -111,14 +140,10 @@ var ArgsValidationError = class extends Error {
111
140
  * integer,
112
141
  * boolean,
113
142
  * positional,
114
- * choice,
115
143
  * withDefault,
116
- * multiple,
117
144
  * required,
118
145
  * short,
119
- * map,
120
- * merge,
121
- * extend
146
+ * merge
122
147
  * } from 'args-tokens/combinators'
123
148
  *
124
149
  * // Define reusable schema groups with args()
@@ -146,8 +171,6 @@ var ArgsValidationError = class extends Error {
146
171
  * const { values } = resolveArgs(schema, tokens)
147
172
  * ```
148
173
  *
149
- * @experimental This module is experimental and may change in future versions.
150
- *
151
174
  * @module
152
175
  */
153
176
  /**
@@ -175,23 +198,21 @@ function createInvalidChoiceError(message, choices, actual) {
175
198
  });
176
199
  }
177
200
  /**
178
- * Create a string argument schema with optional validation.
179
- *
180
- * @param opts - Validation options.
181
- * @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.
182
203
  *
183
- * @example
184
- * ```ts
185
- * const args = {
186
- * name: string({ minLength: 1, maxLength: 50 })
187
- * }
188
- * ```
204
+ * @typeParam S - The schema type.
189
205
  *
190
- * @experimental
206
+ * @param schema - A schema whose `parse` function has no side effects.
207
+ * @returns The same schema.
191
208
  */
209
+ function pureParse(schema) {
210
+ Object.defineProperty(schema.parse, PURE_PARSE, { value: true });
211
+ return schema;
212
+ }
192
213
  // @__NO_SIDE_EFFECTS__
193
214
  function string(opts) {
194
- return {
215
+ const schema = {
195
216
  type: "string",
196
217
  metavar: "string",
197
218
  ...opts?.description != null ? { description: opts.description } : {},
@@ -205,27 +226,12 @@ function string(opts) {
205
226
  return value;
206
227
  }
207
228
  };
229
+ Object.defineProperty(schema.parse, PURE_PARSE, { get: () => !opts?.pattern?.global && !opts?.pattern?.sticky });
230
+ return schema;
208
231
  }
209
- /**
210
- * Create a number argument schema with optional range validation.
211
- *
212
- * Accepts any numeric value (integer or float).
213
- *
214
- * @param opts - Range options.
215
- * @returns A combinator schema that resolves to number.
216
- *
217
- * @example
218
- * ```ts
219
- * const args = {
220
- * timeout: number({ min: 0, max: 30000 })
221
- * }
222
- * ```
223
- *
224
- * @experimental
225
- */
226
232
  // @__NO_SIDE_EFFECTS__
227
233
  function number(opts) {
228
- return {
234
+ return pureParse({
229
235
  type: "number",
230
236
  metavar: "number",
231
237
  ...opts?.description != null ? { description: opts.description } : {},
@@ -239,28 +245,11 @@ function number(opts) {
239
245
  if (opts?.max != null && n > opts.max) throw new RangeError(`Number must be <= ${opts.max}, got ${n}`);
240
246
  return n;
241
247
  }
242
- };
248
+ });
243
249
  }
244
- /**
245
- * Create an integer argument schema with optional range validation.
246
- *
247
- * Only accepts integer values (no decimals).
248
- *
249
- * @param opts - Range options.
250
- * @returns A combinator schema that resolves to number (integer).
251
- *
252
- * @example
253
- * ```ts
254
- * const args = {
255
- * retries: integer({ min: 0, max: 10 })
256
- * }
257
- * ```
258
- *
259
- * @experimental
260
- */
261
250
  // @__NO_SIDE_EFFECTS__
262
251
  function integer(opts) {
263
- return {
252
+ return pureParse({
264
253
  type: "custom",
265
254
  metavar: "integer",
266
255
  ...opts?.description != null ? { description: opts.description } : {},
@@ -275,28 +264,11 @@ function integer(opts) {
275
264
  if (opts?.max != null && n > opts.max) throw new RangeError(`Integer must be <= ${opts.max}, got ${n}`);
276
265
  return n;
277
266
  }
278
- };
267
+ });
279
268
  }
280
- /**
281
- * Create a floating-point argument schema with optional range validation.
282
- *
283
- * Rejects `NaN` and `Infinity` values.
284
- *
285
- * @param opts - Range options.
286
- * @returns A combinator schema that resolves to number (float).
287
- *
288
- * @example
289
- * ```ts
290
- * const args = {
291
- * ratio: float({ min: 0, max: 1 })
292
- * }
293
- * ```
294
- *
295
- * @experimental
296
- */
297
269
  // @__NO_SIDE_EFFECTS__
298
270
  function float(opts) {
299
- return {
271
+ return pureParse({
300
272
  type: "custom",
301
273
  metavar: "float",
302
274
  ...opts?.description != null ? { description: opts.description } : {},
@@ -312,27 +284,8 @@ function float(opts) {
312
284
  if (opts?.max != null && n > opts.max) throw new RangeError(`Float must be <= ${opts.max}, got ${n}`);
313
285
  return n;
314
286
  }
315
- };
287
+ });
316
288
  }
317
- /**
318
- * Create a boolean argument schema.
319
- *
320
- * Boolean arguments are existence-based. The resolver passes `"true"` or `"false"`
321
- * to the parse function based on the presence or negation of the flag.
322
- *
323
- * @param opts - Boolean options.
324
- * @returns A combinator schema for boolean flags.
325
- *
326
- * @example
327
- * ```ts
328
- * const args = {
329
- * color: boolean({ negatable: true })
330
- * }
331
- * // Usage: --color (true), --no-color (false)
332
- * ```
333
- *
334
- * @experimental
335
- */
336
289
  // @__NO_SIDE_EFFECTS__
337
290
  function boolean(opts) {
338
291
  return {
@@ -353,45 +306,28 @@ function positional(parser) {
353
306
  if (parser && "parse" in parser) return {
354
307
  type: "positional",
355
308
  parse: parser.parse,
356
- metavar: parser.metavar,
309
+ ...parser.metavar != null ? { metavar: parser.metavar } : {},
357
310
  ...parser.description != null ? { description: parser.description } : {},
358
311
  ...parser.hidden != null ? { hidden: parser.hidden } : {},
359
312
  ...parser.required != null ? { required: parser.required } : {},
360
- ...parser.default != null ? { default: parser.default } : {}
313
+ ...parser.default != null ? { default: parser.default } : {},
314
+ ...parser.multiple != null ? { multiple: parser.multiple } : {}
361
315
  };
362
316
  const opts = parser;
363
- return {
317
+ return pureParse({
364
318
  type: "positional",
365
319
  ...opts?.description != null ? { description: opts.description } : {},
366
320
  ...opts?.hidden != null ? { hidden: opts.hidden } : {},
367
321
  ...opts?.short != null ? { short: opts.short } : {},
368
- ...opts?.required != null ? { required: opts.required } : {}
369
- };
322
+ ...opts?.required != null ? { required: opts.required } : {},
323
+ parse(value) {
324
+ return value;
325
+ }
326
+ });
370
327
  }
371
- /**
372
- * Create an enum-like argument schema with literal type inference.
373
- *
374
- * Uses `const T` generic to infer literal union types from the values array.
375
- *
376
- * @typeParam T - The readonly array of allowed string values.
377
- *
378
- * @param values - Allowed values.
379
- * @param opts - Common options (description, short, required).
380
- * @returns A combinator schema that resolves to a union of the allowed values.
381
- *
382
- * @example
383
- * ```ts
384
- * const args = {
385
- * level: choice(['debug', 'info', 'warn', 'error'] as const)
386
- * }
387
- * // typeof values.level === 'debug' | 'info' | 'warn' | 'error'
388
- * ```
389
- *
390
- * @experimental
391
- */
392
328
  // @__NO_SIDE_EFFECTS__
393
329
  function choice(values, opts) {
394
- return {
330
+ return pureParse({
395
331
  type: "enum",
396
332
  metavar: values.join("|"),
397
333
  choices: values,
@@ -403,38 +339,8 @@ function choice(values, opts) {
403
339
  if (!values.includes(value)) throw createInvalidChoiceError(`Value must be one of: ${values.join(", ")}`, values, value);
404
340
  return value;
405
341
  }
406
- };
342
+ });
407
343
  }
408
- /**
409
- * Create a custom argument schema with a user-defined parse function.
410
- *
411
- * This is the most general custom combinator. Use it when none of the built-in
412
- * base combinators ({@link string}, {@link number}, {@link integer},
413
- * {@link float}, {@link boolean}, {@link choice}) fit your needs.
414
- *
415
- * The returned schema has `type: 'custom'`.
416
- *
417
- * @typeParam T - The parsed value type.
418
- *
419
- * @param config - Configuration with a parse function and optional metavar.
420
- * @returns A combinator schema that resolves to the parse function's return type.
421
- *
422
- * @example
423
- * ```ts
424
- * const date = combinator({
425
- * parse: (value) => {
426
- * const d = new Date(value)
427
- * if (isNaN(d.getTime())) {
428
- * throw new Error('Invalid date format')
429
- * }
430
- * return d
431
- * },
432
- * metavar: 'date'
433
- * })
434
- * ```
435
- *
436
- * @experimental
437
- */
438
344
  // @__NO_SIDE_EFFECTS__
439
345
  function combinator(config) {
440
346
  return {
@@ -447,28 +353,6 @@ function combinator(config) {
447
353
  parse: config.parse
448
354
  };
449
355
  }
450
- /**
451
- * Transform the output of a combinator schema.
452
- *
453
- * Creates a new schema that applies `transform` to the result of `schema.parse`.
454
- * The original schema is not modified.
455
- *
456
- * @typeParam T - The input schema's parsed type.
457
- * @typeParam U - The transformed type.
458
- *
459
- * @param schema - The base combinator schema.
460
- * @param transform - The transformation function.
461
- * @returns A new combinator schema that resolves to the transformed type.
462
- *
463
- * @example
464
- * ```ts
465
- * const args = {
466
- * doubled: map(integer(), n => n * 2)
467
- * }
468
- * ```
469
- *
470
- * @experimental
471
- */
472
356
  // @__NO_SIDE_EFFECTS__
473
357
  function map(schema, transform) {
474
358
  const baseParse = schema.parse;
@@ -479,26 +363,6 @@ function map(schema, transform) {
479
363
  }
480
364
  };
481
365
  }
482
- /**
483
- * Set a default value on a combinator schema.
484
- *
485
- * The original schema is not modified.
486
- *
487
- * @typeParam T - The schema's parsed type.
488
- *
489
- * @param schema - The base combinator schema.
490
- * @param defaultValue - The default value.
491
- * @returns A new schema with the default value set.
492
- *
493
- * @example
494
- * ```ts
495
- * const args = {
496
- * port: withDefault(integer({ min: 1, max: 65535 }), 8080)
497
- * }
498
- * ```
499
- *
500
- * @experimental
501
- */
502
366
  // @__NO_SIDE_EFFECTS__
503
367
  function withDefault(schema, defaultValue) {
504
368
  return {
@@ -506,26 +370,6 @@ function withDefault(schema, defaultValue) {
506
370
  default: defaultValue
507
371
  };
508
372
  }
509
- /**
510
- * Mark a combinator schema as accepting multiple values.
511
- *
512
- * The resolved value becomes an array. The original schema is not modified.
513
- * Other modifiers on `schema` (for example {@link required}) are kept.
514
- *
515
- * @typeParam S - The input combinator schema.
516
- * @param schema - The base combinator schema.
517
- * @returns A copy of `schema` with `multiple: true`.
518
- *
519
- * @example
520
- * ```ts
521
- * const args = {
522
- * tags: multiple(string())
523
- * }
524
- * // typeof values.tags === string[]
525
- * ```
526
- *
527
- * @experimental
528
- */
529
373
  // @__NO_SIDE_EFFECTS__
530
374
  function multiple(schema) {
531
375
  return {
@@ -533,26 +377,6 @@ function multiple(schema) {
533
377
  multiple: true
534
378
  };
535
379
  }
536
- /**
537
- * Mark a combinator schema as required.
538
- *
539
- * The original schema is not modified.
540
- * Other modifiers on `schema` (for example {@link multiple}) are kept.
541
- *
542
- * @typeParam S - The input combinator schema.
543
- *
544
- * @param schema - The base combinator schema.
545
- * @returns A copy of `schema` with `required: true`.
546
- *
547
- * @example
548
- * ```ts
549
- * const args = {
550
- * name: required(string())
551
- * }
552
- * ```
553
- *
554
- * @experimental
555
- */
556
380
  // @__NO_SIDE_EFFECTS__
557
381
  function required(schema) {
558
382
  return {
@@ -560,28 +384,6 @@ function required(schema) {
560
384
  required: true
561
385
  };
562
386
  }
563
- /**
564
- * Set a short alias on a combinator schema.
565
- *
566
- * The original schema is not modified.
567
- *
568
- * @typeParam T - The schema's parsed type.
569
- * @typeParam S - The short alias string literal type.
570
- *
571
- * @param schema - The base combinator schema.
572
- * @param alias - Single character short alias.
573
- * @returns A new schema with the short alias set.
574
- *
575
- * @example
576
- * ```ts
577
- * const args = {
578
- * verbose: short(boolean(), 'v')
579
- * }
580
- * // Usage: -v or --verbose
581
- * ```
582
- *
583
- * @experimental
584
- */
585
387
  // @__NO_SIDE_EFFECTS__
586
388
  function short(schema, alias) {
587
389
  return {
@@ -589,27 +391,6 @@ function short(schema, alias) {
589
391
  short: alias
590
392
  };
591
393
  }
592
- /**
593
- * Set a description on a combinator schema for help text generation.
594
- *
595
- * The original schema is not modified.
596
- *
597
- * @typeParam T - The schema's parsed type.
598
- * @typeParam D - The description string literal type.
599
- *
600
- * @param schema - The base combinator schema.
601
- * @param text - Human-readable description.
602
- * @returns A new schema with the description set.
603
- *
604
- * @example
605
- * ```ts
606
- * const args = {
607
- * port: describe(integer(), 'Port number to listen on')
608
- * }
609
- * ```
610
- *
611
- * @experimental
612
- */
613
394
  // @__NO_SIDE_EFFECTS__
614
395
  function describe(schema, text) {
615
396
  return {
@@ -617,26 +398,6 @@ function describe(schema, text) {
617
398
  description: text
618
399
  };
619
400
  }
620
- /**
621
- * Hide a combinator schema from generated help or usage output.
622
- *
623
- * The original schema is not modified. This only marks renderer metadata and
624
- * does not change parsing, validation, defaults, conflicts, or resolved values.
625
- *
626
- * @typeParam T - The schema type.
627
- *
628
- * @param schema - The base combinator schema.
629
- * @returns A new schema with `hidden: true`.
630
- *
631
- * @example
632
- * ```ts
633
- * const args = {
634
- * legacy: hidden(string())
635
- * }
636
- * ```
637
- *
638
- * @experimental
639
- */
640
401
  // @__NO_SIDE_EFFECTS__
641
402
  function hidden(schema) {
642
403
  return {
@@ -644,28 +405,6 @@ function hidden(schema) {
644
405
  hidden: true
645
406
  };
646
407
  }
647
- /**
648
- * Mark a combinator schema as not required.
649
- *
650
- * Useful for overriding a base combinator that was created with `required: true`,
651
- * or for making a positional argument explicitly optional.
652
- * The original schema is not modified.
653
- *
654
- * @typeParam T - The schema type.
655
- *
656
- * @param schema - The base combinator schema.
657
- * @returns A new schema with `required: false`.
658
- *
659
- * @example
660
- * ```ts
661
- * const args = {
662
- * name: unrequired(string({ required: true })),
663
- * query: unrequired(positional())
664
- * }
665
- * ```
666
- *
667
- * @experimental
668
- */
669
408
  // @__NO_SIDE_EFFECTS__
670
409
  function unrequired(schema) {
671
410
  return {
@@ -691,8 +430,6 @@ function unrequired(schema) {
691
430
  * help: short(boolean(), 'h')
692
431
  * })
693
432
  * ```
694
- *
695
- * @experimental
696
433
  */
697
434
  // @__NO_SIDE_EFFECTS__
698
435
  function args(fields) {
@@ -722,8 +459,6 @@ function merge(...schemas) {
722
459
  * const base = args({ port: withDefault(integer(), 8080) })
723
460
  * const strict = extend(base, { port: required(integer({ min: 1, max: 65535 })) })
724
461
  * ```
725
- *
726
- * @experimental
727
462
  */
728
463
  // @__NO_SIDE_EFFECTS__
729
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.3",
4
+ "version": "1.0.0-beta.1",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -53,11 +53,11 @@
53
53
  },
54
54
  "devDependencies": {
55
55
  "deno": "^2.7.14",
56
+ "gunshi": "1.0.0-beta.1",
56
57
  "jsr": "^0.14.3",
57
58
  "jsr-exports-lint": "^0.4.2",
58
59
  "publint": "^0.3.20",
59
- "tsdown": "0.21.0",
60
- "gunshi": "0.37.3"
60
+ "tsdown": "0.22.14"
61
61
  },
62
62
  "scripts": {
63
63
  "build": "tsdown",