mathjs/examples/code editor/getExpressions.js
David Contreras 8abac840e7
CodeEditorExample (#3027)
* broadcasting

* Simplified broadcasting

* Updated for broadcasting

* Changed to camel case

* Camel case and auto formating

* Added comments

* Skip if matrices have the same size

* Fixed issue with undefined variable

missing dot  in `A._size`

* Implemented broadcasting in all functions

* Added helper functions

* Added function to check for broadcasting rules

* Tests for broadcasted arithmetic

* Fixed issue with matrix the size of a vector

* Documented and updated broadcasting

* Included broadcast.test

* Included math to syntax when missing

* Add code editor example

* Vite mini project

* Initial example

* added alpine debounce

* Fixed display

* Added parser.clear

* Added mathjs-language

* Made module to get expressions

* Added custom events

* Issue with help formatting

* Simplified help format

* Restored package.json

* removed unneded icons

* Added readme file

* Fixed versions

* Commented getExpressions

* Documented main.js

* Fixed title

* Fixed alpine version

* Removed AlpineJS

* Added documentation and renamed variables for clarity

* Fixed naming errors

---------

Co-authored-by: David Contreras <david.contreras@guentner.com>
Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
2024-01-17 10:52:25 +01:00

59 lines
2.1 KiB
JavaScript

import { parse } from 'mathjs'
/**
* Extracts parsable expressions from a multiline string.
*
* @param {string} str - The multiline string containing expressions.
* @returns {Array<{from: number, to: number, source: string}>} An array of objects,
* where each object represents a parsable expression and contains:
* - from: The starting line number of the expression within the original string.
* - to: The ending line number of the expression within the original string.
* - source: The actual string content of the expression.
*/
export default function getExpressions(str) {
const lines = str.split('\n');
let nextLineToParse = 0;
const result = [];
for (let lineID = 0; lineID < lines.length; lineID++) {
const linesToTest = lines.slice(nextLineToParse, lineID + 1).join('\n');
if (canBeParsed(linesToTest)) {
if (!isEmptyString(linesToTest)) {
result.push({ from: nextLineToParse, to: lineID, source: linesToTest });
}
// Start the next parsing attempt from the line after the successfully parsed expression.
nextLineToParse = lineID + 1;
}
}
// Handle any remaining lines that couldn't be parsed as expressions.
const linesToTest = lines.slice(nextLineToParse).join('\n');
if (!isEmptyString(linesToTest)) {
result.push({ from: nextLineToParse, to: lines.length - 1, source: linesToTest });
}
return result;
}
/**
* Determines whether a given expression can be successfully parsed.
*
* @param {string} expression - The expression to parse.
* @returns {boolean} True if the expression can be parsed, false otherwise.
*/
function canBeParsed(expression) {
try {
parse(expression)
return true
} catch (error) {
return false
}
}
/**
* Checks if a given string is empty or only contains whitespace characters.
*
* @param {string} str - The string to check.
* @returns {boolean} True if the string is empty or only contains whitespace, false otherwise.
*/
function isEmptyString(str) {
return str.trim() === ""
}