Skip to content

Commit 753cbed

Browse files
gurgundayruyadorno
authored andcommitted
lib: remove startsWith/endsWith primordials for char checks
PR-URL: #55407 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mattias Buelens <mattias@buelens.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
1 parent 6e3e99c commit 753cbed

File tree

11 files changed

+17
-22
lines changed

11 files changed

+17
-22
lines changed

lib/_http_agent.js

+1-1
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

+1-1
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

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ const {
33
ObjectPrototypeHasOwnProperty,
44
PromisePrototypeThen,
55
SafeMap,
6-
StringPrototypeEndsWith,
76
StringPrototypeSlice,
8-
StringPrototypeStartsWith,
97
} = primordials;
108
const {
119
Buffer: { concat: BufferConcat },
@@ -248,8 +246,9 @@ allowList.addRange('127.0.0.1', '127.255.255.255');
248246
async function isLocalAddress(hostname) {
249247
try {
250248
if (
251-
StringPrototypeStartsWith(hostname, '[') &&
252-
StringPrototypeEndsWith(hostname, ']')
249+
hostname.length &&
250+
hostname[0] === '[' &&
251+
hostname[hostname.length - 1] === ']'
253252
) {
254253
hostname = StringPrototypeSlice(hostname, 1, -1);
255254
}

lib/internal/modules/esm/resolve.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,9 @@ function resolvePackageTargetString(
391391
}
392392

393393
if (!StringPrototypeStartsWith(target, './')) {
394-
if (internal && !StringPrototypeStartsWith(target, '../') &&
395-
!StringPrototypeStartsWith(target, '/')) {
394+
if (internal &&
395+
target[0] !== '/' &&
396+
!StringPrototypeStartsWith(target, '../')) {
396397
// No need to convert target to string, since it's already presumed to be
397398
if (!URLCanParse(target)) {
398399
const exportTarget = pattern ?

lib/internal/modules/helpers.js

+1-1
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

+1-2
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;
@@ -300,7 +299,7 @@ function buildAllowedFlags() {
300299
}
301300

302301
function isAccepted(to) {
303-
if (!StringPrototypeStartsWith(to, '-') || to === '--') return true;
302+
if (!to.length || to[0] !== '-' || to === '--') return true;
304303
const recursiveExpansion = aliases.get(to);
305304
if (recursiveExpansion) {
306305
if (recursiveExpansion[0] === to)

lib/internal/process/pre_execution.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const {
1414
ObjectFreeze,
1515
ObjectGetOwnPropertyDescriptor,
1616
String,
17-
StringPrototypeStartsWith,
1817
Symbol,
1918
globalThis,
2019
} = primordials;
@@ -242,8 +241,7 @@ function patchProcessObject(expandArgv1) {
242241
let mainEntry;
243242
// If requested, update process.argv[1] to replace whatever the user provided with the resolved absolute file path of
244243
// the entry point.
245-
if (expandArgv1 && process.argv[1] &&
246-
!StringPrototypeStartsWith(process.argv[1], '-')) {
244+
if (expandArgv1 && process.argv[1] && process.argv[1][0] !== '-') {
247245
// Expand process.argv[1] into a full path.
248246
const path = require('path');
249247
try {

lib/internal/repl/utils.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ const {
1010
RegExpPrototypeExec,
1111
SafeSet,
1212
SafeStringIterator,
13-
StringPrototypeEndsWith,
1413
StringPrototypeIndexOf,
1514
StringPrototypeLastIndexOf,
1615
StringPrototypeReplaceAll,
1716
StringPrototypeSlice,
18-
StringPrototypeStartsWith,
1917
StringPrototypeToLowerCase,
2018
StringPrototypeTrim,
2119
Symbol,
@@ -298,8 +296,7 @@ function setupPreview(repl, contextSymbol, bufferSymbol, active) {
298296
function getInputPreview(input, callback) {
299297
// For similar reasons as `defaultEval`, wrap expressions starting with a
300298
// curly brace with parenthesis.
301-
if (StringPrototypeStartsWith(input, '{') &&
302-
!StringPrototypeEndsWith(input, ';') && !wrapped) {
299+
if (!wrapped && input[0] === '{' && input[input.length - 1] !== ';') {
303300
input = `(${input})`;
304301
wrapped = true;
305302
}

lib/internal/url.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ function urlToHttpOptions(url) {
14281428
__proto__: null,
14291429
...url, // In case the url object was extended by the user.
14301430
protocol: url.protocol,
1431-
hostname: hostname && StringPrototypeStartsWith(hostname, '[') ?
1431+
hostname: hostname && hostname[0] === '[' ?
14321432
StringPrototypeSlice(hostname, 1, -1) :
14331433
hostname,
14341434
hash: url.hash,

lib/internal/util/inspect.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,7 @@ function getClassBase(value, constructor, tag) {
11911191

11921192
function getFunctionBase(value, constructor, tag) {
11931193
const stringified = FunctionPrototypeToString(value);
1194-
if (StringPrototypeStartsWith(stringified, 'class') && StringPrototypeEndsWith(stringified, '}')) {
1194+
if (StringPrototypeStartsWith(stringified, 'class') && stringified[stringified.length - 1] === '}') {
11951195
const slice = StringPrototypeSlice(stringified, 5, -1);
11961196
const bracketIndex = StringPrototypeIndexOf(slice, '{');
11971197
if (bracketIndex !== -1 &&
@@ -1570,7 +1570,8 @@ function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) {
15701570
function addNumericSeparator(integerString) {
15711571
let result = '';
15721572
let i = integerString.length;
1573-
const start = StringPrototypeStartsWith(integerString, '-') ? 1 : 0;
1573+
assert(i !== 0);
1574+
const start = integerString[0] === '-' ? 1 : 0;
15741575
for (; i >= start + 4; i -= 3) {
15751576
result = `_${StringPrototypeSlice(integerString, i - 3, i)}${result}`;
15761577
}

lib/repl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ const { shouldColorize } = require('internal/util/colors');
130130
const CJSModule = require('internal/modules/cjs/loader').Module;
131131
let _builtinLibs = ArrayPrototypeFilter(
132132
CJSModule.builtinModules,
133-
(e) => !StringPrototypeStartsWith(e, '_'),
133+
(e) => e[0] !== '_',
134134
);
135135
const nodeSchemeBuiltinLibs = ArrayPrototypeMap(
136136
_builtinLibs, (lib) => `node:${lib}`);

0 commit comments

Comments
 (0)