Skip to content

Commit 3706c83

Browse files
committed
lib: remove startsWith/endsWith primordials for char checks
1 parent fa8f149 commit 3706c83

File tree

12 files changed

+19
-23
lines changed

12 files changed

+19
-23
lines changed

lib/_http_agent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ function calculateServerName(options, req) {
341341
// abc:123 => abc
342342
// [::1] => ::1
343343
// [::1]:123 => ::1
344-
if (hostHeader.startsWith('[')) {
344+
if (hostHeader[0] === '[') {
345345
const index = hostHeader.indexOf(']');
346346
if (index === -1) {
347347
// Leading '[', but no ']'. Need to do something...

lib/internal/main/watch_mode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ for (let i = 0; i < process.execArgv.length; i++) {
4646
if (StringPrototypeStartsWith(arg, '--watch')) {
4747
i++;
4848
const nextArg = process.execArgv[i];
49-
if (nextArg && StringPrototypeStartsWith(nextArg, '-')) {
49+
if (nextArg && nextArg[0] === '-') {
5050
ArrayPrototypePush(argsWithoutWatchOptions, nextArg);
5151
}
5252
continue;

lib/internal/modules/esm/fetch_module.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const {
55
SafeMap,
66
StringPrototypeEndsWith,
77
StringPrototypeSlice,
8-
StringPrototypeStartsWith,
98
} = primordials;
109
const {
1110
Buffer: { concat: BufferConcat },
@@ -248,8 +247,9 @@ allowList.addRange('127.0.0.1', '127.255.255.255');
248247
async function isLocalAddress(hostname) {
249248
try {
250249
if (
251-
StringPrototypeStartsWith(hostname, '[') &&
252-
StringPrototypeEndsWith(hostname, ']')
250+
hostname &&
251+
hostname[0] === '[' &&
252+
hostname[hostname.length - 1] === ']'
253253
) {
254254
hostname = StringPrototypeSlice(hostname, 1, -1);
255255
}

lib/internal/modules/esm/resolve.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,9 @@ function resolvePackageTargetString(
373373
}
374374

375375
if (!StringPrototypeStartsWith(target, './')) {
376-
if (internal && !StringPrototypeStartsWith(target, '../') &&
377-
!StringPrototypeStartsWith(target, '/')) {
376+
if (internal &&
377+
target[0] !== '/' &&
378+
!StringPrototypeStartsWith(target, '../')) {
378379
// No need to convert target to string, since it's already presumed to be
379380
if (!URLCanParse(target)) {
380381
const exportTarget = pattern ?

lib/internal/modules/helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ function addBuiltinLibsToObject(object, dummyModuleName) {
202202
ArrayPrototypeForEach(builtinModules, (name) => {
203203
// Neither add underscored modules, nor ones that contain slashes (e.g.,
204204
// 'fs/promises') or ones that are already defined.
205-
if (StringPrototypeStartsWith(name, '_') ||
205+
if (name[0] === '_' ||
206206
StringPrototypeIncludes(name, '/') ||
207207
ObjectPrototypeHasOwnProperty(object, name)) {
208208
return;

lib/internal/process/per_thread.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const {
2525
StringPrototypeEndsWith,
2626
StringPrototypeReplace,
2727
StringPrototypeSlice,
28-
StringPrototypeStartsWith,
2928
Symbol,
3029
SymbolIterator,
3130
} = primordials;
@@ -296,7 +295,7 @@ function buildAllowedFlags() {
296295
}
297296

298297
function isAccepted(to) {
299-
if (!StringPrototypeStartsWith(to, '-') || to === '--') return true;
298+
if (!to || to[0] !== '-' || to === '--') return true;
300299
const recursiveExpansion = aliases.get(to);
301300
if (recursiveExpansion) {
302301
if (recursiveExpansion[0] === to)

lib/internal/process/pre_execution.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const {
1313
ObjectDefineProperty,
1414
ObjectFreeze,
1515
String,
16-
StringPrototypeStartsWith,
1716
globalThis,
1817
} = primordials;
1918

@@ -206,8 +205,7 @@ function patchProcessObject(expandArgv1) {
206205
let mainEntry;
207206
// If requested, update process.argv[1] to replace whatever the user provided with the resolved absolute file path of
208207
// the entry point.
209-
if (expandArgv1 && process.argv[1] &&
210-
!StringPrototypeStartsWith(process.argv[1], '-')) {
208+
if (expandArgv1 && process.argv[1] && process.argv[1][0] !== '-') {
211209
// Expand process.argv[1] into a full path.
212210
const path = require('path');
213211
try {

lib/internal/repl/utils.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const {
1515
StringPrototypeLastIndexOf,
1616
StringPrototypeReplaceAll,
1717
StringPrototypeSlice,
18-
StringPrototypeStartsWith,
1918
StringPrototypeToLowerCase,
2019
StringPrototypeTrim,
2120
Symbol,
@@ -298,8 +297,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
298297
function getInputPreview(input, callback) {
299298
// For similar reasons as `defaultEval`, wrap expressions starting with a
300299
// curly brace with parenthesis.
301-
if (StringPrototypeStartsWith(input, '{') &&
302-
!StringPrototypeEndsWith(input, ';') && !wrapped) {
300+
if (!wrapped && input[0] === '{' && input[input.length - 1] !== ';') {
303301
input = `(${input})`;
304302
wrapped = true;
305303
}

lib/internal/url.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ function urlToHttpOptions(url) {
14161416
__proto__: null,
14171417
...url, // In case the url object was extended by the user.
14181418
protocol: url.protocol,
1419-
hostname: hostname && StringPrototypeStartsWith(hostname, '[') ?
1419+
hostname: hostname && hostname[0] === '[' ?
14201420
StringPrototypeSlice(hostname, 1, -1) :
14211421
hostname,
14221422
hash: url.hash,

lib/internal/util/inspect.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ function getClassBase(value, constructor, tag) {
11901190

11911191
function getFunctionBase(value, constructor, tag) {
11921192
const stringified = FunctionPrototypeToString(value);
1193-
if (StringPrototypeStartsWith(stringified, 'class') && StringPrototypeEndsWith(stringified, '}')) {
1193+
if (StringPrototypeStartsWith(stringified, 'class') && stringified[stringified.length - 1] === '}') {
11941194
const slice = StringPrototypeSlice(stringified, 5, -1);
11951195
const bracketIndex = StringPrototypeIndexOf(slice, '{');
11961196
if (bracketIndex !== -1 &&
@@ -1573,7 +1573,7 @@ function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) {
15731573
function addNumericSeparator(integerString) {
15741574
let result = '';
15751575
let i = integerString.length;
1576-
const start = StringPrototypeStartsWith(integerString, '-') ? 1 : 0;
1576+
const start = integerString[0] === '-' ? 1 : 0;
15771577
for (; i >= start + 4; i -= 3) {
15781578
result = `_${StringPrototypeSlice(integerString, i - 3, i)}${result}`;
15791579
}

0 commit comments

Comments
 (0)