@futpib/parser 1.0.6 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -96,6 +96,10 @@ export {
96
96
  createElementParser,
97
97
  } from './elementParser.js';
98
98
 
99
+ export {
100
+ createPredicateElementParser,
101
+ } from './predicateElementParser.js';
102
+
99
103
  export {
100
104
  createTerminatedArrayParser,
101
105
  } from './terminatedArrayParser.js';
@@ -0,0 +1,22 @@
1
+ import { setParserName, type Parser } from './parser.js';
2
+ import { type DeriveSequenceElement } from './sequence.js';
3
+
4
+ export const createPredicateElementParser = <Sequence, Element = DeriveSequenceElement<Sequence>>(
5
+ predicate: (element: Element) => boolean,
6
+ ): Parser<Element, Sequence, Element> => {
7
+ const predicateElementParser: Parser<Element, Sequence, Element> = async parserContext => {
8
+ const element = await parserContext.read(0);
9
+
10
+ parserContext.invariant(
11
+ predicate(element),
12
+ 'Element does not match predicate: %s',
13
+ element,
14
+ );
15
+
16
+ return element;
17
+ };
18
+
19
+ setParserName(predicateElementParser, `createPredicateElementParser(${predicate.name || 'anonymous'})`);
20
+
21
+ return predicateElementParser;
22
+ };