Skip to content

Commit 43b60af

Browse files
committed
url: remove redundant function
1 parent e997db9 commit 43b60af

File tree

2 files changed

+21
-76
lines changed

2 files changed

+21
-76
lines changed

lib/internal/url.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -878,8 +878,7 @@ function serializeParams(array) {
878878

879879
const firstEncodedParam = encodeStr(array[0], noEscape, paramHexTable);
880880
const firstEncodedValue = encodeStr(array[1], noEscape, paramHexTable);
881-
let output =
882-
`${firstEncodedParam}=${firstEncodedValue}`;
881+
let output = `${firstEncodedParam}=${firstEncodedValue}`;
883882

884883
for (var i = 2; i < len; i += 2) {
885884
const encodedParam = encodeStr(array[i], noEscape, paramHexTable);

lib/url.js

+20-74
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ const {
3636
URLSearchParams,
3737
domainToASCII,
3838
domainToUnicode,
39-
formatSymbol
39+
formatSymbol,
40+
encodeStr,
4041
} = require('internal/url');
4142

4243
// Original url.parse() API
@@ -504,10 +505,27 @@ function urlFormat(urlObject, options) {
504505
return urlObject.format();
505506
}
506507

508+
// These characters do not need escaping:
509+
// ! - . _ ~
510+
// ' ( ) * :
511+
// digits
512+
// alpha (uppercase)
513+
// alpha (lowercase)
514+
const noEscapeAuth = [
515+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00 - 0x0F
516+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10 - 0x1F
517+
0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, // 0x20 - 0x2F
518+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, // 0x30 - 0x3F
519+
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40 - 0x4F
520+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 0x50 - 0x5F
521+
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 0x6F
522+
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0 // 0x70 - 0x7F
523+
];
524+
507525
Url.prototype.format = function format() {
508526
var auth = this.auth || '';
509527
if (auth) {
510-
auth = encodeAuth(auth);
528+
auth = encodeStr(auth, noEscapeAuth, hexTable);
511529
auth += '@';
512530
}
513531

@@ -890,78 +908,6 @@ Url.prototype.parseHost = function parseHost() {
890908
if (host) this.hostname = host;
891909
};
892910

893-
// These characters do not need escaping:
894-
// ! - . _ ~
895-
// ' ( ) * :
896-
// digits
897-
// alpha (uppercase)
898-
// alpha (lowercase)
899-
const noEscapeAuth = [
900-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00 - 0x0F
901-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10 - 0x1F
902-
0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, // 0x20 - 0x2F
903-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, // 0x30 - 0x3F
904-
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40 - 0x4F
905-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, // 0x50 - 0x5F
906-
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 0x6F
907-
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0 // 0x70 - 0x7F
908-
];
909-
910-
function encodeAuth(str) {
911-
// faster encodeURIComponent alternative for encoding auth uri components
912-
var out = '';
913-
var lastPos = 0;
914-
for (var i = 0; i < str.length; ++i) {
915-
var c = str.charCodeAt(i);
916-
917-
// ASCII
918-
if (c < 0x80) {
919-
if (noEscapeAuth[c] === 1)
920-
continue;
921-
if (lastPos < i)
922-
out += str.slice(lastPos, i);
923-
lastPos = i + 1;
924-
out += hexTable[c];
925-
continue;
926-
}
927-
928-
if (lastPos < i)
929-
out += str.slice(lastPos, i);
930-
931-
// Multi-byte characters ...
932-
if (c < 0x800) {
933-
lastPos = i + 1;
934-
out += hexTable[0xC0 | (c >> 6)] + hexTable[0x80 | (c & 0x3F)];
935-
continue;
936-
}
937-
if (c < 0xD800 || c >= 0xE000) {
938-
lastPos = i + 1;
939-
out += hexTable[0xE0 | (c >> 12)] +
940-
hexTable[0x80 | ((c >> 6) & 0x3F)] +
941-
hexTable[0x80 | (c & 0x3F)];
942-
continue;
943-
}
944-
// Surrogate pair
945-
++i;
946-
var c2;
947-
if (i < str.length)
948-
c2 = str.charCodeAt(i) & 0x3FF;
949-
else
950-
c2 = 0;
951-
lastPos = i + 1;
952-
c = 0x10000 + (((c & 0x3FF) << 10) | c2);
953-
out += hexTable[0xF0 | (c >> 18)] +
954-
hexTable[0x80 | ((c >> 12) & 0x3F)] +
955-
hexTable[0x80 | ((c >> 6) & 0x3F)] +
956-
hexTable[0x80 | (c & 0x3F)];
957-
}
958-
if (lastPos === 0)
959-
return str;
960-
if (lastPos < str.length)
961-
return out + str.slice(lastPos);
962-
return out;
963-
}
964-
965911
module.exports = {
966912
// Original API
967913
Url,

0 commit comments

Comments
 (0)