-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprecondition-polymer-v1.js
More file actions
49 lines (42 loc) · 1.21 KB
/
precondition-polymer-v1.js
File metadata and controls
49 lines (42 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Traditional class
*/
let precondition = function (){};
precondition.checkArgument = function()
{
const expression = arguments[0] === null ? false : arguments[0];
let msg = this._prepareMessage(arguments);
if (!expression) {
throw new Error(msg);
}
}
precondition._prepareMessage = function()
{
var argLength = arguments[0].length;
var arg = arguments[0];
if (argLength === 0) {
return "Invalid usage, please supply the condition you want to check";
}
if (typeof(arg[0]) !== 'boolean') {
return "The argument expression must be Boolean";
}
if (typeof(arg[1]) !== 'string') {
return "The message expression must be String";
}
if (argLength == 2 && arg[1].includes("%s")) {
return "The error message contain indicator with no parameter to match.";
}
if (arg[1].split('%s').length != argLength - 1) {
return "The number of indicators are not equal to " +
"the number of parameters.";
}
var msg = arg[1];
var i;
if (argLength > 2) {
const len = arg[1].split('%s').length + 1;
for (i=2; i<len; i++) {
msg = msg.replace("%s", arg[i]);
}
}
return msg;
}