Skip to content

Commit 354849e

Browse files
dayninlpinca
authored andcommitted
url: replace "magic" numbers by constants
PR-URL: #19300 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
1 parent 422ac61 commit 354849e

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/internal/constants.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module.exports = {
3939
CHAR_CIRCUMFLEX_ACCENT: 94, /* ^ */
4040
CHAR_GRAVE_ACCENT: 96, /* ` */
4141
CHAR_AT: 64, /* @ */
42+
CHAR_AMPERSAND: 38, /* & */
43+
CHAR_EQUAL: 61, /* = */
4244

4345
// Digits
4446
CHAR_0: 48, /* 0 */

lib/internal/url.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ const {
1919
ERR_INVALID_URL_SCHEME,
2020
ERR_MISSING_ARGS
2121
} = require('internal/errors').codes;
22+
const {
23+
CHAR_PERCENT,
24+
CHAR_PLUS,
25+
CHAR_AMPERSAND,
26+
CHAR_EQUAL,
27+
CHAR_LOWERCASE_A,
28+
CHAR_LOWERCASE_Z,
29+
} = require('internal/constants');
2230
const querystring = require('querystring');
2331

2432
const { platform } = process;
@@ -712,7 +720,7 @@ function parseParams(qs) {
712720
const code = qs.charCodeAt(i);
713721

714722
// Try matching key/value pair separator
715-
if (code === 38/* & */) {
723+
if (code === CHAR_AMPERSAND) {
716724
if (pairStart === i) {
717725
// We saw an empty substring between pair separators
718726
lastPos = pairStart = i + 1;
@@ -738,7 +746,7 @@ function parseParams(qs) {
738746
}
739747

740748
// Try matching key/value separator (e.g. '=') if we haven't already
741-
if (!seenSep && code === 61/* = */) {
749+
if (!seenSep && code === CHAR_EQUAL) {
742750
// Key/value separator match!
743751
if (lastPos < i)
744752
buf += qs.slice(lastPos, i);
@@ -755,15 +763,15 @@ function parseParams(qs) {
755763
}
756764

757765
// Handle + and percent decoding.
758-
if (code === 43/* + */) {
766+
if (code === CHAR_PLUS) {
759767
if (lastPos < i)
760768
buf += qs.slice(lastPos, i);
761769
buf += ' ';
762770
lastPos = i + 1;
763771
} else if (!encoded) {
764772
// Try to match an (valid) encoded byte (once) to minimize unnecessary
765773
// calls to string decoding functions
766-
if (code === 37/* % */) {
774+
if (code === CHAR_PERCENT) {
767775
encodeCheck = 1;
768776
} else if (encodeCheck > 0) {
769777
// eslint-disable-next-line no-extra-boolean-cast
@@ -1357,7 +1365,7 @@ function getPathFromURLWin32(url) {
13571365
// Otherwise, it's a local path that requires a drive letter
13581366
var letter = pathname.codePointAt(1) | 0x20;
13591367
var sep = pathname[2];
1360-
if (letter < 97 || letter > 122 || // a..z A..Z
1368+
if (letter < CHAR_LOWERCASE_A || letter > CHAR_LOWERCASE_Z || // a..z A..Z
13611369
(sep !== ':')) {
13621370
throw new ERR_INVALID_FILE_URL_PATH('must be absolute');
13631371
}

0 commit comments

Comments
 (0)