-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathregexp.ts
More file actions
130 lines (117 loc) · 3.67 KB
/
regexp.ts
File metadata and controls
130 lines (117 loc) · 3.67 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { BSONValue } from './bson_value';
import { BSONError } from './error';
import type { EJSONOptions } from './extended_json';
import { defaultInspect } from './parser/utils';
function alphabetize(str: string): string {
return str.split('').sort().join('');
}
/** @public */
export interface BSONRegExpExtendedLegacy {
$regex: string | BSONRegExp;
$options: string;
}
/** @public */
export interface BSONRegExpExtended {
$regularExpression: {
pattern: string;
options: string;
};
}
/**
* A class representation of the BSON RegExp type.
* @public
* @category BSONType
*/
export class BSONRegExp extends BSONValue {
get _bsontype(): 'BSONRegExp' {
return 'BSONRegExp';
}
pattern!: string;
options!: string;
/**
* @param pattern - The regular expression pattern to match
* @param options - The regular expression options
*/
constructor(pattern: string, options?: string) {
super();
this.pattern = pattern;
this.options = alphabetize(options ?? '');
if (this.pattern.indexOf('\x00') !== -1) {
throw new BSONError(
`BSON Regex patterns cannot contain null bytes, found: ${JSON.stringify(this.pattern)}`
);
}
if (this.options.indexOf('\x00') !== -1) {
throw new BSONError(
`BSON Regex options cannot contain null bytes, found: ${JSON.stringify(this.options)}`
);
}
// Validate options
for (let i = 0; i < this.options.length; i++) {
if (
!(
this.options[i] === 'i' ||
this.options[i] === 'm' ||
this.options[i] === 'x' ||
this.options[i] === 'l' ||
this.options[i] === 's' ||
this.options[i] === 'u'
)
) {
throw new BSONError(`The regular expression option [${this.options[i]}] is not supported`);
}
}
}
static parseOptions(options?: string): string {
return options ? options.split('').sort().join('') : '';
}
/** @internal */
toExtendedJSON(options?: EJSONOptions): BSONRegExpExtendedLegacy | BSONRegExpExtended {
options = options || {};
if (options.legacy) {
return { $regex: this.pattern, $options: this.options };
}
return { $regularExpression: { pattern: this.pattern, options: this.options } };
}
/** @internal */
static fromExtendedJSON(doc: BSONRegExpExtendedLegacy | BSONRegExpExtended): BSONRegExp {
if ('$regex' in doc) {
if (typeof doc.$regex !== 'string') {
// This is for $regex query operators that have extended json values.
if (doc.$regex._bsontype === 'BSONRegExp') {
return doc as unknown as BSONRegExp;
}
} else {
return new BSONRegExp(doc.$regex, BSONRegExp.parseOptions(doc.$options));
}
}
if ('$regularExpression' in doc) {
return new BSONRegExp(
doc.$regularExpression.pattern,
BSONRegExp.parseOptions(doc.$regularExpression.options)
);
}
throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
}
inspect(depth?: number, options?: unknown): string {
const stylize = getStylizeFunction(options);
const pattern = stylize(`'${this.pattern}'`, 'regexp');
const flags = stylize(`'${this.options}'`, 'regexp');
return `new BSONRegExp(${pattern}, ${flags})`;
}
}
/** @internal */
type StylizeFunction = (x: string, style: string) => string;
/** @internal */
function getStylizeFunction(options?: unknown): StylizeFunction {
const stylizeExists =
options != null &&
typeof options === 'object' &&
'stylize' in options &&
typeof options.stylize === 'function';
if (stylizeExists) {
return options.stylize as StylizeFunction;
} else {
return defaultInspect;
}
}