Skip to content

Commit

Permalink
feat(parse): add replace/xfReplace() xform
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Aug 17, 2020
1 parent 5e994d0 commit 7291181
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/parse/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ export * from "./xform/join";
export * from "./xform/nest";
export * from "./xform/number";
export * from "./xform/print";
export * from "./xform/replace";
export * from "./xform/trim";
export * from "./xform/with-id";
23 changes: 23 additions & 0 deletions packages/parse/src/xform/replace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { Parser, ScopeTransform } from "../api";
import { xform } from "../combinators/xform";

/**
* HOF scope transform which replaces a node's result with given pre-configured
* value and discards node's children. Also see {@link replace}.
*
* @param result - replacement value
*/
export const xfReplace = <T>(result: any): ScopeTransform<T> => (scope) => {
scope!.result = result;
scope!.children = null;
return scope;
};

/**
* Syntax sugar for `xform(parser, xfReplace(result))`.
*
* @param parser -
* @param result -
*/
export const replace = <T>(parser: Parser<T>, result: any) =>
xform(parser, xfReplace(result));

0 comments on commit 7291181

Please sign in to comment.