(math)
| 6 | * @param {Object} math A mathjs instance |
| 7 | */ |
| 8 | export function mathjsLang(math) { |
| 9 | function wordRegexp(words) { |
| 10 | return new RegExp('^((' + words.join(')|(') + '))\\b') |
| 11 | } |
| 12 | |
| 13 | const singleOperators = new RegExp("^[-+*/&|^~<>!%']") |
| 14 | const singleDelimiters = new RegExp('^[([{},:=;.?]') |
| 15 | const doubleOperators = new RegExp('^((==)|(!=)|(<=)|(>=)|(<<)|(>>)|(\\.[-+*/^]))') |
| 16 | const doubleDelimiters = new RegExp('^((!=)|(^\\|))') |
| 17 | const tripleDelimiters = new RegExp('^((>>>)|(<<<))') |
| 18 | const expressionEnd = new RegExp('^[\\])]') |
| 19 | const identifiers = new RegExp('^[_A-Za-z\xa1-\uffff][_A-Za-z0-9\xa1-\uffff]*') |
| 20 | |
| 21 | const mathFunctions = [] |
| 22 | const mathPhysicalConstants = [] |
| 23 | const mathIgnore = ['expr', 'type'] |
| 24 | const numberLiterals = [ |
| 25 | 'e', |
| 26 | 'E', |
| 27 | 'i', |
| 28 | 'Infinity', |
| 29 | 'LN2', |
| 30 | 'LN10', |
| 31 | 'LOG2E', |
| 32 | 'LOG10E', |
| 33 | 'NaN', |
| 34 | 'null', |
| 35 | 'phi', |
| 36 | 'pi', |
| 37 | 'PI', |
| 38 | 'SQRT1_2', |
| 39 | 'SQRT2', |
| 40 | 'tau', |
| 41 | 'undefined', |
| 42 | 'version' |
| 43 | ] |
| 44 | |
| 45 | // based on https://github.com/josdejong/mathjs/blob/develop/bin/cli.js |
| 46 | for (const expr in math.expression.mathWithTransform) { |
| 47 | if (!mathIgnore.includes(expr)) { |
| 48 | if (typeof math[expr] === 'function') { |
| 49 | mathFunctions.push(expr) |
| 50 | } else if (!numberLiterals.includes(expr)) { |
| 51 | mathPhysicalConstants.push(expr) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // generates a list of all valid units in mathjs |
| 57 | const listOfUnits = [] |
| 58 | for (const unit in math.Unit.UNITS) { |
| 59 | for (const prefix in math.Unit.UNITS[unit].prefixes) { |
| 60 | listOfUnits.push(prefix + unit) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | const builtins = wordRegexp(mathFunctions) |
| 65 |
no test coverage detected
searching dependent graphs…