Skip to content

Commit

Permalink
feat(parse): add new transformers (json, numbers)
Browse files Browse the repository at this point in the history
- add xfJson(), json() transform
- add int(), hexInt(), float() transform syntax sugar
- add `json` as built-in tx for grammar
  • Loading branch information
postspectacular committed Jun 15, 2022
1 parent f0d5133 commit 2087131
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/parse/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@
"./xform/join": {
"default": "./xform/join.js"
},
"./xform/json": {
"default": "./xform/json.js"
},
"./xform/nest": {
"default": "./xform/nest.js"
},
Expand Down
2 changes: 2 additions & 0 deletions packages/parse/src/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { xfCount } from "./xform/count.js";
import { discard, xfDiscard } from "./xform/discard.js";
import { hoist, hoistResult, xfHoist, xfHoistResult } from "./xform/hoist.js";
import { join, xfJoin } from "./xform/join.js";
import { xfJson } from "./xform/json.js";
import { nest } from "./xform/nest.js";
import { xfFloat, xfInt } from "./xform/number.js";
import { print, xfPrint } from "./xform/print.js";
Expand Down Expand Up @@ -393,6 +394,7 @@ export const defGrammar = (
hoistR: xfHoistResult,
int: xfInt(10),
join: xfJoin,
json: xfJson,
print: xfPrint(),
trim: xfTrim,
...env,
Expand Down
1 change: 1 addition & 0 deletions packages/parse/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export * from "./xform/count.js";
export * from "./xform/discard.js";
export * from "./xform/hoist.js";
export * from "./xform/join.js";
export * from "./xform/json.js";
export * from "./xform/nest.js";
export * from "./xform/number.js";
export * from "./xform/print.js";
Expand Down
21 changes: 21 additions & 0 deletions packages/parse/src/xform/json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Parser, ScopeTransform } from "../api.js";
import { xform } from "../combinators/xform.js";
import { xfJoin } from "./join.js";

/**
* First joins all children via {@link xfJoin}, then parses resulting string
* using `JSON.parse()`.
*
* @param scope -
*/
export const xfJson: ScopeTransform<string> = (scope) => {
scope!.result = JSON.parse(xfJoin(scope)!.result);
return scope;
};

/**
* Syntax sugar for `xform(parser, xfJson)`.
*
* @param parser -
*/
export const json = (parser: Parser<string>) => xform(parser, xfJson);
28 changes: 25 additions & 3 deletions packages/parse/src/xform/number.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { ScopeTransform } from "../api.js";
import type { Parser, ScopeTransform } from "../api.js";
import { xform } from "../combinators/xform.js";
import { xfJoin } from "./join.js";

/**
* Higher order transform. First joins all children via {@link xfJoin},
* then parses resulting string into an integer with given `radix`.
*
* @param radix -
* @param radix -
*/
export const xfInt =
(radix = 10): ScopeTransform<string> =>
Expand All @@ -14,13 +15,34 @@ export const xfInt =
return scope;
};

/**
* Syntax sugar for `xform(parser, xfInt(10))`
*
* @param parser
*/
export const int = (parser: Parser<string>) => xform(parser, xfInt(10));

/**
* Syntax sugar for `xform(parser, xfInt(16))`
*
* @param parser
*/
export const hexInt = (parser: Parser<string>) => xform(parser, xfInt(16));

/**
* First joins all children via {@link xfJoin}, then parses resulting
* string into a floating point value.
*
* @param scope -
* @param scope -
*/
export const xfFloat: ScopeTransform<string> = (scope) => {
scope!.result = parseFloat(xfJoin(scope)!.result);
return scope;
};

/**
* Syntax sugar for `xform(parser, xfFloat)`
*
* @param parser
*/
export const float = (parser: Parser<string>) => xform(parser, xfFloat);

0 comments on commit 2087131

Please sign in to comment.