@optique/core 0.1.0-dev.1 → 0.1.0-dev.3
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/README.md +29 -1
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/parser.cjs +55 -0
- package/dist/parser.d.cts +35 -1
- package/dist/parser.d.ts +35 -1
- package/dist/parser.js +55 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -46,10 +46,12 @@ Optique is built around several key concepts:
|
|
|
46
46
|
- `option()` for command-line flags and their arguments
|
|
47
47
|
- `argument()` for positional arguments
|
|
48
48
|
- `command()` for subcommands
|
|
49
|
+
- `constant()` for literal values (essential for discriminated unions)
|
|
49
50
|
|
|
50
51
|
- *Modifying combinators* transform and combine parsers:
|
|
51
52
|
- `optional()` makes parsers optional
|
|
52
53
|
- `withDefault()` provides default values for optional parsers
|
|
54
|
+
- `map()` transforms parsed values using mapping functions
|
|
53
55
|
- `multiple()` allows repetition
|
|
54
56
|
- `or()` creates alternatives
|
|
55
57
|
- `merge()` combines object parsers
|
|
@@ -189,6 +191,13 @@ Optique provides several types of parsers and combinators for building sophistic
|
|
|
189
191
|
command("add", object({ file: argument(string()) }))
|
|
190
192
|
~~~~
|
|
191
193
|
|
|
194
|
+
- **`constant()`**: Produces a constant value without consuming input, essential for discriminated unions
|
|
195
|
+
|
|
196
|
+
~~~~ typescript
|
|
197
|
+
constant("add") // Always returns "add"
|
|
198
|
+
constant(42) // Always returns 42
|
|
199
|
+
~~~~
|
|
200
|
+
|
|
192
201
|
### Modifying combinators
|
|
193
202
|
|
|
194
203
|
- **`optional()`**: Makes any parser optional, returning `undefined` if not
|
|
@@ -212,6 +221,20 @@ Optique provides several types of parsers and combinators for building sophistic
|
|
|
212
221
|
});
|
|
213
222
|
~~~~
|
|
214
223
|
|
|
224
|
+
- **`map()`**: Transforms the parsed value using a mapping function while
|
|
225
|
+
preserving the original parsing logic
|
|
226
|
+
|
|
227
|
+
~~~~ typescript
|
|
228
|
+
const parser = object({
|
|
229
|
+
// Transform boolean flag to its inverse
|
|
230
|
+
disallow: map(option("--allow"), b => !b),
|
|
231
|
+
// Transform string to uppercase
|
|
232
|
+
upperName: map(option("-n", "--name", string()), s => s.toUpperCase()),
|
|
233
|
+
// Transform number to formatted string
|
|
234
|
+
portDesc: map(option("-p", "--port", integer()), n => `port: ${n}`),
|
|
235
|
+
});
|
|
236
|
+
~~~~
|
|
237
|
+
|
|
215
238
|
- **`multiple()`**: Allows repeating a parser multiple times with constraints
|
|
216
239
|
|
|
217
240
|
~~~~ typescript
|
|
@@ -421,11 +444,16 @@ This example demonstrates:
|
|
|
421
444
|
- *Subcommand routing*: `command("show", ...)` matches the first argument
|
|
422
445
|
and applies the inner parser to remaining arguments
|
|
423
446
|
- *Type discrimination*: Using `constant()` with unique values enables
|
|
424
|
-
TypeScript to discriminate between subcommand types
|
|
447
|
+
TypeScript to discriminate between subcommand types in the result union
|
|
425
448
|
- *Per-subcommand options*: Each subcommand can have its own unique set
|
|
426
449
|
of options and arguments
|
|
427
450
|
- *Complex arguments*: The `delete` command shows multiple required arguments
|
|
428
451
|
|
|
452
|
+
The `constant()` parser is crucial here—it adds a literal value to each
|
|
453
|
+
subcommand's result object without consuming any input. This creates
|
|
454
|
+
a discriminated union type that TypeScript can use for type narrowing in
|
|
455
|
+
`switch` statements or type guards.
|
|
456
|
+
|
|
429
457
|
Example usage:
|
|
430
458
|
|
|
431
459
|
~~~~ bash
|
package/dist/index.cjs
CHANGED
|
@@ -19,6 +19,7 @@ exports.getDocPage = require_parser.getDocPage;
|
|
|
19
19
|
exports.integer = require_valueparser.integer;
|
|
20
20
|
exports.isValueParser = require_valueparser.isValueParser;
|
|
21
21
|
exports.locale = require_valueparser.locale;
|
|
22
|
+
exports.map = require_parser.map;
|
|
22
23
|
exports.merge = require_parser.merge;
|
|
23
24
|
exports.message = require_message.message;
|
|
24
25
|
exports.metavar = require_message.metavar;
|
package/dist/index.d.cts
CHANGED
|
@@ -2,6 +2,6 @@ import { Message, MessageFormatOptions, MessageTerm, formatMessage, message, met
|
|
|
2
2
|
import { OptionName, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, formatUsage, formatUsageTerm, normalizeUsage } from "./usage.cjs";
|
|
3
3
|
import { DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, formatDocPage } from "./doc.cjs";
|
|
4
4
|
import { ChoiceOptions, FloatOptions, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, StringOptions, UrlOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.cjs";
|
|
5
|
-
import { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, constant, getDocPage, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.cjs";
|
|
5
|
+
import { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.cjs";
|
|
6
6
|
import { RunError, RunOptions, run } from "./facade.cjs";
|
|
7
|
-
export { ArgumentOptions, ChoiceOptions, CommandOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, FloatOptions, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, Message, MessageFormatOptions, MessageTerm, MultipleOptions, OptionName, OptionOptions, Parser, ParserContext, ParserResult, Result, RunError, RunOptions, StringOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, argument, choice, command, constant, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, integer, isValueParser, locale, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
|
7
|
+
export { ArgumentOptions, ChoiceOptions, CommandOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, FloatOptions, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, Message, MessageFormatOptions, MessageTerm, MultipleOptions, OptionName, OptionOptions, Parser, ParserContext, ParserResult, Result, RunError, RunOptions, StringOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, argument, choice, command, constant, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, integer, isValueParser, locale, map, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,6 @@ import { Message, MessageFormatOptions, MessageTerm, formatMessage, message, met
|
|
|
2
2
|
import { OptionName, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, formatUsage, formatUsageTerm, normalizeUsage } from "./usage.js";
|
|
3
3
|
import { DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, formatDocPage } from "./doc.js";
|
|
4
4
|
import { ChoiceOptions, FloatOptions, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, StringOptions, UrlOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.js";
|
|
5
|
-
import { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, constant, getDocPage, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.js";
|
|
5
|
+
import { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.js";
|
|
6
6
|
import { RunError, RunOptions, run } from "./facade.js";
|
|
7
|
-
export { ArgumentOptions, ChoiceOptions, CommandOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, FloatOptions, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, Message, MessageFormatOptions, MessageTerm, MultipleOptions, OptionName, OptionOptions, Parser, ParserContext, ParserResult, Result, RunError, RunOptions, StringOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, argument, choice, command, constant, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, integer, isValueParser, locale, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
|
7
|
+
export { ArgumentOptions, ChoiceOptions, CommandOptions, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, FloatOptions, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, LocaleOptions, Message, MessageFormatOptions, MessageTerm, MultipleOptions, OptionName, OptionOptions, Parser, ParserContext, ParserResult, Result, RunError, RunOptions, StringOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, argument, choice, command, constant, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, integer, isValueParser, locale, map, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { formatMessage, message, metavar, optionName, optionNames, text, value,
|
|
|
2
2
|
import { formatUsage, formatUsageTerm, normalizeUsage } from "./usage.js";
|
|
3
3
|
import { formatDocPage } from "./doc.js";
|
|
4
4
|
import { choice, float, integer, isValueParser, locale, string, url, uuid } from "./valueparser.js";
|
|
5
|
-
import { argument, command, constant, getDocPage, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.js";
|
|
5
|
+
import { argument, command, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault } from "./parser.js";
|
|
6
6
|
import { RunError, run } from "./facade.js";
|
|
7
7
|
|
|
8
|
-
export { RunError, argument, choice, command, constant, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, integer, isValueParser, locale, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
|
8
|
+
export { RunError, argument, choice, command, constant, float, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getDocPage, integer, isValueParser, locale, map, merge, message, metavar, multiple, normalizeUsage, object, option, optionName, optionNames, optional, or, parse, run, string, text, tuple, url, uuid, value, values, withDefault };
|
package/dist/parser.cjs
CHANGED
|
@@ -413,6 +413,60 @@ function withDefault(parser, defaultValue) {
|
|
|
413
413
|
};
|
|
414
414
|
}
|
|
415
415
|
/**
|
|
416
|
+
* Creates a parser that transforms the result value of another parser using
|
|
417
|
+
* a mapping function. This enables value transformation while preserving
|
|
418
|
+
* the original parser's parsing logic and state management.
|
|
419
|
+
*
|
|
420
|
+
* The `map()` function is useful for:
|
|
421
|
+
* - Converting parsed values to different types
|
|
422
|
+
* - Applying transformations like string formatting or boolean inversion
|
|
423
|
+
* - Computing derived values from parsed input
|
|
424
|
+
* - Creating reusable transformations that can be applied to any parser
|
|
425
|
+
*
|
|
426
|
+
* @template T The type of the value produced by the original parser.
|
|
427
|
+
* @template U The type of the value produced by the mapping function.
|
|
428
|
+
* @template TState The type of the state used by the original parser.
|
|
429
|
+
* @param parser The {@link Parser} whose result will be transformed.
|
|
430
|
+
* @param transform A function that transforms the parsed value from type T to type U.
|
|
431
|
+
* @returns A {@link Parser} that produces the transformed value of type U
|
|
432
|
+
* while preserving the original parser's state type and parsing behavior.
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
* ```typescript
|
|
436
|
+
* // Transform boolean flag to its inverse
|
|
437
|
+
* const parser = object({
|
|
438
|
+
* disallow: map(option("--allow"), b => !b)
|
|
439
|
+
* });
|
|
440
|
+
*
|
|
441
|
+
* // Transform string to uppercase
|
|
442
|
+
* const upperParser = map(argument(string()), s => s.toUpperCase());
|
|
443
|
+
*
|
|
444
|
+
* // Transform number to formatted string
|
|
445
|
+
* const prefixedParser = map(option("-n", integer()), n => `value: ${n}`);
|
|
446
|
+
* ```
|
|
447
|
+
*/
|
|
448
|
+
function map(parser, transform) {
|
|
449
|
+
return {
|
|
450
|
+
$valueType: [],
|
|
451
|
+
$stateType: parser.$stateType,
|
|
452
|
+
priority: parser.priority,
|
|
453
|
+
usage: parser.usage,
|
|
454
|
+
initialState: parser.initialState,
|
|
455
|
+
parse: parser.parse.bind(parser),
|
|
456
|
+
complete(state) {
|
|
457
|
+
const result = parser.complete(state);
|
|
458
|
+
if (result.success) return {
|
|
459
|
+
success: true,
|
|
460
|
+
value: transform(result.value)
|
|
461
|
+
};
|
|
462
|
+
return result;
|
|
463
|
+
},
|
|
464
|
+
getDocFragments(state, _defaultValue) {
|
|
465
|
+
return parser.getDocFragments(state, void 0);
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
416
470
|
* Creates a parser that allows multiple occurrences of a given parser.
|
|
417
471
|
* This parser can be used to parse multiple values of the same type,
|
|
418
472
|
* such as multiple command-line arguments or options.
|
|
@@ -1054,6 +1108,7 @@ exports.argument = argument;
|
|
|
1054
1108
|
exports.command = command;
|
|
1055
1109
|
exports.constant = constant;
|
|
1056
1110
|
exports.getDocPage = getDocPage;
|
|
1111
|
+
exports.map = map;
|
|
1057
1112
|
exports.merge = merge;
|
|
1058
1113
|
exports.multiple = multiple;
|
|
1059
1114
|
exports.object = object;
|
package/dist/parser.d.cts
CHANGED
|
@@ -238,6 +238,40 @@ declare function optional<TValue, TState>(parser: Parser<TValue, TState>): Parse
|
|
|
238
238
|
* or the default value if the wrapped parser fails to match.
|
|
239
239
|
*/
|
|
240
240
|
declare function withDefault<TValue, TState>(parser: Parser<TValue, TState>, defaultValue: TValue | (() => TValue)): Parser<TValue, [TState] | undefined>;
|
|
241
|
+
/**
|
|
242
|
+
* Creates a parser that transforms the result value of another parser using
|
|
243
|
+
* a mapping function. This enables value transformation while preserving
|
|
244
|
+
* the original parser's parsing logic and state management.
|
|
245
|
+
*
|
|
246
|
+
* The `map()` function is useful for:
|
|
247
|
+
* - Converting parsed values to different types
|
|
248
|
+
* - Applying transformations like string formatting or boolean inversion
|
|
249
|
+
* - Computing derived values from parsed input
|
|
250
|
+
* - Creating reusable transformations that can be applied to any parser
|
|
251
|
+
*
|
|
252
|
+
* @template T The type of the value produced by the original parser.
|
|
253
|
+
* @template U The type of the value produced by the mapping function.
|
|
254
|
+
* @template TState The type of the state used by the original parser.
|
|
255
|
+
* @param parser The {@link Parser} whose result will be transformed.
|
|
256
|
+
* @param transform A function that transforms the parsed value from type T to type U.
|
|
257
|
+
* @returns A {@link Parser} that produces the transformed value of type U
|
|
258
|
+
* while preserving the original parser's state type and parsing behavior.
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* ```typescript
|
|
262
|
+
* // Transform boolean flag to its inverse
|
|
263
|
+
* const parser = object({
|
|
264
|
+
* disallow: map(option("--allow"), b => !b)
|
|
265
|
+
* });
|
|
266
|
+
*
|
|
267
|
+
* // Transform string to uppercase
|
|
268
|
+
* const upperParser = map(argument(string()), s => s.toUpperCase());
|
|
269
|
+
*
|
|
270
|
+
* // Transform number to formatted string
|
|
271
|
+
* const prefixedParser = map(option("-n", integer()), n => `value: ${n}`);
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
declare function map<T, U, TState>(parser: Parser<T, TState>, transform: (value: T) => U): Parser<U, TState>;
|
|
241
275
|
/**
|
|
242
276
|
* Options for the {@link multiple} parser.
|
|
243
277
|
*/
|
|
@@ -577,4 +611,4 @@ declare function parse<T>(parser: Parser<T, unknown>, args: readonly string[]):
|
|
|
577
611
|
*/
|
|
578
612
|
declare function getDocPage(parser: Parser<unknown, unknown>, args?: readonly string[]): DocPage | undefined;
|
|
579
613
|
//#endregion
|
|
580
|
-
export { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, constant, getDocPage, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
|
|
614
|
+
export { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
|
package/dist/parser.d.ts
CHANGED
|
@@ -238,6 +238,40 @@ declare function optional<TValue, TState>(parser: Parser<TValue, TState>): Parse
|
|
|
238
238
|
* or the default value if the wrapped parser fails to match.
|
|
239
239
|
*/
|
|
240
240
|
declare function withDefault<TValue, TState>(parser: Parser<TValue, TState>, defaultValue: TValue | (() => TValue)): Parser<TValue, [TState] | undefined>;
|
|
241
|
+
/**
|
|
242
|
+
* Creates a parser that transforms the result value of another parser using
|
|
243
|
+
* a mapping function. This enables value transformation while preserving
|
|
244
|
+
* the original parser's parsing logic and state management.
|
|
245
|
+
*
|
|
246
|
+
* The `map()` function is useful for:
|
|
247
|
+
* - Converting parsed values to different types
|
|
248
|
+
* - Applying transformations like string formatting or boolean inversion
|
|
249
|
+
* - Computing derived values from parsed input
|
|
250
|
+
* - Creating reusable transformations that can be applied to any parser
|
|
251
|
+
*
|
|
252
|
+
* @template T The type of the value produced by the original parser.
|
|
253
|
+
* @template U The type of the value produced by the mapping function.
|
|
254
|
+
* @template TState The type of the state used by the original parser.
|
|
255
|
+
* @param parser The {@link Parser} whose result will be transformed.
|
|
256
|
+
* @param transform A function that transforms the parsed value from type T to type U.
|
|
257
|
+
* @returns A {@link Parser} that produces the transformed value of type U
|
|
258
|
+
* while preserving the original parser's state type and parsing behavior.
|
|
259
|
+
*
|
|
260
|
+
* @example
|
|
261
|
+
* ```typescript
|
|
262
|
+
* // Transform boolean flag to its inverse
|
|
263
|
+
* const parser = object({
|
|
264
|
+
* disallow: map(option("--allow"), b => !b)
|
|
265
|
+
* });
|
|
266
|
+
*
|
|
267
|
+
* // Transform string to uppercase
|
|
268
|
+
* const upperParser = map(argument(string()), s => s.toUpperCase());
|
|
269
|
+
*
|
|
270
|
+
* // Transform number to formatted string
|
|
271
|
+
* const prefixedParser = map(option("-n", integer()), n => `value: ${n}`);
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
declare function map<T, U, TState>(parser: Parser<T, TState>, transform: (value: T) => U): Parser<U, TState>;
|
|
241
275
|
/**
|
|
242
276
|
* Options for the {@link multiple} parser.
|
|
243
277
|
*/
|
|
@@ -577,4 +611,4 @@ declare function parse<T>(parser: Parser<T, unknown>, args: readonly string[]):
|
|
|
577
611
|
*/
|
|
578
612
|
declare function getDocPage(parser: Parser<unknown, unknown>, args?: readonly string[]): DocPage | undefined;
|
|
579
613
|
//#endregion
|
|
580
|
-
export { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, constant, getDocPage, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
|
|
614
|
+
export { ArgumentOptions, CommandOptions, InferValue, MultipleOptions, OptionOptions, Parser, ParserContext, ParserResult, Result, argument, command, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
|
package/dist/parser.js
CHANGED
|
@@ -413,6 +413,60 @@ function withDefault(parser, defaultValue) {
|
|
|
413
413
|
};
|
|
414
414
|
}
|
|
415
415
|
/**
|
|
416
|
+
* Creates a parser that transforms the result value of another parser using
|
|
417
|
+
* a mapping function. This enables value transformation while preserving
|
|
418
|
+
* the original parser's parsing logic and state management.
|
|
419
|
+
*
|
|
420
|
+
* The `map()` function is useful for:
|
|
421
|
+
* - Converting parsed values to different types
|
|
422
|
+
* - Applying transformations like string formatting or boolean inversion
|
|
423
|
+
* - Computing derived values from parsed input
|
|
424
|
+
* - Creating reusable transformations that can be applied to any parser
|
|
425
|
+
*
|
|
426
|
+
* @template T The type of the value produced by the original parser.
|
|
427
|
+
* @template U The type of the value produced by the mapping function.
|
|
428
|
+
* @template TState The type of the state used by the original parser.
|
|
429
|
+
* @param parser The {@link Parser} whose result will be transformed.
|
|
430
|
+
* @param transform A function that transforms the parsed value from type T to type U.
|
|
431
|
+
* @returns A {@link Parser} that produces the transformed value of type U
|
|
432
|
+
* while preserving the original parser's state type and parsing behavior.
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
* ```typescript
|
|
436
|
+
* // Transform boolean flag to its inverse
|
|
437
|
+
* const parser = object({
|
|
438
|
+
* disallow: map(option("--allow"), b => !b)
|
|
439
|
+
* });
|
|
440
|
+
*
|
|
441
|
+
* // Transform string to uppercase
|
|
442
|
+
* const upperParser = map(argument(string()), s => s.toUpperCase());
|
|
443
|
+
*
|
|
444
|
+
* // Transform number to formatted string
|
|
445
|
+
* const prefixedParser = map(option("-n", integer()), n => `value: ${n}`);
|
|
446
|
+
* ```
|
|
447
|
+
*/
|
|
448
|
+
function map(parser, transform) {
|
|
449
|
+
return {
|
|
450
|
+
$valueType: [],
|
|
451
|
+
$stateType: parser.$stateType,
|
|
452
|
+
priority: parser.priority,
|
|
453
|
+
usage: parser.usage,
|
|
454
|
+
initialState: parser.initialState,
|
|
455
|
+
parse: parser.parse.bind(parser),
|
|
456
|
+
complete(state) {
|
|
457
|
+
const result = parser.complete(state);
|
|
458
|
+
if (result.success) return {
|
|
459
|
+
success: true,
|
|
460
|
+
value: transform(result.value)
|
|
461
|
+
};
|
|
462
|
+
return result;
|
|
463
|
+
},
|
|
464
|
+
getDocFragments(state, _defaultValue) {
|
|
465
|
+
return parser.getDocFragments(state, void 0);
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
416
470
|
* Creates a parser that allows multiple occurrences of a given parser.
|
|
417
471
|
* This parser can be used to parse multiple values of the same type,
|
|
418
472
|
* such as multiple command-line arguments or options.
|
|
@@ -1050,4 +1104,4 @@ function getDocPage(parser, args = []) {
|
|
|
1050
1104
|
}
|
|
1051
1105
|
|
|
1052
1106
|
//#endregion
|
|
1053
|
-
export { argument, command, constant, getDocPage, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
|
|
1107
|
+
export { argument, command, constant, getDocPage, map, merge, multiple, object, option, optional, or, parse, tuple, withDefault };
|