@optique/core 0.6.4 → 0.6.6

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.
@@ -23,18 +23,29 @@ function optional(parser) {
23
23
  }],
24
24
  initialState: void 0,
25
25
  parse(context) {
26
+ const innerState = typeof context.state === "undefined" ? parser.initialState : context.state[0];
26
27
  const result = parser.parse({
27
28
  ...context,
28
- state: typeof context.state === "undefined" ? parser.initialState : context.state[0]
29
+ state: innerState
29
30
  });
30
- if (result.success) return {
31
- success: true,
32
- next: {
33
- ...result.next,
34
- state: [result.next.state]
35
- },
36
- consumed: result.consumed
37
- };
31
+ if (result.success) {
32
+ if (result.next.state !== innerState || result.consumed.length === 0) return {
33
+ success: true,
34
+ next: {
35
+ ...result.next,
36
+ state: [result.next.state]
37
+ },
38
+ consumed: result.consumed
39
+ };
40
+ return {
41
+ success: true,
42
+ next: {
43
+ ...result.next,
44
+ state: context.state
45
+ },
46
+ consumed: result.consumed
47
+ };
48
+ }
38
49
  if (result.consumed === 0) return {
39
50
  success: true,
40
51
  next: context,
@@ -110,18 +121,29 @@ function withDefault(parser, defaultValue, options) {
110
121
  }],
111
122
  initialState: void 0,
112
123
  parse(context) {
124
+ const innerState = typeof context.state === "undefined" ? parser.initialState : context.state[0];
113
125
  const result = parser.parse({
114
126
  ...context,
115
- state: typeof context.state === "undefined" ? parser.initialState : context.state[0]
127
+ state: innerState
116
128
  });
117
- if (result.success) return {
118
- success: true,
119
- next: {
120
- ...result.next,
121
- state: [result.next.state]
122
- },
123
- consumed: result.consumed
124
- };
129
+ if (result.success) {
130
+ if (result.next.state !== innerState || result.consumed.length === 0) return {
131
+ success: true,
132
+ next: {
133
+ ...result.next,
134
+ state: [result.next.state]
135
+ },
136
+ consumed: result.consumed
137
+ };
138
+ return {
139
+ success: true,
140
+ next: {
141
+ ...result.next,
142
+ state: context.state
143
+ },
144
+ consumed: result.consumed
145
+ };
146
+ }
125
147
  if (result.consumed === 0) return {
126
148
  success: true,
127
149
  next: context,
@@ -81,7 +81,7 @@ declare class WithDefaultError extends Error {
81
81
  * or the default value if the wrapped parser fails to match
82
82
  * (union type {@link TValue} | {@link TDefault}).
83
83
  */
84
- declare function withDefault<TValue, TState, TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault)): Parser<TValue | TDefault, [TState] | undefined>;
84
+ declare function withDefault<TValue, TState, const TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault)): Parser<TValue | TDefault, [TState] | undefined>;
85
85
  /**
86
86
  * Creates a parser that makes another parser use a default value when it fails
87
87
  * to match or consume input. This is similar to {@link optional}, but instead
@@ -100,7 +100,7 @@ declare function withDefault<TValue, TState, TDefault = TValue>(parser: Parser<T
100
100
  * (union type {@link TValue} | {@link TDefault}).
101
101
  * @since 0.5.0
102
102
  */
103
- declare function withDefault<TValue, TState, TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault), options?: WithDefaultOptions): Parser<TValue | TDefault, [TState] | undefined>;
103
+ declare function withDefault<TValue, TState, const TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault), options?: WithDefaultOptions): Parser<TValue | TDefault, [TState] | undefined>;
104
104
  /**
105
105
  * Creates a parser that transforms the result value of another parser using
106
106
  * a mapping function. This enables value transformation while preserving
@@ -81,7 +81,7 @@ declare class WithDefaultError extends Error {
81
81
  * or the default value if the wrapped parser fails to match
82
82
  * (union type {@link TValue} | {@link TDefault}).
83
83
  */
84
- declare function withDefault<TValue, TState, TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault)): Parser<TValue | TDefault, [TState] | undefined>;
84
+ declare function withDefault<TValue, TState, const TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault)): Parser<TValue | TDefault, [TState] | undefined>;
85
85
  /**
86
86
  * Creates a parser that makes another parser use a default value when it fails
87
87
  * to match or consume input. This is similar to {@link optional}, but instead
@@ -100,7 +100,7 @@ declare function withDefault<TValue, TState, TDefault = TValue>(parser: Parser<T
100
100
  * (union type {@link TValue} | {@link TDefault}).
101
101
  * @since 0.5.0
102
102
  */
103
- declare function withDefault<TValue, TState, TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault), options?: WithDefaultOptions): Parser<TValue | TDefault, [TState] | undefined>;
103
+ declare function withDefault<TValue, TState, const TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault), options?: WithDefaultOptions): Parser<TValue | TDefault, [TState] | undefined>;
104
104
  /**
105
105
  * Creates a parser that transforms the result value of another parser using
106
106
  * a mapping function. This enables value transformation while preserving
package/dist/modifiers.js CHANGED
@@ -23,18 +23,29 @@ function optional(parser) {
23
23
  }],
24
24
  initialState: void 0,
25
25
  parse(context) {
26
+ const innerState = typeof context.state === "undefined" ? parser.initialState : context.state[0];
26
27
  const result = parser.parse({
27
28
  ...context,
28
- state: typeof context.state === "undefined" ? parser.initialState : context.state[0]
29
+ state: innerState
29
30
  });
30
- if (result.success) return {
31
- success: true,
32
- next: {
33
- ...result.next,
34
- state: [result.next.state]
35
- },
36
- consumed: result.consumed
37
- };
31
+ if (result.success) {
32
+ if (result.next.state !== innerState || result.consumed.length === 0) return {
33
+ success: true,
34
+ next: {
35
+ ...result.next,
36
+ state: [result.next.state]
37
+ },
38
+ consumed: result.consumed
39
+ };
40
+ return {
41
+ success: true,
42
+ next: {
43
+ ...result.next,
44
+ state: context.state
45
+ },
46
+ consumed: result.consumed
47
+ };
48
+ }
38
49
  if (result.consumed === 0) return {
39
50
  success: true,
40
51
  next: context,
@@ -110,18 +121,29 @@ function withDefault(parser, defaultValue, options) {
110
121
  }],
111
122
  initialState: void 0,
112
123
  parse(context) {
124
+ const innerState = typeof context.state === "undefined" ? parser.initialState : context.state[0];
113
125
  const result = parser.parse({
114
126
  ...context,
115
- state: typeof context.state === "undefined" ? parser.initialState : context.state[0]
127
+ state: innerState
116
128
  });
117
- if (result.success) return {
118
- success: true,
119
- next: {
120
- ...result.next,
121
- state: [result.next.state]
122
- },
123
- consumed: result.consumed
124
- };
129
+ if (result.success) {
130
+ if (result.next.state !== innerState || result.consumed.length === 0) return {
131
+ success: true,
132
+ next: {
133
+ ...result.next,
134
+ state: [result.next.state]
135
+ },
136
+ consumed: result.consumed
137
+ };
138
+ return {
139
+ success: true,
140
+ next: {
141
+ ...result.next,
142
+ state: context.state
143
+ },
144
+ consumed: result.consumed
145
+ };
146
+ }
125
147
  if (result.consumed === 0) return {
126
148
  success: true,
127
149
  next: context,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "0.6.4",
3
+ "version": "0.6.6",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",