-
-
Notifications
You must be signed in to change notification settings - Fork 150
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 count/xfCount transform
- Loading branch information
1 parent
456efdc
commit 056ae08
Showing
3 changed files
with
26 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { Parser, ScopeTransform } from "../api"; | ||
import { xform } from "../combinators/xform"; | ||
|
||
/** | ||
* Stores number of children as result, then discards children. Also see | ||
* {@link count}. | ||
* | ||
* @param scope | ||
*/ | ||
export const xfCount: ScopeTransform<any> = (scope) => { | ||
scope!.result = scope!.children ? scope!.children.length : 0; | ||
scope!.children = null; | ||
return scope; | ||
}; | ||
|
||
/** | ||
* Syntax sugar for `xform(parser, xfCount)`. | ||
* | ||
* @param parser | ||
*/ | ||
export const count = <T>(parser: Parser<T>) => xform(parser, xfCount); |