Skip to content

Commit 74c9ce0

Browse files
authored
Merge pull request #9 from mkantor/narrower-arrays-returns
Return narrower non-readonly arrays from parsers
2 parents 4cddec0 + 1b80bbf commit 74c9ce0

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/combinators.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ type OneOfOutput<Parsers extends readonly Parser<unknown>[]> = {
146146
*/
147147
export const oneOrMore = <Output>(
148148
parser: Parser<Output>,
149-
): Parser<readonly [Output, ...(readonly Output[])]> =>
149+
): Parser<[Output, ...(readonly Output[])]> =>
150150
map(sequence([parser, zeroOrMore(parser)]), ([head, tail]) => [head, ...tail])
151151

152152
/**
@@ -181,17 +181,15 @@ export const sequence =
181181
return parseResult as ParserResult<SequenceOutput<Parsers>>
182182
}
183183
type SequenceOutput<Parsers extends readonly Parser<unknown>[]> = {
184-
[Index in keyof Parsers]: OutputOf<Parsers[Index]>
184+
-readonly [Index in keyof Parsers]: OutputOf<Parsers[Index]>
185185
}
186186

187187
/**
188188
* Repeatedly apply `parser` to the input as long as it keeps succeeding.
189189
* Outputs are collected in an array.
190190
*/
191191
export const zeroOrMore =
192-
<Output>(
193-
parser: Parser<Output>,
194-
): ParserWhichAlwaysSucceeds<readonly Output[]> =>
192+
<Output>(parser: Parser<Output>): ParserWhichAlwaysSucceeds<Output[]> =>
195193
// Uses a loop rather than recursion to avoid stack overflow.
196194
input => {
197195
const output: Output[] = []

src/parsing.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,10 @@ test('README example', _ => {
213213
const longInputLength = 10000
214214
const longInput = 'a'.repeat(longInputLength)
215215
const longInputElementParser = literal('a')
216-
// Written oddly to prove non-emptiness.
217-
const longExpectedOutput = [
218-
'a',
219-
...Array.from({ length: longInputLength - 1 }, _ => 'a' as const),
220-
] as const
216+
const longExpectedOutput = Array.from(
217+
{ length: longInputLength },
218+
_ => 'a' as const,
219+
)
221220

222221
const adjustStartStackFn = (
223222
error: AssertionError,

0 commit comments

Comments
 (0)