UNPKG

954 BJavaScriptView Raw
1var sanitizer = module.exports = {};
2
3
4/*
5 * options = { radix = 10 }
6 */
7sanitizer.toInt = function(ctx, options, value) {
8 if (value === null) return value;
9
10 options.radix = options.radix || 10;
11 return parseInt(value, options.radix);
12};
13
14
15/*
16 * options = { precision }
17 */
18sanitizer.toFloat = function(ctx, options, value) {
19 if (value === null) return value;
20
21 value = parseFloat(value);
22 if (typeof options.precision != 'undefined') {
23 value = value.toFixed(options.precision);
24 value = parseFloat(value);
25 }
26 return value;
27};
28
29
30sanitizer.toDate = function(ctx, options, value) {
31 if (value === null) return value;
32 return new Date(value);
33};
34
35
36/*
37 * options = { chars }
38 */
39sanitizer.trim = function(ctx, options, value) {
40 if (value === null) return value;
41
42 value += '';
43
44 options.chars = options.chars || '\\r\\n\\t\\s';
45
46 return value.replace(new RegExp('^[' + options.chars + ']+|[' + options.chars + ']+$', 'g'), '');
47};