-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathchar-class.ts
75 lines (62 loc) · 2.19 KB
/
char-class.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import type { CharacterClass, CharacterEscape, EncodedRegex } from '../types';
import { ensureText } from '../utils';
export function charClass(...elements: Array<CharacterClass | CharacterEscape>): CharacterClass {
if (!elements.length) {
throw new Error('Expected at least one element');
}
return {
chars: elements.map((c) => c.chars).flat(),
ranges: elements.map((c) => c.ranges ?? []).flat(),
encode: encodeCharClass,
};
}
export function charRange(start: string, end: string): CharacterClass {
if (start.length !== 1 || end.length !== 1) {
throw new Error(`Expected single characters, but received "${start}" & "${end}"`);
}
if (start > end) {
[start, end] = [end, start];
}
return {
chars: [],
ranges: [{ start, end }],
encode: encodeCharClass,
};
}
export function anyOf(chars: string): CharacterClass {
ensureText(chars);
return {
chars: chars.split('').map(escapeChar),
encode: encodeCharClass,
};
}
export function negated(element: CharacterClass | CharacterEscape): EncodedRegex {
return encodeCharClass.call(element, true);
}
/**
* @deprecated Renamed to `negated`.
*/
export const inverted = negated;
/** Escape chars for usage inside char class */
function escapeChar(text: string): string {
return text.replace(/[\]\\]/g, '\\$&'); // $& means the whole matched string
}
function encodeCharClass(
this: CharacterClass | CharacterEscape,
isNegated?: boolean,
): EncodedRegex {
// If passed characters includes hyphen (`-`) it need to be moved to
// first (or last) place in order to treat it as hyphen character and not a range.
// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions/Character_classes#types
const hyphen = this.chars.includes('-') ? '-' : '';
const caret = this.chars.includes('^') ? '^' : '';
const otherChars = this.chars.filter((c) => c !== '-' && c !== '^').join('');
const ranges = this.ranges?.map(({ start, end }) => `${start}-${end}`).join('') ?? '';
const negation = isNegated ? '^' : '';
let pattern = `[${negation}${ranges}${otherChars}${caret}${hyphen}]`;
if (pattern === '[^-]') pattern = '[\\^-]';
return {
type: 'atom',
pattern,
};
}