replacer.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createReplacer = void 0;
  6. var _factory = require("../utils/factory.js");
  7. var name = 'replacer';
  8. var dependencies = [];
  9. var createReplacer = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function () {
  10. /**
  11. * Stringify data types into their JSON representation.
  12. * Most data types can be serialized using their `.toJSON` method,
  13. * but not all, for example the number `Infinity`. For these cases you have
  14. * to use the replacer. Example usage:
  15. *
  16. * JSON.stringify([2, Infinity], math.replacer)
  17. *
  18. * @param {string} key
  19. * @param {*} value
  20. * @returns {*} Returns the replaced object
  21. */
  22. return function replacer(key, value) {
  23. // the numeric values Infinitiy, -Infinity, and NaN cannot be serialized to JSON
  24. if (typeof value === 'number' && (!isFinite(value) || isNaN(value))) {
  25. return {
  26. mathjs: 'number',
  27. value: String(value)
  28. };
  29. }
  30. return value;
  31. };
  32. });
  33. exports.createReplacer = createReplacer;