Skip to content

Commit 31df23c

Browse files
committed
process: refactor process per thread
• Validate the `prevValue` parameter in the `cpuUsage()` function at the correct place. • Use validation methods for consistency. • Use the logical OR assignment operator (`||=`) where appropriate. • Use `StringPrototypeReplaceAll()` where appropriate.
1 parent 96e00e0 commit 31df23c

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

lib/internal/process/per_thread.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const {
2323
SetPrototypeValues,
2424
StringPrototypeEndsWith,
2525
StringPrototypeReplace,
26+
StringPrototypeReplaceAll,
2627
StringPrototypeSlice,
2728
StringPrototypeStartsWith,
2829
Symbol,
@@ -33,15 +34,14 @@ const {
3334
const {
3435
errnoException,
3536
codes: {
36-
ERR_ASSERTION,
37-
ERR_INVALID_ARG_TYPE,
3837
ERR_INVALID_ARG_VALUE,
3938
ERR_OUT_OF_RANGE,
4039
ERR_UNKNOWN_SIGNAL
4140
}
4241
} = require('internal/errors');
4342
const format = require('internal/util/inspect').format;
4443
const {
44+
isInt32,
4545
validateArray,
4646
validateNumber,
4747
validateObject,
@@ -120,9 +120,9 @@ function wrapProcessMethods(binding) {
120120
function cpuUsage(prevValue) {
121121
// If a previous value was passed in, ensure it has the correct shape.
122122
if (prevValue) {
123-
if (!previousValueIsValid(prevValue.user)) {
124-
validateObject(prevValue, 'prevValue');
123+
validateObject(prevValue, 'prevValue');
125124

125+
if (!previousValueIsValid(prevValue.user)) {
126126
validateNumber(prevValue.user, 'prevValue.user');
127127
throw new ERR_INVALID_ARG_VALUE.RangeError('prevValue.user',
128128
prevValue.user);
@@ -192,18 +192,15 @@ function wrapProcessMethods(binding) {
192192
function kill(pid, sig) {
193193
let err;
194194

195-
// eslint-disable-next-line eqeqeq
196-
if (pid != (pid | 0)) {
197-
throw new ERR_INVALID_ARG_TYPE('pid', 'number', pid);
198-
}
195+
validateNumber(pid, 'pid');
199196

200197
// Preserve null signal
201-
if (sig === (sig | 0)) {
198+
if (isInt32(sig)) {
202199
// XXX(joyeecheung): we have to use process._kill here because
203200
// it's monkey-patched by tests.
204201
err = process._kill(pid, sig);
205202
} else {
206-
sig = sig || 'SIGTERM';
203+
sig ||= 'SIGTERM';
207204
if (constants[sig]) {
208205
err = process._kill(pid, constants[sig]);
209206
} else {
@@ -251,7 +248,6 @@ function wrapProcessMethods(binding) {
251248
};
252249
}
253250

254-
const replaceUnderscoresRegex = /_/g;
255251
const leadingDashesRegex = /^--?/;
256252
const trailingValuesRegex = /=.*$/;
257253

@@ -333,7 +329,7 @@ function buildAllowedFlags() {
333329
// on a dummy option set and see whether it rejects the argument or
334330
// not.
335331
if (typeof key === 'string') {
336-
key = StringPrototypeReplace(key, replaceUnderscoresRegex, '-');
332+
key = StringPrototypeReplaceAll(key, '_', '-');
337333
if (RegExpPrototypeTest(leadingDashesRegex, key)) {
338334
key = StringPrototypeReplace(key, trailingValuesRegex, '');
339335
return ArrayPrototypeIncludes(this[kInternal].array, key);

0 commit comments

Comments
 (0)