forked from IKS/rdfQuery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.datatype.js
431 lines (405 loc) · 12.4 KB
/
jquery.datatype.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
* jQuery CURIE @VERSION
*
* Copyright (c) 2008,2009 Jeni Tennison
* Licensed under the MIT (MIT-LICENSE.txt)
*
* Depends:
* jquery.uri.js
*/
/**
* @fileOverview XML Schema datatype handling
* @author <a href="mailto:[email protected]">Jeni Tennison</a>
* @copyright (c) 2008,2009 Jeni Tennison
* @license MIT license (MIT-LICENSE.txt)
* @version 1.0
* @requires jquery.uri.js
*/
(function ($) {
var strip = function (value) {
return value.replace(/[ \t\n\r]+/, ' ').replace(/^ +/, '').replace(/ +$/, '');
};
/**
* Creates a new jQuery.typedValue object. This should be invoked as a method
* rather than constructed using new.
* @class Represents a value with an XML Schema datatype
* @param {String} value The string representation of the value
* @param {String} datatype The XML Schema datatype URI
* @returns {jQuery.typedValue}
* @example intValue = jQuery.typedValue('42', 'http://www.w3.org/2001/XMLSchema#integer');
*/
$.typedValue = function (value, datatype) {
return $.typedValue.fn.init(value, datatype);
};
$.typedValue.fn = $.typedValue.prototype = {
/**
* The string representation of the value
* @memberOf jQuery.typedValue#
*/
representation: undefined,
/**
* The value as an object. The type of the object will
* depend on the XML Schema datatype URI specified
* in the constructor. The following table lists the mappings
* currently supported:
* <table>
* <tr>
* <th>XML Schema Datatype</th>
* <th>Value type</th>
* </tr>
* <tr>
* <td>http://www.w3.org/2001/XMLSchema#string</td>
* <td>string</td>
* </tr>
* <tr>
* <td>http://www.w3.org/2001/XMLSchema#token</td>
* <td>string</td>
* </tr>
* <tr>
* <td>http://www.w3.org/2001/XMLSchema#NCName</td>
* <td>string</td>
* </tr>
* <tr>
* <td>http://www.w3.org/2001/XMLSchema#boolean</td>
* <td>bool</td>
* </tr>
* <tr>
* <td>http://www.w3.org/2001/XMLSchema#decimal</td>
* <td>string</td>
* </tr>
* <tr>
* <td>http://www.w3.org/2001/XMLSchema#integer</td>
* <td>int</td>
* </tr>
* <tr>
* <td>http://www.w3.org/2001/XMLSchema#int</td>
* <td>int</td>
* </tr>
* <tr>
* <td>http://www.w3.org/2001/XMLSchema#float</td>
* <td>float</td>
* </tr>
* <tr>
* <td>http://www.w3.org/2001/XMLSchema#double</td>
* <td>float</td>
* </tr>
* <tr>
* <td>http://www.w3.org/2001/XMLSchema#dateTime</td>
* <td>string</td>
* </tr>
* <tr>
* <td>http://www.w3.org/2001/XMLSchema#date</td>
* <td>string</td>
* </tr>
* <tr>
* <td>http://www.w3.org/2001/XMLSchema#gYear</td>
* <td>int</td>
* </tr>
* <tr>
* <td>http://www.w3.org/2001/XMLSchema#gMonthDay</td>
* <td>string</td>
* </tr>
* <tr>
* <td>http://www.w3.org/2001/XMLSchema#anyURI</td>
* <td>{@link jQuery.uri}</td>
* </tr>
* </table>
* @memberOf jQuery.typedValue#
*/
value: undefined,
/**
* The XML Schema datatype URI for the value's datatype
* @memberOf jQuery.typedValue#
*/
datatype: undefined,
init: function (value, datatype) {
var d = $.typedValue.types[datatype];
if ($.typedValue.valid(value, datatype)) {
this.representation = value;
this.datatype = datatype;
this.value = d === undefined ? strip(value) : d.value(d.strip ? strip(value) : value);
return this;
} else {
throw {
name: 'InvalidValue',
message: value + ' is not a valid ' + datatype + ' value'
};
}
}
};
$.typedValue.fn.init.prototype = $.typedValue.fn;
/**
* An object that holds the datatypes supported by the script. The properties of this object are the URIs of the datatypes, and each datatype has four properties:
* <dl>
* <dt>strip</dt>
* <dd>A boolean value that indicates whether whitespace should be stripped from the value prior to testing against the regular expression or passing to the value function.</dd>
* <dt>regex</dt>
* <dd>A regular expression that valid values of the type must match.</dd>
* <dt>validate</dt>
* <dd>Optional. A function that performs further testing on the value.</dd>
* <dt>value</dt>
* <dd>A function that returns a Javascript object equivalent for the value.</dd>
* </dl>
* You can add to this object as necessary for your own datatypes, and {@link jQuery.typedValue} and {@link jQuery.typedValue.valid} will work with them.
* @see jQuery.typedValue
* @see jQuery.typedValue.valid
*/
$.typedValue.types = {};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#string'] = {
regex: /^.*$/,
strip: false,
/** @ignore */
value: function (v) {
return v;
}
};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#token'] = {
regex: /^.*$/,
strip: true,
/** @ignore */
value: function (v) {
return strip(v);
}
};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#NCName'] = {
regex: /^[a-z_][-\.a-z0-9]+$/i,
strip: true,
/** @ignore */
value: function (v) {
return strip(v);
}
};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#boolean'] = {
regex: /^(?:true|false|1|0)$/,
strip: true,
/** @ignore */
value: function (v) {
return v === 'true' || v === '1';
}
};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#decimal'] = {
regex: /^[\-\+]?(?:[0-9]+\.[0-9]*|\.[0-9]+|[0-9]+)$/,
strip: true,
/** @ignore */
value: function (v) {
v = v.replace(/^0+/, '')
.replace(/0+$/, '');
if (v === '') {
v = '0.0';
}
if (v.substring(0, 1) === '.') {
v = '0' + v;
}
if (/\.$/.test(v)) {
v = v + '0';
} else if (!/\./.test(v)) {
v = v + '.0';
}
return v;
}
};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#integer'] = {
regex: /^[\-\+]?[0-9]+$/,
strip: true,
/** @ignore */
value: function (v) {
return parseInt(v, 10);
}
};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#int'] = {
regex: /^[\-\+]?[0-9]+$/,
strip: true,
/** @ignore */
value: function (v) {
return parseInt(v, 10);
}
};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#float'] = {
regex: /^(?:[\-\+]?(?:[0-9]+\.[0-9]*|\.[0-9]+|[0-9]+)(?:[eE][\-\+]?[0-9]+)?|[\-\+]?INF|NaN)$/,
strip: true,
/** @ignore */
value: function (v) {
if (v === '-INF') {
return -1 / 0;
} else if (v === 'INF' || v === '+INF') {
return 1 / 0;
} else {
return parseFloat(v);
}
}
};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#double'] = {
regex: $.typedValue.types['http://www.w3.org/2001/XMLSchema#float'].regex,
strip: true,
value: $.typedValue.types['http://www.w3.org/2001/XMLSchema#float'].value
};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#duration'] = {
regex: /^([\-\+])?P(?:([0-9]+)Y)?(?:([0-9]+)M)?(?:([0-9]+)D)?(?:T(?:([0-9]+)H)?(?:([0-9]+)M)?(?:([0-9]+(?:\.[0-9]+)?)?S)?)$/,
/** @ignore */
validate: function (v) {
var m = this.regex.exec(v);
return m[2] || m[3] || m[4] || m[5] || m[6] || m[7];
},
strip: true,
/** @ignore */
value: function (v) {
return v;
}
};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#yearMonthDuration'] = {
regex: /^([\-\+])?P(?:([0-9]+)Y)?(?:([0-9]+)M)?$/,
/** @ignore */
validate: function (v) {
var m = this.regex.exec(v);
return m[2] || m[3];
},
strip: true,
/** @ignore */
value: function (v) {
var m = this.regex.exec(v),
years = m[2] || 0,
months = m[3] || 0;
months += years * 12;
return m[1] === '-' ? -1 * months : months;
}
};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#dateTime'] = {
regex: /^(-?[0-9]{4,})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):(([0-9]{2})(\.([0-9]+))?)((?:[\-\+]([0-9]{2}):([0-9]{2}))|Z)?$/,
/** @ignore */
validate: function (v) {
var
m = this.regex.exec(v),
year = parseInt(m[1], 10),
tz = m[10] === undefined || m[10] === 'Z' ? '+0000' : m[10].replace(/:/, ''),
date;
if (year === 0 ||
parseInt(tz, 10) < -1400 || parseInt(tz, 10) > 1400) {
return false;
}
try {
year = year < 100 ? Math.abs(year) + 1000 : year;
month = parseInt(m[2], 10);
day = parseInt(m[3], 10);
if (day > 31) {
return false;
} else if (day > 30 && !(month === 1 || month === 3 || month === 5 || month === 7 || month === 8 || month === 10 || month === 12)) {
return false;
} else if (month === 2) {
if (day > 29) {
return false;
} else if (day === 29 && (year % 4 !== 0 || (year % 100 === 0 && year % 400 !== 0))) {
return false;
}
}
date = '' + year + '/' + m[2] + '/' + m[3] + ' ' + m[4] + ':' + m[5] + ':' + m[7] + ' ' + tz;
date = new Date(date);
return true;
} catch (e) {
return false;
}
},
strip: true,
/** @ignore */
value: function (v) {
return v;
}
};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#date'] = {
regex: /^(-?[0-9]{4,})-([0-9]{2})-([0-9]{2})((?:[\-\+]([0-9]{2}):([0-9]{2}))|Z)?$/,
/** @ignore */
validate: function (v) {
var
m = this.regex.exec(v),
year = parseInt(m[1], 10),
month = parseInt(m[2], 10),
day = parseInt(m[3], 10),
tz = m[10] === undefined || m[10] === 'Z' ? '+0000' : m[10].replace(/:/, '');
if (year === 0 ||
month > 12 ||
day > 31 ||
parseInt(tz, 10) < -1400 || parseInt(tz, 10) > 1400) {
return false;
} else {
return true;
}
},
strip: true,
/** @ignore */
value: function (v) {
return v;
}
};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#gYear'] = {
regex: /^-?([0-9]{4,})$/,
/** @ignore */
validate: function (v) {
var i = parseInt(v, 10);
return i !== 0;
},
strip: true,
/** @ignore */
value: function (v) {
return parseInt(v, 10);
}
};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#gMonthDay'] = {
regex: /^--([0-9]{2})-([0-9]{2})((?:[\-\+]([0-9]{2}):([0-9]{2}))|Z)?$/,
/** @ignore */
validate: function (v) {
var
m = this.regex.exec(v),
month = parseInt(m[1], 10),
day = parseInt(m[2], 10),
tz = m[3] === undefined || m[3] === 'Z' ? '+0000' : m[3].replace(/:/, '');
if (month > 12 ||
day > 31 ||
parseInt(tz, 10) < -1400 || parseInt(tz, 10) > 1400) {
return false;
} else if (month === 2 && day > 29) {
return false;
} else if ((month === 4 || month === 6 || month === 9 || month === 11) && day > 30) {
return false;
} else {
return true;
}
},
strip: true,
/** @ignore */
value: function (v) {
return v;
}
};
$.typedValue.types['http://www.w3.org/2001/XMLSchema#anyURI'] = {
regex: /^.*$/,
strip: true,
/** @ignore */
value: function (v, options) {
var opts = $.extend({}, $.typedValue.defaults, options);
return $.uri.resolve(v, opts.base);
}
};
$.typedValue.defaults = {
base: $.uri.base(),
namespaces: {}
};
/**
* Checks whether a value is valid according to a given datatype. The datatype must be held in the {@link jQuery.typedValue.types} object.
* @param {String} value The value to validate.
* @param {String} datatype The URI for the datatype against which the value will be validated.
* @returns {boolean} True if the value is valid or the datatype is not recognised.
* @example validDate = $.typedValue.valid(date, 'http://www.w3.org/2001/XMLSchema#date');
*/
$.typedValue.valid = function (value, datatype) {
var d = $.typedValue.types[datatype];
if (d === undefined) {
return true;
} else {
value = d.strip ? strip(value) : value;
if (d.regex.test(value)) {
return d.validate === undefined ? true : d.validate(value);
} else {
return false;
}
}
};
})(jQuery);