import { Combinated } from "../combinated"; import type { ParjsCombinator, ParjsProjection } from "../parjser"; import type { ParjserBase } from "../parser"; import type { ParsingState } from "../state"; import { wrapImplicit } from "../wrap-implicit"; class Map extends Combinated { type = "map"; expecting = this.source.expecting; constructor( source: ParjserBase, private readonly _projection: ParjsProjection ) { super(source); } _apply(ps: ParsingState): void { this.source.apply(ps); if (!ps.isOk) { return; } ps.value = this._projection(ps.value as T, ps.userState); } } /** * Applies the source parser and projects its result with `projection`. * * @param projection The projection to apply. */ export function map(projection: ParjsProjection): ParjsCombinator { return source => new Map(wrapImplicit(source), projection); } /** * Applies the source parser and yields the constant value `result`. * * @param result The constant value to yield. */ export function mapConst(result: T): ParjsCombinator { return map(() => result); }