UNPKG

785 BJavaScriptView Raw
1'use strict'
2
3function factory (type, config, load, typed, math) {
4 /**
5 * Instantiate mathjs data types from their JSON representation
6 * @param {string} key
7 * @param {*} value
8 * @returns {*} Returns the revived object
9 */
10 return function reviver (key, value) {
11 const constructor = type[value && value.mathjs] ||
12 (math.expression && math.expression.node[value && value.mathjs])
13 // TODO: instead of checking math.expression.node, expose all Node classes on math.type too
14
15 if (constructor && typeof constructor.fromJSON === 'function') {
16 return constructor.fromJSON(value)
17 }
18
19 return value
20 }
21}
22
23exports.name = 'reviver'
24exports.path = 'json'
25exports.factory = factory
26exports.math = true // request the math namespace as fifth argument