-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
74 lines (62 loc) · 1.76 KB
/
index.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*jslint node: true */
"use strict";
var u = require("underscore");
exports.truth = (function () {
var tex = /^\s*(?:true|yes|t|y|1)\s*$/i,
fex = /^\s*(?:false|no|f|n|0)?\s*$/i;
return function (v) {
var rval;
if (v === null || v === undefined) {
rval = undefined;
} else if (u.isBoolean(v)) {
rval = v;
} else if (u.isString(v)) {
if (v.match(tex)) {
rval = true;
} else if (v.match(fex)) {
rval = false;
} else {
throw new Error("cannot convert string to boolean");
}
} else if (v === 0) {
rval = false;
} else if (v === 1) {
rval = true;
} else {
throw new Error("cannot convert type to boolean");
}
return rval;
};
}());
exports.nstr = function (v) {
var temp, rval;
if (v === null || v === undefined) {
rval = undefined;
} else if (u.isString(v)) {
temp = v.trim();
rval = temp || undefined;
} else {
throw new Error("cannot normalize non-string");
}
return rval;
};
exports.num = function (v) {
var temp, rval;
if (v === null || v === undefined) {
rval = undefined;
} else if (u.isNumber(v)) {
rval = v;
} else if (u.isString(v)) {
temp = parseFloat(v || "0");
rval = u.isNaN(temp) ? undefined : temp;
} else {
throw new Error("cannot convert to number");
}
return rval;
};
exports.require = function (v, desc) {
if (v === null || v === undefined) {
throw new Error(desc || "value is required");
}
return v;
};