From 7efa7c7f2716c4e62041727e8960fc48a09fd60c Mon Sep 17 00:00:00 2001 From: Matt Kantor Date: Sun, 27 Apr 2025 09:22:00 -0400 Subject: [PATCH] Remove `transformOutput` combinator The fact that `transformOutput` required an `InvalidInputError` to be explicitly created in order to model failure made this function difficult to use (you had to keep track of your own `input`, etc). I considered changing the signature to instead only require an error message, but ultimately decided to ditch it entirely for now. Either change is semver-major and I don't currently use `transformOutput` at all (and I believe I'm still the sole consumer of this package). --- src/combinators.ts | 18 +----------------- src/parsing.test.ts | 16 ---------------- 2 files changed, 1 insertion(+), 33 deletions(-) diff --git a/src/combinators.ts b/src/combinators.ts index 0ff2967..7d622d4 100644 --- a/src/combinators.ts +++ b/src/combinators.ts @@ -1,8 +1,7 @@ -import type { Either, Right } from '@matt.kantor/either' +import type { Right } from '@matt.kantor/either' import * as either from '@matt.kantor/either' import { nothing } from './constructors.js' import type { - InvalidInputError, Parser, ParserResult, ParserWhichAlwaysSucceeds, @@ -187,21 +186,6 @@ type SequenceOutput[]> = { [Index in keyof Parsers]: OutputOf } -/** - * Refine/transform the output of `parser` via a function which may fail. - */ -export const transformOutput = ( - parser: Parser, - f: (output: Output) => Either, -): Parser => { - const transformation = (success: Success) => - either.map(f(success.output), output => ({ - output, - remainingInput: success.remainingInput, - })) - return input => either.flatMap(parser(input), transformation) -} - /** * Repeatedly apply `parser` to the input as long as it keeps succeeding. * Outputs are collected in an array. diff --git a/src/parsing.test.ts b/src/parsing.test.ts index 9492b2e..740528b 100644 --- a/src/parsing.test.ts +++ b/src/parsing.test.ts @@ -11,7 +11,6 @@ import { oneOf, oneOrMore, sequence, - transformOutput, zeroOrMore, } from './combinators.js' import { @@ -131,21 +130,6 @@ suite('combinators', _ => { assertFailure(ab('bab')) }) - test('transformOutput', _ => { - const aTransformedToUppercase = transformOutput(literal('a'), a => - either.makeRight(a.toUpperCase()), - ) - assertSuccess(aTransformedToUppercase('a'), 'A') - assertSuccess(aTransformedToUppercase('ab'), 'A') - assertFailure(aTransformedToUppercase('b')) - assertFailure(aTransformedToUppercase('')) - assertFailure( - transformOutput(anySingleCharacter, _ => - either.makeLeft({ kind: 'invalidInput', input: '', message: '' }), - )(''), - ) - }) - test('zeroOrMore', _ => { const zeroOrMoreA = zeroOrMore(literal('a')) assertSuccess(zeroOrMoreA('a'), ['a'])