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