UNPKG

mathsteps-experimental-fork

Version:

Step by step math solutions. Experimental Fork

31 lines (26 loc) 1.49 kB
'use strict'; Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); const myNodeToString = require('./myNodeToString.js'); const nodeHelpers = require('./nodeHelpers.js'); const parseText = require('./parseText.js'); function _tryParseTextTermToString(terms) { const cleanStrFn = (str) => str.replace(/\s+/g, "").replace(/\+\-/g, "-").replace(/\-\+/g, "-").replace(/\-\-/g, "+").replace(/\+\+/g, "+"); const stringTerm = nodeHelpers.termToString(terms); try { return parseText.parseText(cleanStrFn(stringTerm)); } catch (e) { console.log("error in tryTermToString", { stringTerm, terms, e }); return null; } } function removeUnnecessaryParentheses(expression) { const node = typeof expression === "string" ? parseText.parseText(expression) : expression; const expressionAsString = typeof expression === "string" ? expression : myNodeToString.myNodeToString(node); const functions = ["sqrt", "log", "ln", "sin", "cos", "tan", "cot", "sec", "csc", "asin", "acos", "atan", "acot", "asec", "acsc", "sinh", "cosh", "tanh", "coth", "sech", "csch", "asinh", "acosh", "atanh", "acoth", "asech", "acsch"]; if (functions.some((f) => expressionAsString.toLowerCase().includes(f))) return node; const terms = nodeHelpers.flattenAndIndexTrackAST(node).sort((a, b) => a.index - b.index); const newNode = _tryParseTextTermToString(terms); return !newNode ? node : newNode; } exports.removeUnnecessaryParentheses = removeUnnecessaryParentheses;