-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
315 lines (281 loc) · 6.52 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
'use strict';
/**
* Expose `Normalizer`.
*/
module.exports = Normalizer;
/*!
* Module dependencies.
*/
var assert = require('assert');
/**
* @class
* Create a normalizer value from the given value.
*
* @param {*} val The value object
* @param {Object} opts The Options
*/
function Normalizer(val, opts) {
this.val = val;
this.opts = opts;
}
/*!
* Object containing converter functions.
*/
Normalizer.converters = {};
/**
* Define a named type conversion. The conversion is used when a
* `ApiMethod` argument defines a type with the given `name`.
*
* ```js
* Normalizer.define('MyType', function(val, opts) {
* // use the val and opts objects to return the concrete value
* return new MyType(val);
* });
* ```
*
* @param {String} name The type name
* @param {Function} converter
*/
Normalizer.define = function(name, converter) {
this.converters[name] = converter;
};
/**
* undefine a converter via its name
*/
Normalizer.undefine = function(name) {
delete this.converters[name];
};
/**
* Is the given type supported.
*
* @param {String} type
* @returns {Boolean}
*/
Normalizer.canConvert = function(type) {
return !!this.getConverter(type);
};
/**
* Try to convert value
*
* @param {String} val
* @param {String} type
* @param {Object} opts
* @returns {Object}
*/
Normalizer.tryConvert = function(val, toType, opts) {
if (this.canConvert(toType)) {
return this.convert(val, toType, opts);
} else {
return val;
}
};
/**
* Get converter by type name.
*
* @param {String} type
* @returns {Function}
*/
Normalizer.getConverter = function(type) {
if (Array.isArray(type)) {
if (type.length > 1) return;
type = type[0];
}
return this.converters[type];
};
/**
* Shortcut method for convert value
*
* @param {String} val
* @param {String} type
* @param {Object} opts
* @returns {Object}
*/
Normalizer.convert = function(val, toType, opts) {
if (!opts) opts = {};
if (Array.isArray(toType)) {
assert(toType.length === 1, 'Multiple types converter is not allowed');
// Convert val to array
val = Normalizer.convertArray(val, opts);
// Parse array inner value
return val.map(function(v) {
return Normalizer.convert(v, toType[0], opts);
});
}
return (new Normalizer(val, opts)).to(toType);
};
/**
* Convert value to array
*
* @param {String} val
* @param {Object} opts
* @returns {Array}
*/
Normalizer.convertArray = function(val, opts) {
if (!opts) opts = {};
// If we expect an array type and we received a string, parse it with JSON.
// If that fails, parse it with the arrayItemDelimiters option.
if (val && typeof val === 'string') {
var parsed = false;
if (val[0] === '[') {
try {
val = JSON.parse(val);
parsed = true;
} catch (e) { /* Do nothing */ }
}
if (!parsed && opts.arrayItemDelimiters) {
val = val.split(opts.arrayItemDelimiters);
}
}
if (!Array.isArray(val)) {
if (val === undefined || val === '') {
val = [];
} else {
val = [val];
}
}
return val;
};
/**
* Convert the normalizer value to the given type.
*
* @param {String} type
* @returns {*} The concrete value
*/
Normalizer.prototype.to = function(type) {
var converter = this.constructor.getConverter(type);
assert(converter, 'No Type converter defined for ' + type);
return converter(this.val, this.opts);
};
/**
* Built in type converters...
* number
* date
* string
* boolean
* object
* any
*/
/**
* number converter
* undefined => undefined
* '' => null
* null => null
* Number(val)
*/
Normalizer.define('number', function convertNumber(val) {
if (val === undefined || val === null || typeof val === 'number') return val;
if (val === '') return null;
return Number(val);
});
/**
* date converter
* undefined => undefined
* null => null
* new Date(val)
*/
Normalizer.define('date', function convertDate(val) {
if (val === undefined || val === null || val instanceof Date) return val;
return new Date(val);
});
/**
* string converter
* undefined => undefined
* null => null
* String(val)
*/
Normalizer.define('string', function convertString(val) {
if (val === undefined || val === null || typeof val === 'string') return val;
return String(val);
});
/**
* boolean converter
* undefined => undefined
* Boolean(val)
*/
Normalizer.define('boolean', function convertBoolean(val) {
switch (typeof val) {
case 'undefined':
return undefined;
case 'string':
switch (val) {
case 'undefined':
case 'null':
case 'false':
case '0':
case '':
return false;
default:
return true;
}
case 'number':
return Number.isNaN(val) ? false : val !== 0;
default:
return Boolean(val);
}
});
/*!
* Integer test regexp.
*/
var isInt = /^[0-9]+$/;
/*!
* Float test regexp.
*/
var isFloat = /^([0-9]+)?\.[0-9]+$/;
function coerce(str) {
if (typeof str !== 'string') return str;
if ('undefined' === str) return undefined;
if ('null' === str) return null;
if ('true' === str) return true;
if ('false' === str) return false;
if (isFloat.test(str)) return parseFloat(str, 10);
if (isInt.test(str) && str.charAt(0) !== '0') return parseInt(str, 10);
return str;
}
// coerce every string in the given object / array
function coerceAll(obj, coerceLevel) {
if (coerceLevel === 0) return obj;
var type = Array.isArray(obj) ? 'array' : typeof obj;
var i;
var n;
var _coerceLevel = coerceLevel - 1;
switch (type) {
case 'string':
return coerce(obj);
case 'object':
if (obj) {
var props = Object.keys(obj);
for (i = 0, n = props.length; i < n; i++) {
var key = props[i];
obj[key] = coerceAll(obj[key], _coerceLevel);
}
}
break;
case 'array':
for (i = 0, n = obj.length; i < n; i++) {
coerceAll(obj[i], _coerceLevel);
}
break;
}
return obj;
}
// convert object/any type
function convertAny(val, opts) {
if (!opts) opts = {};
// use maxCoerceLevel to prevent circular reference parse error
var maxCoerceLevel = opts.maxCoerceLevel || 5;
// needCoerce option: default to be `true`
var needCoerce = opts.coerce !== false;
return needCoerce ? coerceAll(val, maxCoerceLevel) : val;
}
/**
* object/any converter
* object/any => object/any
*/
Normalizer.define('any', convertAny);
Normalizer.define('object', convertAny);
/**
* array converter
*/
Normalizer.define('array', function convertArray(val, opts) {
return Normalizer.convert(val, ['any'], opts);
});