UNPKG

mathsteps-experimental-fork

Version:

Step by step math solutions. Experimental Fork

45 lines (36 loc) 1.36 kB
import { MyNodeToStringCache } from '../../cache.js'; import { isNode, isConstantNode, isSymbolNode } from '../../config.js'; import { makeKeyFromNode, getValueFromNodeValue } from './nodeCacher.js'; import { cleanString } from '../../util/cleanString.js'; const cache = MyNodeToStringCache; function getNode(thing) { return thing?.rootNode || thing?.node || thing?.nodeAfter || (isNode(thing) ? thing : null) } function getValueFromNode(node) { return isConstantNode(node) ? getValueFromNodeValue(node) : isSymbolNode(node) ? node.name : null } function oneItemToString(thing, options, extraOptions = {}) { const node = getNode(thing); if (!node) return null const key = makeKeyFromNode(node, extraOptions) + (options ? JSON.stringify(options) : ''); if (!extraOptions.disableCache && cache.has(key)) return cache.get(key) const value = cleanString((getValueFromNode(node) || node.toString(options ?? {}))); if (!extraOptions.disableCache) cache.set(key, value); return value } /** * @returns {string} */ function myNodeToString(unknownThing, options, extraOptions = {}) { if (typeof unknownThing !== 'object') throw new TypeError('nodeToString only accepts objects') return oneItemToString(unknownThing, options, extraOptions) } export { myNodeToString };