-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquantifiers.ts
49 lines (45 loc) · 1.52 KB
/
quantifiers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { encodeAtomic } from '../encoder';
import type { EncodedRegex, RegexSequence } from '../types';
import { ensureElements } from '../utils';
export interface QuantifierOptions {
greedy?: boolean;
}
/**
* Creates a quantifier which matches zero or more of the given elements.
*
* @param sequence - Elements to match zero or more of.
* @param options - Quantifier options.
*/
export function zeroOrMore(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
const elements = ensureElements(sequence);
return {
precedence: 'sequence',
pattern: `${encodeAtomic(elements)}*${options?.greedy === false ? '?' : ''}`,
};
}
/**
* Creates a quantifier which matches one or more of the given elements.
*
* @param sequence - Elements to match one or more of.
* @param options - Quantifier options.
*/
export function oneOrMore(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
const elements = ensureElements(sequence);
return {
precedence: 'sequence',
pattern: `${encodeAtomic(elements)}+${options?.greedy === false ? '?' : ''}`,
};
}
/**
* Creates a quantifier which matches zero or one of the given elements.
*
* @param sequence - Elements to match zero or one of.
* @param options - Quantifier options.
*/
export function optional(sequence: RegexSequence, options?: QuantifierOptions): EncodedRegex {
const elements = ensureElements(sequence);
return {
precedence: 'sequence',
pattern: `${encodeAtomic(elements)}?${options?.greedy === false ? '?' : ''}`,
};
}