-
Notifications
You must be signed in to change notification settings - Fork 2
/
modifiers.js
199 lines (165 loc) · 4.9 KB
/
modifiers.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var typeOf = require('precise-typeof');
// by default use just `date`, `json` modifiers
module.exports = {
// relies on built-in JSON.parse
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
json: json,
// relies on built-in Date.parse
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
date: date,
// relies on built-in Number.parseInt
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/parseInt
number: number,
// use `int` as an alias to number
int: number,
// relies on built-in Number.parseFloat
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/parseFloat
float: float,
// makes sure only arrays go through
array: array,
// makes sure only objects go through
object: object,
// makes sure on boolean go through
boolean: boolean,
// use `bool` as an alias to boolean
bool: boolean,
// provides regexp support
regexp: regexp
};
/**
* Parses provided string as JSON string
*
* @param {string} value - Date string to parse
* @returns {object} - Parsed JSON POJO
*/
function json(value) {
var parsedPojo;
try {
parsedPojo = JSON.parse(value);
} catch(e) {
return throwModifierError('json', value, e);
}
return parsedPojo;
}
/**
* Parses provided string as a Date string
*
* @param {string} value - Date string to parse
* @returns {Date} - Date object representaion of the provided string
*/
function date(value) {
var parsedDate = Date.parse(value);
// couldn't parse it
if (typeOf(parsedDate) != 'number') {
return throwModifierError('date', value, {message: 'Invalid Date'});
}
return new Date(parsedDate);
}
/**
* Parses provided string as a Number (Int)
*
* @param {string} value - string to parse
* @returns {number} - Parsed integer number
*/
function number(value) {
var parsedNumber = Number.parseInt(value, 10);
// couldn't parse it
if (typeOf(parsedNumber) != 'number') {
return throwModifierError('number', value, {message: 'Invalid Number'});
}
return parsedNumber;
}
/**
* Parses provided string as Float
*
* @param {string} value - string to parse
* @returns {float} - Parsed float number
*/
function float(value) {
var parsedFloat = Number.parseFloat(value);
// couldn't parse it
if (typeOf(parsedFloat) != 'number') {
return throwModifierError('float', value, {message: 'Invalid Float'});
}
return parsedFloat;
}
/**
* Parses provided string as an array (json + array validation)
*
* @param {string} value - string to parse
* @returns {array} - Parsed array
*/
function array(value) {
var parsedArray = json(value);
// couldn't parse it
if (typeOf(parsedArray) != 'array') {
return throwModifierError('array', value, {message: 'Invalid Array'});
}
return parsedArray;
}
/**
* Parses provided string as an object (json + object validation)
*
* @param {string} value - string to parse
* @returns {object} - Parsed object
*/
function object(value) {
var parsedObject = json(value);
// couldn't parse it
if (typeOf(parsedObject) != 'object') {
return throwModifierError('object', value, {message: 'Invalid Object'});
}
return parsedObject;
}
/**
* Parses provided string as a boolean (json + boolean validation)
*
* @param {string} value - string to parse
* @returns {boolean} - Parsed boolean
*/
function boolean(value) {
var parsedBoolean = json(value);
// handle 0|1 case
if (typeOf(parsedBoolean) == 'number' && [0, 1].indexOf(parsedBoolean) > -1) {
parsedBoolean = parsedBoolean === 0 ? false : true;
}
// couldn't parse it
if (typeOf(parsedBoolean) != 'boolean') {
return throwModifierError('boolean', value, {message: 'Invalid Boolean'});
}
return parsedBoolean;
}
/**
* Parses provided string as a RegExp object
* if provided string starts and ends with `/`
* (with optional modifiers after the last `/`)
* then it's considered as regular expression string
* so characters between slashes are used as input
* for the new RegExp object, with optional modifiers
* as second argument, otherwise the whole string
* is used as RegExp input.
*
* @param {string} value - string to parse
* @returns {RegExp} - Parsed regular expression
*/
function regexp(value) {
var re
, pattern = value.match(/^\/(.+)\/([a-z]*)$/)
;
if (pattern) {
re = new RegExp(pattern[1], pattern[2] || '');
} else {
re = new RegExp(value);
}
return re;
}
/**
* Throws modifiers errors in a centralized way
*
* @param {string} modifier - Modifier name
* @param {mixed} value - Attempted to parse value
* @param {object} exception - Corresponding exception, if provided
*/
function throwModifierError(modifier, value, exception) {
throw new Error('Unable to parse provided value `' + value + '` with `' + modifier + '` modifier. Exception: ' + exception.message);
}