es.date.to-json.js 945 B

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