Allow numberOrBooleanParser to parse string correctly. Improve error message in numberParser. Add env variable PARSE_SERVER_CLUSTER for cluster. Add tests (#3034)
This commit is contained in:
committed by
Florent Vilmart
parent
9abf17730f
commit
5082609937
34
spec/parsers.spec.js
Normal file
34
spec/parsers.spec.js
Normal file
@@ -0,0 +1,34 @@
|
||||
import {
|
||||
numberParser,
|
||||
numberOrBoolParser,
|
||||
booleanParser,
|
||||
} from '../src/cli/utils/parsers';
|
||||
|
||||
describe('parsers', () => {
|
||||
it('parses correctly with numberParser', () => {
|
||||
const parser = numberParser('key');
|
||||
expect(parser(2)).toEqual(2);
|
||||
expect(parser('2')).toEqual(2);
|
||||
expect(() => {parser('string')}).toThrow();
|
||||
});
|
||||
|
||||
it('parses correctly with numberOrBoolParser', () => {
|
||||
const parser = numberOrBoolParser('key');
|
||||
expect(parser(true)).toEqual(true);
|
||||
expect(parser(false)).toEqual(false);
|
||||
expect(parser('true')).toEqual(true);
|
||||
expect(parser('false')).toEqual(false);
|
||||
expect(parser(1)).toEqual(1);
|
||||
expect(parser('1')).toEqual(1);
|
||||
});
|
||||
|
||||
it('parses correctly with booleanParser', () => {
|
||||
const parser = booleanParser;
|
||||
expect(parser(true)).toEqual(true);
|
||||
expect(parser(false)).toEqual(false);
|
||||
expect(parser('true')).toEqual(true);
|
||||
expect(parser('false')).toEqual(false);
|
||||
expect(parser(1)).toEqual(true);
|
||||
expect(parser(2)).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -201,6 +201,7 @@ export default {
|
||||
action: booleanParser
|
||||
},
|
||||
"cluster": {
|
||||
env: PARSE_SERVER_CLUSTER,
|
||||
help: "Run with cluster, optionally set the number of processes default to os.cpus().length",
|
||||
action: numberOrBoolParser("cluster")
|
||||
},
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
export function numberParser(key) {
|
||||
return function(opt) {
|
||||
opt = parseInt(opt);
|
||||
if (!Number.isInteger(opt)) {
|
||||
throw new Error(`The ${key} is invalid`);
|
||||
const intOpt = parseInt(opt);
|
||||
if (!Number.isInteger(intOpt)) {
|
||||
throw new Error(`Key ${key} has invalid value ${opt}`);
|
||||
}
|
||||
return opt;
|
||||
return intOpt;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,12 @@ export function numberOrBoolParser(key) {
|
||||
if (typeof opt === 'boolean') {
|
||||
return opt;
|
||||
}
|
||||
if (opt === 'true') {
|
||||
return true;
|
||||
}
|
||||
if (opt === 'false') {
|
||||
return false;
|
||||
}
|
||||
return numberParser(key)(opt);
|
||||
}
|
||||
}
|
||||
@@ -45,7 +51,7 @@ export function moduleOrObjectParser(opt) {
|
||||
}
|
||||
|
||||
export function booleanParser(opt) {
|
||||
if (opt == true || opt == "true" || opt == "1") {
|
||||
if (opt == true || opt == 'true' || opt == '1') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user