Skip to content

Commit 242f911

Browse files
dayninMayaLekova
authored andcommitted
module: replace "magic" numbers by constants
- add new constants - replace numbers by constants PR-URL: nodejs#18869 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Matheus Marchini <matheus@sthima.com>
1 parent b3f4145 commit 242f911

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

lib/internal/constants.js

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ module.exports = {
1414
CHAR_COLON: 58, /* : */
1515
CHAR_QUESTION_MARK: 63, /* ? */
1616
CHAR_UNDERSCORE: 95, /* _ */
17+
CHAR_LINE_FEED: 10, /* \n */
18+
CHAR_CARRIAGE_RETURN: 13, /* \r */
19+
CHAR_EXCLAMATION_MARK: 33, /* ! */
20+
CHAR_HASH: 35, /* # */
1721

1822
// Digits
1923
CHAR_0: 48, /* 0 */

lib/internal/module.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
const errors = require('internal/errors');
44

5+
const {
6+
CHAR_LINE_FEED,
7+
CHAR_CARRIAGE_RETURN,
8+
CHAR_EXCLAMATION_MARK,
9+
CHAR_HASH,
10+
} = require('internal/constants');
11+
512
// Invoke with makeRequireFunction(module) where |module| is the Module object
613
// to use as the context for the require() function.
714
function makeRequireFunction(mod) {
@@ -65,8 +72,8 @@ function stripShebang(content) {
6572
// Remove shebang
6673
var contLen = content.length;
6774
if (contLen >= 2) {
68-
if (content.charCodeAt(0) === 35/*#*/ &&
69-
content.charCodeAt(1) === 33/*!*/) {
75+
if (content.charCodeAt(0) === CHAR_HASH &&
76+
content.charCodeAt(1) === CHAR_EXCLAMATION_MARK) {
7077
if (contLen === 2) {
7178
// Exact match
7279
content = '';
@@ -75,7 +82,7 @@ function stripShebang(content) {
7582
var i = 2;
7683
for (; i < contLen; ++i) {
7784
var code = content.charCodeAt(i);
78-
if (code === 10/*\n*/ || code === 13/*\r*/)
85+
if (code === CHAR_LINE_FEED || code === CHAR_CARRIAGE_RETURN)
7986
break;
8087
}
8188
if (i === contLen)

0 commit comments

Comments
 (0)