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 <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 4e5c90b commit 714f272

11 files changed

Lines changed: 17 additions & 22 deletions

File tree

‎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
constindex=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
constnextArg=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
asyncfunctionisLocalAddress(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
constexportTarget=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
functionisAccepted(to){
299-
if(!StringPrototypeStartsWith(to,'-')||to==='--')returntrue;
298+
if(!to.length||to[0]!=='-'||to==='--')returntrue;
300299
constrecursiveExpansion=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
letmainEntry;
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
constpath=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
functiongetInputPreview(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
functiongetFunctionBase(value,constructor,tag){
11921192
conststringified=FunctionPrototypeToString(value);
1193-
if(StringPrototypeStartsWith(stringified,'class')&&StringPrototypeEndsWith(stringified,'}')){
1193+
if(StringPrototypeStartsWith(stringified,'class')&&stringified[stringified.length-1]==='}'){
11941194
constslice=StringPrototypeSlice(stringified,5,-1);
11951195
constbracketIndex=StringPrototypeIndexOf(slice,'{');
11961196
if(bracketIndex!==-1&&
@@ -1573,7 +1573,8 @@ function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) {
15731573
functionaddNumericSeparator(integerString){
15741574
letresult='';
15751575
leti=integerString.length;
1576-
conststart=StringPrototypeStartsWith(integerString,'-') ? 1 : 0;
1576+
assert(i!==0);
1577+
conststart=integerString[0]==='-' ? 1 : 0;
15771578
for(;i>=start+4;i-=3){
15781579
result=`_${StringPrototypeSlice(integerString,i-3,i)}${result}`;
15791580
}

0 commit comments

Comments
 (0)