Skip to content

Commit 5082609

Browse files
steven-supersolidflovilmart
authored andcommitted
Allow numberOrBooleanParser to parse string correctly. Improve error message in numberParser. Add env variable PARSE_SERVER_CLUSTER for cluster. Add tests (#3034)
1 parent 9abf177 commit 5082609

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

spec/parsers.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {
2+
numberParser,
3+
numberOrBoolParser,
4+
booleanParser,
5+
} from '../src/cli/utils/parsers';
6+
7+
describe('parsers', () => {
8+
it('parses correctly with numberParser', () => {
9+
const parser = numberParser('key');
10+
expect(parser(2)).toEqual(2);
11+
expect(parser('2')).toEqual(2);
12+
expect(() => {parser('string')}).toThrow();
13+
});
14+
15+
it('parses correctly with numberOrBoolParser', () => {
16+
const parser = numberOrBoolParser('key');
17+
expect(parser(true)).toEqual(true);
18+
expect(parser(false)).toEqual(false);
19+
expect(parser('true')).toEqual(true);
20+
expect(parser('false')).toEqual(false);
21+
expect(parser(1)).toEqual(1);
22+
expect(parser('1')).toEqual(1);
23+
});
24+
25+
it('parses correctly with booleanParser', () => {
26+
const parser = booleanParser;
27+
expect(parser(true)).toEqual(true);
28+
expect(parser(false)).toEqual(false);
29+
expect(parser('true')).toEqual(true);
30+
expect(parser('false')).toEqual(false);
31+
expect(parser(1)).toEqual(true);
32+
expect(parser(2)).toEqual(false);
33+
});
34+
});

src/cli/definitions/parse-server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ export default {
201201
action: booleanParser
202202
},
203203
"cluster": {
204+
env: PARSE_SERVER_CLUSTER,
204205
help: "Run with cluster, optionally set the number of processes default to os.cpus().length",
205206
action: numberOrBoolParser("cluster")
206207
},

src/cli/utils/parsers.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
export function numberParser(key) {
22
return function(opt) {
3-
opt = parseInt(opt);
4-
if (!Number.isInteger(opt)) {
5-
throw new Error(`The ${key} is invalid`);
3+
const intOpt = parseInt(opt);
4+
if (!Number.isInteger(intOpt)) {
5+
throw new Error(`Key ${key} has invalid value ${opt}`);
66
}
7-
return opt;
7+
return intOpt;
88
}
99
}
1010

@@ -13,6 +13,12 @@ export function numberOrBoolParser(key) {
1313
if (typeof opt === 'boolean') {
1414
return opt;
1515
}
16+
if (opt === 'true') {
17+
return true;
18+
}
19+
if (opt === 'false') {
20+
return false;
21+
}
1622
return numberParser(key)(opt);
1723
}
1824
}
@@ -45,7 +51,7 @@ export function moduleOrObjectParser(opt) {
4551
}
4652

4753
export function booleanParser(opt) {
48-
if (opt == true || opt == "true" || opt == "1") {
54+
if (opt == true || opt == 'true' || opt == '1') {
4955
return true;
5056
}
5157
return false;

0 commit comments

Comments
 (0)