-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add fallback for Date#toJSON in engines without native Date#toISOStri…
…ng, close #220
- Loading branch information
Showing
6 changed files
with
82 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); |