Skip to content

Commit b885409

Browse files
aduh95danielleadams
authored andcommitted
readline: refactor to use more primordials
PR-URL: #36296 Reviewed-By: Rich Trott <[email protected]>
1 parent 8d69d83 commit b885409

File tree

2 files changed

+99
-57
lines changed

2 files changed

+99
-57
lines changed

‎lib/internal/readline/utils.js‎

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
'use strict';
22

33
const{
4+
ArrayPrototypeSlice,
5+
ArrayPrototypeSort,
6+
RegExpPrototypeTest,
47
StringFromCharCode,
8+
StringPrototypeCharCodeAt,
9+
StringPrototypeCodePointAt,
10+
StringPrototypeMatch,
11+
StringPrototypeSlice,
12+
StringPrototypeToLowerCase,
513
Symbol,
614
}=primordials;
715

@@ -32,8 +40,9 @@ CSI.kClearScreenDown = CSI`0J`;
3240
functioncharLengthLeft(str,i){
3341
if(i<=0)
3442
return0;
35-
if((i>1&&str.codePointAt(i-2)>=kUTF16SurrogateThreshold)||
36-
str.codePointAt(i-1)>=kUTF16SurrogateThreshold){
43+
if((i>1&&
44+
StringPrototypeCodePointAt(str,i-2)>=kUTF16SurrogateThreshold)||
45+
StringPrototypeCodePointAt(str,i-1)>=kUTF16SurrogateThreshold){
3746
return2;
3847
}
3948
return1;
@@ -45,7 +54,7 @@ function charLengthAt(str, i){
4554
// moving to the right.
4655
return1;
4756
}
48-
returnstr.codePointAt(i)>=kUTF16SurrogateThreshold ? 2 : 1;
57+
returnStringPrototypeCodePointAt(str,i)>=kUTF16SurrogateThreshold ? 2 : 1;
4958
}
5059

5160
/*
@@ -178,13 +187,15 @@ function* emitKeys(stream){
178187
* We buffered enough data, now trying to extract code
179188
* and modifier from it
180189
*/
181-
constcmd=s.slice(cmdStart);
190+
constcmd=StringPrototypeSlice(s,cmdStart);
182191
letmatch;
183192

184-
if((match=cmd.match(/^(\d\d?)(;(\d))?([~^$])$/))){
193+
if((match=StringPrototypeMatch(cmd,/^(\d\d?)(;(\d))?([~^$])$/))){
185194
code+=match[1]+match[4];
186195
modifier=(match[3]||1)-1;
187-
}elseif((match=cmd.match(/^((\d;)?(\d))?([A-Za-z])$/))){
196+
}elseif(
197+
(match=StringPrototypeMatch(cmd,/^((\d;)?(\d))?([A-Za-z])$/))
198+
){
188199
code+=match[4];
189200
modifier=(match[3]||1)-1;
190201
}else{
@@ -325,12 +336,14 @@ function* emitKeys(stream){
325336
key.meta=escaped;
326337
}elseif(!escaped&&ch<='\x1a'){
327338
// ctrl+letter
328-
key.name=StringFromCharCode(ch.charCodeAt(0)+'a'.charCodeAt(0)-1);
339+
key.name=StringFromCharCode(
340+
StringPrototypeCharCodeAt(ch)+StringPrototypeCharCodeAt('a')-1
341+
);
329342
key.ctrl=true;
330-
}elseif(/^[0-9A-Za-z]$/.test(ch)){
343+
}elseif(RegExpPrototypeTest(/^[0-9A-Za-z]$/,ch)){
331344
// Letter, number, shift+letter
332-
key.name=ch.toLowerCase();
333-
key.shift=/^[A-Z]$/.test(ch);
345+
key.name=StringPrototypeToLowerCase(ch);
346+
key.shift=RegExpPrototypeTest(/^[A-Z]$/,ch);
334347
key.meta=escaped;
335348
}elseif(escaped){
336349
// Escape sequence timeout
@@ -356,12 +369,12 @@ function commonPrefix(strings){
356369
if(strings.length===1){
357370
returnstrings[0];
358371
}
359-
constsorted=strings.slice().sort();
372+
constsorted=ArrayPrototypeSort(ArrayPrototypeSlice(strings));
360373
constmin=sorted[0];
361374
constmax=sorted[sorted.length-1];
362375
for(leti=0;i<min.length;i++){
363376
if(min[i]!==max[i]){
364-
returnmin.slice(0,i);
377+
returnStringPrototypeSlice(min,0,i);
365378
}
366379
}
367380
returnmin;

0 commit comments

Comments
(0)