-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
243 lines (203 loc) · 6.31 KB
/
index.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Whole-script strict mode syntax
"use strict";
var JIT = require('./lib/jit');
var getStartNodeFromPattern = require('./lib/exec').getStartNodeFromPattern;
var exec = require('./lib/exec').exec;
var canonicalize = require('./lib/utils').canonicalize;
var BuildInRegExp = RegExp;
// See Section 9.3. Only losely following the spec here.
function ToNumber(value) {
if (value === undefined) {
return NaN;
} else if (value === null) {
return +0;
} else if (value === true) {
return 1;
} else if (value === false) {
return +0;
} else if (typeof value === 'number') {
return value;
} else if (typeof value === 'string') {
// This is not really what the spec says.
return parseFloat(value);
} else {
// This is not really what the spec says.
var r = parseFloat(value.valueOf());
if (isNaN(r)) {
return parseFloat(ToString(value));
}
return r;
}
}
function sign(x) {
return x >= 0 ? 1 : -1;
}
// See Section 9.4. Only losely following the spec here.
function ToInteger(value) {
var number = ToNumber(value);
if (isNaN(number)) {
return +0;
} else if (number === 0 || !isFinite(number)) {
return number;
} else {
return sign(number) * Math.floor(Math.abs(number));
}
}
// See Section 9.8. Only losely following the spec here.
function ToString(input) {
var t = input;
if (input === undefined) {
return 'undefined';
} else if (input === null) {
return 'null';
} else if (input === false) {
return 'false';
} else if (input === true) {
return 'true';
} else if (typeof input === 'number') {
return input + '';
} else if (typeof input === 'string') {
return input;
}
// If there is a toString property that is callback.
if (input.toString && input.toString.call) {
input = input.toString();
} else if (t.valueOf) {
input = t.valueOf();
}
return ToString(input);
}
var __DIRECT_RETURN__ = {};
function RegExpJS(pattern, flags) {
// Hack to make `RegExpJSReturn.prototype = new RegExpJS(RegExpJSReturn);`
// work :)
if (pattern === __DIRECT_RETURN__) {
return this;
}
// Calling RegExp('a') is valid and should return a new object.
if (this === undefined || this.__proto__ !== RegExpJS.prototype) {
return new RegExpJS(pattern, flags);
}
if (pattern === null) {
pattern = 'null';
} else if (!pattern) {
pattern = '(?:)';
}
// Don't recreate a RegExpJS object if the passed in pattern is already
// an RegExpJS object.
if (pattern instanceof RegExpJS) {
if (flags === undefined) {
return pattern;
} else {
throw new TypeError('Cannot supply flags when constructing one RegExp from another');
}
}
// if (flags !== undefined) {
// throw new Error('Flags are not supported yet');
// }
if (pattern instanceof BuildInRegExp) {
var str = ToString(pattern);
pattern = str.substring(1, str.lastIndexOf('/'));
flags = str.substring(str.lastIndexOf('/') + 1);
} else {
pattern = ToString(pattern);
}
// TOOD: What should happen if flags are passed via the pattern AND
// as second arguemtn?
function invalidFlags() {
// flag is invalid if it is is null
if (flags === null) return true;
flags = ToString(flags);
// flags is invalid if it is made up of any other character
// than g, i, m or consists any of these letters more than once.
return !/^(?:g|i|m)*$/.test(flags) ||
/(i[^i]*){2,}/.test(flags) ||
/(g[^g]*){2,}/.test(flags) ||
/(m[^m]*){2,}/.test(flags);
}
// Check if the passed in flags are valid.
if (flags !== undefined && invalidFlags()) {
throw new SyntaxError('Invalid flags supplied to RegExp constructor ' + flags);
} else {
flags = flags || '';
}
var self = this;
this.constructor = BuildInRegExp;
function freezeIt(prop, propValue) {
// The `source` property
Object.defineProperty(self, prop, {
writable: false,
enumerable: false,
configurable: false,
value: propValue
});
}
freezeIt('source', pattern);
freezeIt('global', flags.indexOf('g') !== -1);
freezeIt('ignoreCase', flags.indexOf('i') !== -1);
freezeIt('multiline', flags.indexOf('m') !== -1);
Object.defineProperty(this, 'lastIndex', {
writable: true,
enumerable: false,
configurable: false,
value: 0
});
this.$startNode = getStartNodeFromPattern(pattern, this.ignoreCase);
// Don't allow to overwrite the toString property on the object.
Object.defineProperty(this, 'toString', {
writable: false,
enumerable: false,
configurable: false,
value: function() { return '[object RegExp]'; }
});
}
RegExpJS.prototype = new RegExp();
RegExpJS.prototype.execDebug = function RegExpJSExec(str) {
// See: 15.10.6.2
var i = this.lastIndex;
if (this.global === false) {
i = 0;
} else {
i = ToInteger(i);
}
str = ToString(str);
if (i < 0 || i > str.length) {
this.lastIndex = 0;
// This makes the caller RegExpJS.prototype.exec
// return `null`.
return { matches: null };
}
var res = exec(str, this.$startNode, i, this.multiline, this.ignoreCase);
if (res.matches && this.global === true) {
this.lastIndex = res.idx;
}
return res;
};
RegExpJS.prototype.exec = function RegExpJSExec(str) {
// console.log('RegExpJS.prototype.exec', str)
var res = this.execDebug(str);
if (res.matches) {
return res.matches;
} else {
return null;
}
};
RegExpJS.prototype.test = function RegExpJSTest(str) {
return this.exec(str) !== null;
};
RegExpJS.prototype.exec.prototype = undefined;
RegExpJS.prototype.test.prototype = undefined;
if (typeof window !== 'undefined') {
window.RegExpJS = RegExpJS;
if (!window.require) {
window.require = require;
}
}
var proto = RegExpJS.prototype;
Object.defineProperty(RegExpJS, 'prototype', {
writable: false,
enumerable: false,
configurable: false,
value: proto
});
exports.RegExpJS = RegExpJS;