Skip to content

Commit b5b8a99

Browse files
aduh95targos
authored andcommitted
readline: refactor to use more primordials
PR-URL: #36296 Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent f5b2115 commit b5b8a99

File tree

2 files changed

+99
-57
lines changed

2 files changed

+99
-57
lines changed

lib/internal/readline/utils.js

+25-12
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
function charLengthLeft(str, i) {
3341
if (i <= 0)
3442
return 0;
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
return 2;
3847
}
3948
return 1;
@@ -45,7 +54,7 @@ function charLengthAt(str, i) {
4554
// moving to the right.
4655
return 1;
4756
}
48-
return str.codePointAt(i) >= kUTF16SurrogateThreshold ? 2 : 1;
57+
return StringPrototypeCodePointAt(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-
const cmd = s.slice(cmdStart);
190+
const cmd = StringPrototypeSlice(s, cmdStart);
182191
let match;
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-
} else if ((match = cmd.match(/^((\d;)?(\d))?([A-Za-z])$/))) {
196+
} else if (
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
} else if (!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-
} else if (/^[0-9A-Za-z]$/.test(ch)) {
343+
} else if (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
} else if (escaped) {
336349
// Escape sequence timeout
@@ -356,12 +369,12 @@ function commonPrefix(strings) {
356369
if (strings.length === 1) {
357370
return strings[0];
358371
}
359-
const sorted = strings.slice().sort();
372+
const sorted = ArrayPrototypeSort(ArrayPrototypeSlice(strings));
360373
const min = sorted[0];
361374
const max = sorted[sorted.length - 1];
362375
for (let i = 0; i < min.length; i++) {
363376
if (min[i] !== max[i]) {
364-
return min.slice(0, i);
377+
return StringPrototypeSlice(min, 0, i);
365378
}
366379
}
367380
return min;

0 commit comments

Comments
 (0)