UNPKG

605 BJavaScriptView Raw
1import { factory } from '../utils/factory'
2
3const name = 'reviver'
4const dependencies = [
5 'classes'
6]
7
8export const createReviver = /* #__PURE__ */ factory(name, dependencies, ({ classes }) => {
9 /**
10 * Instantiate mathjs data types from their JSON representation
11 * @param {string} key
12 * @param {*} value
13 * @returns {*} Returns the revived object
14 */
15 return function reviver (key, value) {
16 const constructor = classes[value && value.mathjs]
17
18 if (constructor && typeof constructor.fromJSON === 'function') {
19 return constructor.fromJSON(value)
20 }
21
22 return value
23 }
24})