Skip to content

Commit

Permalink
add fallback for Date#toJSON in engines without native Date#toISOStri…
Browse files Browse the repository at this point in the history
…ng, close #220
  • Loading branch information
zloirock committed Jul 19, 2016
1 parent 5e106f6 commit 5b97fbd
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 50 deletions.
26 changes: 26 additions & 0 deletions library/modules/_date-to-iso-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
// 20.3.4.36 / 15.9.5.43 Date.prototype.toISOString()
var fails = require('./_fails')
, getTime = Date.prototype.getTime
, $toISOString = Date.prototype.toISOString;

var lz = function(num){
return num > 9 ? num : '0' + num;
};

// PhantomJS / old WebKit has a broken implementations
module.exports = (fails(function(){
return $toISOString.call(new Date(-5e13 - 1)) != '0385-07-25T07:06:39.999Z';
}) || !fails(function(){
$toISOString.call(new Date(NaN));
})) ? function toISOString(){
if(!isFinite(getTime.call(this)))throw RangeError('Invalid time value');
var d = this
, y = d.getUTCFullYear()
, m = d.getUTCMilliseconds()
, s = y < 0 ? '-' : y > 9999 ? '+' : '';
return s + ('00000' + Math.abs(y)).slice(s ? -6 : -4) +
'-' + lz(d.getUTCMonth() + 1) + '-' + lz(d.getUTCDate()) +
'T' + lz(d.getUTCHours()) + ':' + lz(d.getUTCMinutes()) +
':' + lz(d.getUTCSeconds()) + '.' + (m > 99 ? m : '0' + lz(m)) + 'Z';
} : $toISOString;
28 changes: 4 additions & 24 deletions library/modules/es6.date.to-iso-string.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
'use strict';
// 20.3.4.36 / 15.9.5.43 Date.prototype.toISOString()
var $export = require('./_export')
, fails = require('./_fails')
, getTime = Date.prototype.getTime;

var lz = function(num){
return num > 9 ? num : '0' + num;
};
var $export = require('./_export')
, toISOString = require('./_date-to-iso-string');

// PhantomJS / old WebKit has a broken implementations
$export($export.P + $export.F * (fails(function(){
return new Date(-5e13 - 1).toISOString() != '0385-07-25T07:06:39.999Z';
}) || !fails(function(){
new Date(NaN).toISOString();
})), 'Date', {
toISOString: function toISOString(){
if(!isFinite(getTime.call(this)))throw RangeError('Invalid time value');
var d = this
, y = d.getUTCFullYear()
, m = d.getUTCMilliseconds()
, s = y < 0 ? '-' : y > 9999 ? '+' : '';
return s + ('00000' + Math.abs(y)).slice(s ? -6 : -4) +
'-' + lz(d.getUTCMonth() + 1) + '-' + lz(d.getUTCDate()) +
'T' + lz(d.getUTCHours()) + ':' + lz(d.getUTCMinutes()) +
':' + lz(d.getUTCSeconds()) + '.' + (m > 99 ? m : '0' + lz(m)) + 'Z';
}
$export($export.P + $export.F * (Date.prototype.toISOString !== toISOString), 'Date', {
toISOString: toISOString
});
7 changes: 5 additions & 2 deletions library/modules/es6.date.to-json.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict';
var $export = require('./_export')
, toObject = require('./_to-object')
, toPrimitive = require('./_to-primitive');
, toPrimitive = require('./_to-primitive')
, toISOString = require('./_date-to-iso-string')
, classof = require('./_classof');

$export($export.P + $export.F * require('./_fails')(function(){
return new Date(NaN).toJSON() !== null || Date.prototype.toJSON.call({toISOString: function(){ return 1; }}) !== 1;
}), 'Date', {
toJSON: function toJSON(key){
var O = toObject(this)
, pv = toPrimitive(O);
return typeof pv == 'number' && !isFinite(pv) ? null : O.toISOString();
return typeof pv == 'number' && !isFinite(pv) ? null :
(!('toISOString' in O) && classof(O) == 'Date') ? toISOString.call(O) : O.toISOString();
}
});
26 changes: 26 additions & 0 deletions modules/_date-to-iso-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';
// 20.3.4.36 / 15.9.5.43 Date.prototype.toISOString()
var fails = require('./_fails')
, getTime = Date.prototype.getTime
, $toISOString = Date.prototype.toISOString;

var lz = function(num){
return num > 9 ? num : '0' + num;
};

// PhantomJS / old WebKit has a broken implementations
module.exports = (fails(function(){
return $toISOString.call(new Date(-5e13 - 1)) != '0385-07-25T07:06:39.999Z';
}) || !fails(function(){
$toISOString.call(new Date(NaN));
})) ? function toISOString(){
if(!isFinite(getTime.call(this)))throw RangeError('Invalid time value');
var d = this
, y = d.getUTCFullYear()
, m = d.getUTCMilliseconds()
, s = y < 0 ? '-' : y > 9999 ? '+' : '';
return s + ('00000' + Math.abs(y)).slice(s ? -6 : -4) +
'-' + lz(d.getUTCMonth() + 1) + '-' + lz(d.getUTCDate()) +
'T' + lz(d.getUTCHours()) + ':' + lz(d.getUTCMinutes()) +
':' + lz(d.getUTCSeconds()) + '.' + (m > 99 ? m : '0' + lz(m)) + 'Z';
} : $toISOString;
28 changes: 4 additions & 24 deletions modules/es6.date.to-iso-string.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
'use strict';
// 20.3.4.36 / 15.9.5.43 Date.prototype.toISOString()
var $export = require('./_export')
, fails = require('./_fails')
, getTime = Date.prototype.getTime;

var lz = function(num){
return num > 9 ? num : '0' + num;
};
var $export = require('./_export')
, toISOString = require('./_date-to-iso-string');

// PhantomJS / old WebKit has a broken implementations
$export($export.P + $export.F * (fails(function(){
return new Date(-5e13 - 1).toISOString() != '0385-07-25T07:06:39.999Z';
}) || !fails(function(){
new Date(NaN).toISOString();
})), 'Date', {
toISOString: function toISOString(){
if(!isFinite(getTime.call(this)))throw RangeError('Invalid time value');
var d = this
, y = d.getUTCFullYear()
, m = d.getUTCMilliseconds()
, s = y < 0 ? '-' : y > 9999 ? '+' : '';
return s + ('00000' + Math.abs(y)).slice(s ? -6 : -4) +
'-' + lz(d.getUTCMonth() + 1) + '-' + lz(d.getUTCDate()) +
'T' + lz(d.getUTCHours()) + ':' + lz(d.getUTCMinutes()) +
':' + lz(d.getUTCSeconds()) + '.' + (m > 99 ? m : '0' + lz(m)) + 'Z';
}
$export($export.P + $export.F * (Date.prototype.toISOString !== toISOString), 'Date', {
toISOString: toISOString
});
17 changes: 17 additions & 0 deletions modules/library/es6.date.to-json.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
var $export = require('./_export')
, toObject = require('./_to-object')
, toPrimitive = require('./_to-primitive')
, toISOString = require('./_date-to-iso-string')
, classof = require('./_classof');

$export($export.P + $export.F * require('./_fails')(function(){
return new Date(NaN).toJSON() !== null || Date.prototype.toJSON.call({toISOString: function(){ return 1; }}) !== 1;
}), 'Date', {
toJSON: function toJSON(key){
var O = toObject(this)
, pv = toPrimitive(O);
return typeof pv == 'number' && !isFinite(pv) ? null :
(!('toISOString' in O) && classof(O) == 'Date') ? toISOString.call(O) : O.toISOString();
}
});

0 comments on commit 5b97fbd

Please sign in to comment.