Skip to content

Commit 9df114f

Browse files
committed
replace some more charAt cases with bracket access
1 parent eee1dd3 commit 9df114f

File tree

5 files changed

+31
-33
lines changed

5 files changed

+31
-33
lines changed

packages/core-js/internals/get-substitution.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ var uncurryThis = require('../internals/function-uncurry-this');
33
var toObject = require('../internals/to-object');
44

55
var floor = Math.floor;
6-
var charAt = uncurryThis(''.charAt);
76
var replace = uncurryThis(''.replace);
87
var stringSlice = uncurryThis(''.slice);
98
// eslint-disable-next-line redos/no-vulnerable -- safe
@@ -22,7 +21,7 @@ module.exports = function (matched, str, position, captures, namedCaptures, repl
2221
}
2322
return replace(replacement, symbols, function (match, ch) {
2423
var capture;
25-
switch (charAt(ch, 0)) {
24+
switch (ch[0]) {
2625
case '$': return '$';
2726
case '&': return matched;
2827
case '`': return stringSlice(str, 0, position);
@@ -36,7 +35,7 @@ module.exports = function (matched, str, position, captures, namedCaptures, repl
3635
if (n > m) {
3736
var f = floor(n / 10);
3837
if (f === 0) return match;
39-
if (f <= m) return captures[f - 1] === undefined ? charAt(ch, 1) : captures[f - 1] + charAt(ch, 1);
38+
if (f <= m) return captures[f - 1] === undefined ? ch[1] : captures[f - 1] + ch[1];
4039
return match;
4140
}
4241
capture = captures[n - 1];

packages/core-js/modules/es.json.parse.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var isArray = Array.isArray;
1818
var nativeParse = JSON.parse;
1919
var enumerableOwnProperties = Object.keys;
2020
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
21-
var at = uncurryThis(''.charAt);
21+
var charAt = uncurryThis(''.charAt);
2222
var slice = uncurryThis(''.slice);
2323
var exec = uncurryThis(/./.exec);
2424
var push = uncurryThis([].push);
@@ -38,7 +38,7 @@ var $parse = function (source, reviver) {
3838
var value = root.value;
3939
var endIndex = context.skip(IS_WHITESPACE, root.end);
4040
if (endIndex < source.length) {
41-
throw new $SyntaxError('Unexpected extra character: "' + at(source, endIndex) + '" after the parsed data at: ' + endIndex);
41+
throw new $SyntaxError('Unexpected extra character: "' + source[endIndex] + '" after the parsed data at: ' + endIndex);
4242
}
4343
return isCallable(reviver) ? internalize({ '': value }, '', reviver, root) : value;
4444
};
@@ -97,7 +97,7 @@ Context.prototype = {
9797
var source = this.source;
9898
var i = this.skip(IS_WHITESPACE, this.index);
9999
var fork = this.fork(i);
100-
var char = at(source, i);
100+
var char = charAt(source, i);
101101
if (exec(IS_NUMBER_START, char)) return fork.number();
102102
switch (char) {
103103
case '{':
@@ -125,7 +125,7 @@ Context.prototype = {
125125
var nodes = {};
126126
while (i < source.length) {
127127
i = this.until(['"', '}'], i);
128-
if (at(source, i) === '}' && !expectKeypair) {
128+
if (charAt(source, i) === '}' && !expectKeypair) {
129129
i++;
130130
break;
131131
}
@@ -140,7 +140,7 @@ Context.prototype = {
140140
createProperty(nodes, key, result);
141141
createProperty(object, key, result.value);
142142
i = this.until([',', '}'], result.end);
143-
var char = at(source, i);
143+
var char = charAt(source, i);
144144
if (char === ',') {
145145
expectKeypair = true;
146146
i++;
@@ -159,18 +159,18 @@ Context.prototype = {
159159
var nodes = [];
160160
while (i < source.length) {
161161
i = this.skip(IS_WHITESPACE, i);
162-
if (at(source, i) === ']' && !expectElement) {
162+
if (charAt(source, i) === ']' && !expectElement) {
163163
i++;
164164
break;
165165
}
166166
var result = this.fork(i).parse();
167167
push(nodes, result);
168168
push(array, result.value);
169169
i = this.until([',', ']'], result.end);
170-
if (at(source, i) === ',') {
170+
if (charAt(source, i) === ',') {
171171
expectElement = true;
172172
i++;
173-
} else if (at(source, i) === ']') {
173+
} else if (charAt(source, i) === ']') {
174174
i++;
175175
break;
176176
}
@@ -186,14 +186,14 @@ Context.prototype = {
186186
var source = this.source;
187187
var startIndex = this.index;
188188
var i = startIndex;
189-
if (at(source, i) === '-') i++;
190-
if (at(source, i) === '0') i++;
191-
else if (exec(IS_NON_ZERO_DIGIT, at(source, i))) i = this.skip(IS_DIGIT, i + 1);
189+
if (charAt(source, i) === '-') i++;
190+
if (charAt(source, i) === '0') i++;
191+
else if (exec(IS_NON_ZERO_DIGIT, charAt(source, i))) i = this.skip(IS_DIGIT, i + 1);
192192
else throw new $SyntaxError('Failed to parse number at: ' + i);
193-
if (at(source, i) === '.') i = this.skip(IS_DIGIT, i + 1);
194-
if (at(source, i) === 'e' || at(source, i) === 'E') {
193+
if (charAt(source, i) === '.') i = this.skip(IS_DIGIT, i + 1);
194+
if (charAt(source, i) === 'e' || charAt(source, i) === 'E') {
195195
i++;
196-
if (at(source, i) === '+' || at(source, i) === '-') i++;
196+
if (charAt(source, i) === '+' || charAt(source, i) === '-') i++;
197197
var exponentStartIndex = i;
198198
i = this.skip(IS_DIGIT, i);
199199
if (exponentStartIndex === i) throw new $SyntaxError("Failed to parse number's exponent value at: " + i);
@@ -209,12 +209,12 @@ Context.prototype = {
209209
},
210210
skip: function (regex, i) {
211211
var source = this.source;
212-
for (; i < source.length; i++) if (!exec(regex, at(source, i))) break;
212+
for (; i < source.length; i++) if (!exec(regex, source[i])) break;
213213
return i;
214214
},
215215
until: function (array, i) {
216216
i = this.skip(IS_WHITESPACE, i);
217-
var char = at(this.source, i);
217+
var char = charAt(this.source, i);
218218
for (var j = 0; j < array.length; j++) if (array[j] === char) return i;
219219
throw new $SyntaxError('Unexpected character: "' + char + '" at: ' + i);
220220
},

packages/core-js/modules/es.regexp.constructor.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ var $SyntaxError = SyntaxError;
2626
var create = Object.create;
2727
var getOwnPropertyNames = Object.getOwnPropertyNames;
2828
var exec = uncurryThis(RegExpPrototype.exec);
29-
var charAt = uncurryThis(''.charAt);
3029
var replace = uncurryThis(''.replace);
3130
var stringIndexOf = uncurryThis(''.indexOf);
3231
var stringSlice = uncurryThis(''.slice);
@@ -54,10 +53,11 @@ var handleDotAll = function (string) {
5453
var result = '';
5554
var brackets = false;
5655
var char;
57-
for (; index <= length; index++) {
58-
char = charAt(string, index);
56+
for (; index < length; index++) {
57+
char = string[index];
5958
if (char === '\\') {
60-
result += char + charAt(string, ++index);
59+
result += char;
60+
if (++index < length) result += string[index];
6161
continue;
6262
}
6363
if (!brackets && char === '.') {
@@ -83,10 +83,10 @@ var handleNCG = function (string) {
8383
var groupid = 0;
8484
var groupname = '';
8585
var char;
86-
for (; index <= length; index++) {
87-
char = charAt(string, index);
88-
if (char === '\\') {
89-
char += charAt(string, ++index);
86+
for (; index < length; index++) {
87+
char = string[index];
88+
if (char === '\\' && index < length - 1) {
89+
char += string[++index];
9090
} else if (char === ']') {
9191
brackets = false;
9292
} else if (!brackets) switch (true) {

packages/core-js/modules/es.regexp.escape.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ var WHITESPACES = require('../internals/whitespaces');
88

99
var $Array = Array;
1010
var $escape = RegExp.escape;
11-
var charAt = uncurryThis(''.charAt);
1211
var charCodeAt = uncurryThis(''.charCodeAt);
1312
// dependency: es.string.pad-start
1413
var padStart = uncurryThis(getBuiltInPrototypeMethod('String', 'padStart'));
@@ -44,7 +43,7 @@ $({ target: 'RegExp', stat: true, forced: FORCED }, {
4443
var result = $Array(length);
4544

4645
for (var i = 0; i < length; i++) {
47-
var char = charAt(S, i);
46+
var char = S[i];
4847
if (i === 0 && exec(FIRST_DIGIT_OR_ASCII, char)) {
4948
result[i] = escapeChar(char);
5049
} else if (hasOwn(ControlEscape, char)) {
@@ -62,7 +61,7 @@ $({ target: 'RegExp', stat: true, forced: FORCED }, {
6261
// surrogate pair
6362
else {
6463
result[i] = char;
65-
result[++i] = charAt(S, i);
64+
result[++i] = S[i];
6665
}
6766
}
6867
}

packages/core-js/modules/web.url.constructor.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ var parseIPv4 = function (input) {
8181
part = parts[index];
8282
if (part === '') return input;
8383
radix = 10;
84-
if (part.length > 1 && charAt(part, 0) === '0') {
84+
if (part.length > 1 && part[0] === '0') {
8585
radix = exec(HEX_START, part) ? 16 : 8;
8686
part = stringSlice(part, radix === 8 ? 1 : 2);
8787
}
@@ -270,8 +270,8 @@ var specialSchemes = {
270270
// https://url.spec.whatwg.org/#windows-drive-letter
271271
var isWindowsDriveLetter = function (string, normalized) {
272272
var second;
273-
return string.length === 2 && exec(ALPHA, charAt(string, 0))
274-
&& ((second = charAt(string, 1)) === ':' || (!normalized && second === '|'));
273+
return string.length === 2 && exec(ALPHA, string[0])
274+
&& ((second = string[1]) === ':' || (!normalized && second === '|'));
275275
};
276276

277277
// https://url.spec.whatwg.org/#start-with-a-windows-drive-letter

0 commit comments

Comments
 (0)