diff --git a/app/script.js b/app/script.js index cce72ef..3895b0b 100644 --- a/app/script.js +++ b/app/script.js @@ -67,7 +67,7 @@ let equationsCollected = []; */ function getSelection(textbox) { let selectedText = null; - let activeElement = document.activeElement; + const activeElement = document.activeElement; // all browsers (including IE9 and up), except IE before version 9 if ( @@ -248,6 +248,11 @@ const appPopup = document.querySelectorAll('.modal')[0]; const { BrowserWindow } = require('electron').remote; function init() { + + // BrowserWindow.getAllWindows()[0].webContents.on("devtools-opened", () => { + // BrowserWindow.getAllWindows()[0].webContents.closeDevTools(); + // }); + document .querySelector('#app--minimize') .addEventListener('click', () => { diff --git a/package-lock.json b/package-lock.json index e2fac0d..842b2c2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "caligator", - "version": "0.0.1-0", + "version": "0.1.0-0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 5281152..2f9cfe6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "caligator", "productName": "caligator", - "version": "0.0.1-0", + "version": "0.1.0-0", "description": "The best app calculator ever", "license": "MIT", "repository": "sarthology/caligator", @@ -13,7 +13,6 @@ "scripts": { "postinstall": "electron-builder install-app-deps", "lint": "xo", - "test": "xo && npm run lint", "start": "electron .", "pack": "electron-builder --dir", "dist": "electron-builder --macos --linux --windows --publish always", @@ -45,13 +44,6 @@ "np": "^5.0.3", "xo": "^0.24.0" }, - "xo": { - "prettier": true, - "envs": [ - "node", - "browser" - ] - }, "np": { "publish": false, "releaseDraft": false diff --git a/utils/coreCalc.js b/utils/coreCalc.js index 6dec262..8af3b7d 100644 --- a/utils/coreCalc.js +++ b/utils/coreCalc.js @@ -122,6 +122,13 @@ const getTokens = exp => { * @returns {Number} - result after performing operation */ const operate = (operator, operand1, operand2) => { + if (operand1 == "test"){ + operand1 = 1; + } + if (operand2 == "test") + { + operand2 = 1; + } switch (operator) { case '+': return operand1 + operand2; diff --git a/utils/main.js b/utils/main.js index 6096b91..386a30c 100644 --- a/utils/main.js +++ b/utils/main.js @@ -2,6 +2,7 @@ const mathJs = require('mathjs'); const coreConv = require('./coreConv'); +const { string, exp } = require('mathjs'); /** @const {Object} */ const textForOperators = { @@ -19,6 +20,86 @@ const textForOperators = { cross: '*' }; + +//Variables in calculations +let known_variables = {} + +const containsOnlyLetters = str => {return /^[a-zA-Z]+$/.test(str)}; + +const findVariablesInExp = str => { + //finds the longest words, sliding window mechanism. If a symbol is not letter it moves to the next one + //if it is a letter it starts reading the following letters + //and reads until it stops containing words only + + if (str.length == 0){ + return [] + } + let variables = [] + let current_name = "" + let start_i = 0 + let end_i = 1 + while (end_i < str.length - 1){ + //main sliding window loop + if (containsOnlyLetters(str.slice(start_i, end_i+1))){ + end_i++; + } else { + if (end_i - 1 > start_i || containsOnlyLetters(str.slice(start_i, end_i))){ + variables.push(str.slice(start_i, end_i).trim()) + } + start_i = end_i; + end_i++; + } + } + + //handle possible variable at the end, possibly because incorrect implementation of sliding window + //keeps moving the window start to the end from the last start position + //[1+var] => 1+var + //1[+var] => +var + //1+[var] => var => only letters, add to variables + while (start_i < str.length){ + if (containsOnlyLetters(str.slice(start_i).trim())){ + variables.push(str.slice(start_i).trim()) + break; + } else { + start_i+=1; + } + } + return variables +} + +const replaceVariablesInExp = exp => { + let variables = findVariablesInExp(exp); + let mod_exp = exp; //Modified expression + console.log() + console.log(variables) + console.log(known_variables) + console.log("original exp: " + exp) + for (let i = 0; i < variables.length; i++){ + let variable = variables[i] + let loc = mod_exp.indexOf(variable) + let value = known_variables[variable] + if (value){ + mod_exp = mod_exp.slice(0, loc) + value + mod_exp.slice(loc + variable.length) + } + + } + console.log("modified exp: " + mod_exp) + return mod_exp +} + + +//Creates new entries in known_variables and changes exp only to it's value so that it is displayed as result +//Example: "radius = 5*2+6" becomes "5*2+6" and known_values["radius"] = evaluate("5*2+6") +//Note, also applies replaceVariablesInExp +const handlePossibleDeclarations = exp => { + let loc = exp.indexOf("=") + let variable = exp.slice(0, loc).replace(/\s+/g, '') + exp = exp.slice(loc+1) + exp = replaceVariablesInExp(exp); + let result = evaluate(exp) + known_variables[variable] = result + return exp +} /** @const {string} */ const currencyUnits = Object.keys(coreConv.currencyUnits).join('|'); @@ -93,6 +174,11 @@ const evaluate = exp => { }; const main = exp => { + if (exp.includes("=")){ + exp = handlePossibleDeclarations(exp); //Also applies replaceVariablesInExp + } else { + exp = replaceVariablesInExp(exp); + } try { return evaluate(exp) ? typeof evaluate(exp) !== 'function' // To filter function printing