-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parse): add new transformers (json, numbers)
- add xfJson(), json() transform - add int(), hexInt(), float() transform syntax sugar - add `json` as built-in tx for grammar
- Loading branch information
1 parent
f0d5133
commit 2087131
Showing
5 changed files
with
52 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters