Skip to content

Commit 8d7925b

Browse files
committed
Add new prototype String.isJSONDate().
1 parent 142deb1 commit 8d7925b

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

test/test-utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,14 @@ function prototypeString() {
112112
assert.ok(str.isJSON() === false, 'string.isJSON()');
113113
assert.ok('[]'.isJSON() === true, 'string.isJSON([])');
114114
assert.ok('{}'.isJSON() === true, 'string.isJSON({})');
115+
assert.ok(' {} '.isJSON() === true, 'string.isJSON({})');
115116
assert.ok('"'.isJSON() === false, 'string.isJSON(")');
116117
assert.ok('""'.isJSON() === true, 'string.isJSON("")');
117118
assert.ok('12'.isJSON() === false, 'string.isJSON(12)');
118119
assert.ok('[}'.isJSON() === false, 'string.isJSON([})');
119120
assert.ok('["'.isJSON() === false, 'string.isJSON([")');
120-
121-
assert.ok(UID().isUID(), 'string.isUID()');
121+
assert.ok(str.isJSON() === false, 'string.isJSON()');
122+
assert.ok(JSON.parse(JSON.stringify(new Date())).isJSONDate(), 'string.isJSONDate()');
122123

123124
str = 'google.sk';
124125
assert.ok(str.isURL() === false, 'string.isURL(): ' + str);

utils.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2532,6 +2532,16 @@ Date.prototype.toUTC = function(ticks) {
25322532
return ticks ? dt : new Date(dt);
25332533
};
25342534

2535+
// +v2.2.0 parses JSON dates as dates and this is the fallback for backward compatibility
2536+
Date.prototype.parseDate = function() {
2537+
return this;
2538+
};
2539+
2540+
String.prototype.isJSONDate = function() {
2541+
var l = this.length - 1;
2542+
return l > 22 && l < 30 && this[l] === 'Z' && this[10] === 'T' && this[4] === '-' && this[13] === ':' && this[16] === ':';
2543+
};
2544+
25352545
if (!String.prototype.trim) {
25362546
String.prototype.trim = function() {
25372547
return this.replace(regexpTRIM, '');
@@ -4083,7 +4093,7 @@ Array.prototype.quicksort = Array.prototype.orderBy = function(name, asc, maxlen
40834093

40844094
switch (typeof(field)) {
40854095
case 'string':
4086-
if (field.length > 20 && field[10] === 'T' && field[4] === '-')
4096+
if (field.isJSONDate())
40874097
type = 4;
40884098
else
40894099
type = 1;

0 commit comments

Comments
 (0)