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
|
action: booleanParser
|
||||||
},
|
},
|
||||||
"cluster": {
|
"cluster": {
|
||||||
|
env: PARSE_SERVER_CLUSTER,
|
||||||
help: "Run with cluster, optionally set the number of processes default to os.cpus().length",
|
help: "Run with cluster, optionally set the number of processes default to os.cpus().length",
|
||||||
action: numberOrBoolParser("cluster")
|
action: numberOrBoolParser("cluster")
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
export function numberParser(key) {
|
export function numberParser(key) {
|
||||||
return function(opt) {
|
return function(opt) {
|
||||||
opt = parseInt(opt);
|
const intOpt = parseInt(opt);
|
||||||
if (!Number.isInteger(opt)) {
|
if (!Number.isInteger(intOpt)) {
|
||||||
throw new Error(`The ${key} is invalid`);
|
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') {
|
if (typeof opt === 'boolean') {
|
||||||
return opt;
|
return opt;
|
||||||
}
|
}
|
||||||
|
if (opt === 'true') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (opt === 'false') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return numberParser(key)(opt);
|
return numberParser(key)(opt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -45,7 +51,7 @@ export function moduleOrObjectParser(opt) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function booleanParser(opt) {
|
export function booleanParser(opt) {
|
||||||
if (opt == true || opt == "true" || opt == "1") {
|
if (opt == true || opt == 'true' || opt == '1') {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user