Skip to content

Commit 714f272

Browse files
gurgundayaduh95
authored andcommitted
lib: remove startsWith/endsWith primordials for char checks
PR-URL: #55407 Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Mattias Buelens <[email protected]> Reviewed-By: Ethan Arrowood <[email protected]>
1 parent 4e5c90b commit 714f272

File tree

11 files changed

+17
-22
lines changed

11 files changed

+17
-22
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 & 4 deletions
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

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.length || 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 & 4 deletions
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

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: 3 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,8 @@ 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+
assert(i !== 0);
1577+
const start = integerString[0] === '-' ? 1 : 0;
15771578
for (; i >= start + 4; i -= 3) {
15781579
result = `_${StringPrototypeSlice(integerString, i - 3, i)}${result}`;
15791580
}

lib/repl.js

Lines changed: 1 addition & 1 deletion
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)