es.date.to-json.js 1008 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var call = require('../internals/function-call');
  4. var toObject = require('../internals/to-object');
  5. var toPrimitive = require('../internals/to-primitive');
  6. var toISOString = require('../internals/date-to-iso-string');
  7. var classof = require('../internals/classof-raw');
  8. var fails = require('../internals/fails');
  9. var FORCED = fails(function () {
  10. return new Date(NaN).toJSON() !== null
  11. || call(Date.prototype.toJSON, { toISOString: function () { return 1; } }) !== 1;
  12. });
  13. // `Date.prototype.toJSON` method
  14. // https://tc39.es/ecma262/#sec-date.prototype.tojson
  15. $({ target: 'Date', proto: true, forced: FORCED }, {
  16. // eslint-disable-next-line no-unused-vars -- required for `.length`
  17. toJSON: function toJSON(key) {
  18. var O = toObject(this);
  19. var pv = toPrimitive(O, 'number');
  20. return typeof pv == 'number' && !isFinite(pv) ? null :
  21. (!('toISOString' in O) && classof(O) === 'Date') ? call(toISOString, O) : O.toISOString();
  22. }
  23. });