es.date.to-json.js 764 B

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