From 2471a4f37fcae3f65e9d7ffc9b484ca473ea523d Mon Sep 17 00:00:00 2001 From: Bastioner Date: Mon, 30 Nov 2020 11:03:45 +0100 Subject: [PATCH 01/12] Autoclose DevTools. Maybe bad for debug but REALLY good for normal usage. --- app/script.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/script.js b/app/script.js index cce72ef..ffb3769 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', () => { From 6137bfc766794abe97339589164b67b3ea2f0df8 Mon Sep 17 00:00:00 2001 From: Bastioner Date: Wed, 2 Dec 2020 12:24:59 +0100 Subject: [PATCH 02/12] Added replacing variables in expression. Example: radius+2 becomes (value of radius)+2 => 8.14+2 Currently variables only defined in code in main.js/known_variables --- app/script.js | 6 ++--- utils/coreCalc.js | 7 ++++++ utils/main.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 3 deletions(-) diff --git a/app/script.js b/app/script.js index ffb3769..3895b0b 100644 --- a/app/script.js +++ b/app/script.js @@ -249,9 +249,9 @@ const appPopup = document.querySelectorAll('.modal')[0]; function init() { - BrowserWindow.getAllWindows()[0].webContents.on("devtools-opened", () => { - BrowserWindow.getAllWindows()[0].webContents.closeDevTools(); - }); + // BrowserWindow.getAllWindows()[0].webContents.on("devtools-opened", () => { + // BrowserWindow.getAllWindows()[0].webContents.closeDevTools(); + // }); document .querySelector('#app--minimize') 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..96115f1 100644 --- a/utils/main.js +++ b/utils/main.js @@ -2,6 +2,7 @@ const mathJs = require('mathjs'); const coreConv = require('./coreConv'); +const { string } = require('mathjs'); /** @const {Object} */ const textForOperators = { @@ -19,6 +20,65 @@ const textForOperators = { cross: '*' }; + +//Variables in calculations +let known_variables = { + "radius": 8.14, + +} + +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 variable_indexes = [] + let current_name = "" + let start_i = 0 + let end_i = 1 + while (end_i < str.length){ + + if (containsOnlyLetters(str.slice(start_i, end_i))){ + end_i++; + current_name = str.slice(start_i, end_i) + } else { + console.log("pushing") + if (end_i - 1 > start_i){ + variable_indexes.push([[start_i, end_i-1]]) + } + start_i = end_i; + end_i++; + } + } + if (containsOnlyLetters(str.slice(start_i, end_i))){ + variable_indexes.push([[start_i, end_i]]) + } + return variable_indexes +} + +const replaceVariablesInExp = exp => { + let variable_indexes = findVariablesInExp(exp); + console.log("Variable indexes: " + variable_indexes.length) + console.log(variable_indexes) + let mod_exp = exp; //Modified expression + for (let i = 0; i < variable_indexes.length; i++){ + let index_pair = variable_indexes[i][0] + let variable = exp.slice(index_pair[0], index_pair[1]) + let value = known_variables[variable] + console.log(variable) + console.log(value) + mod_exp = mod_exp.slice(0, index_pair[0]) + value + mod_exp.slice(index_pair[1]) + console.log("Modified exp: " + mod_exp) + } + return mod_exp + console.log("----------------") +} + /** @const {string} */ const currencyUnits = Object.keys(coreConv.currencyUnits).join('|'); @@ -93,6 +153,7 @@ const evaluate = exp => { }; const main = exp => { + exp = replaceVariablesInExp(exp); try { return evaluate(exp) ? typeof evaluate(exp) !== 'function' // To filter function printing From fef76ee64257d828ae06b46e84d18f49ae6c3020 Mon Sep 17 00:00:00 2001 From: Bastioner Date: Thu, 3 Dec 2020 14:15:36 +0100 Subject: [PATCH 03/12] Refactoring and bugfixing in progress --- utils/main.js | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/utils/main.js b/utils/main.js index 96115f1..36e1c2b 100644 --- a/utils/main.js +++ b/utils/main.js @@ -2,7 +2,7 @@ const mathJs = require('mathjs'); const coreConv = require('./coreConv'); -const { string } = require('mathjs'); +const { string, exp } = require('mathjs'); /** @const {Object} */ const textForOperators = { @@ -69,16 +69,32 @@ const replaceVariablesInExp = exp => { for (let i = 0; i < variable_indexes.length; i++){ let index_pair = variable_indexes[i][0] let variable = exp.slice(index_pair[0], index_pair[1]) - let value = known_variables[variable] - console.log(variable) - console.log(value) - mod_exp = mod_exp.slice(0, index_pair[0]) + value + mod_exp.slice(index_pair[1]) + let value = known_variables[variable] || variable + console.log(variable + "-" + value + " index pair:") + console.log(index_pair) + + mod_exp = mod_exp.slice(0, index_pair[0]+1) + value + mod_exp.slice(index_pair[1]) console.log("Modified exp: " + mod_exp) } return mod_exp console.log("----------------") } + +//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) + console.log(variable + " - " + result) + known_variables[variable] = result + console.log(known_variables) + return exp +} /** @const {string} */ const currencyUnits = Object.keys(coreConv.currencyUnits).join('|'); @@ -153,7 +169,12 @@ const evaluate = exp => { }; const main = exp => { - exp = replaceVariablesInExp(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 From 1acccfdb28803ffc788d81528975a2dbaf76f97c Mon Sep 17 00:00:00 2001 From: Bastioner Date: Thu, 3 Dec 2020 17:38:20 +0100 Subject: [PATCH 04/12] Likely working version --- utils/main.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/utils/main.js b/utils/main.js index 36e1c2b..20d109c 100644 --- a/utils/main.js +++ b/utils/main.js @@ -37,7 +37,7 @@ const findVariablesInExp = str => { if (str.length == 0){ return [] } - let variable_indexes = [] + let variables = [] let current_name = "" let start_i = 0 let end_i = 1 @@ -49,32 +49,33 @@ const findVariablesInExp = str => { } else { console.log("pushing") if (end_i - 1 > start_i){ - variable_indexes.push([[start_i, end_i-1]]) + variables.push(str.slice(start_i, end_i).trim()) } start_i = end_i; end_i++; } } if (containsOnlyLetters(str.slice(start_i, end_i))){ - variable_indexes.push([[start_i, end_i]]) + variables.push(str.slice(start_i, end_i).trim()) } - return variable_indexes + return variables } const replaceVariablesInExp = exp => { - let variable_indexes = findVariablesInExp(exp); - console.log("Variable indexes: " + variable_indexes.length) - console.log(variable_indexes) + let variables = findVariablesInExp(exp); + console.log("variables") + console.log(variables) let mod_exp = exp; //Modified expression - for (let i = 0; i < variable_indexes.length; i++){ - let index_pair = variable_indexes[i][0] - let variable = exp.slice(index_pair[0], index_pair[1]) - let value = known_variables[variable] || variable - console.log(variable + "-" + value + " index pair:") - console.log(index_pair) + 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){ + console.log(variable + "-" + value + " index pair:") + mod_exp = mod_exp.slice(0, loc) + value + mod_exp.slice(loc + variable.length) + console.log("Modified exp: " + mod_exp) + } - mod_exp = mod_exp.slice(0, index_pair[0]+1) + value + mod_exp.slice(index_pair[1]) - console.log("Modified exp: " + mod_exp) } return mod_exp console.log("----------------") From fc17ad9e5e5d31ed3256fbd9476b09d3a83e94e5 Mon Sep 17 00:00:00 2001 From: Bastioner Date: Fri, 4 Dec 2020 10:10:00 +0100 Subject: [PATCH 05/12] Stopped tracking package.json, because I had to remove error checking because there were too much errors (Warnings, that prevent releasing). It's temporary but I don't know what else to do --- package.json | 87 ---------------------------------------------------- 1 file changed, 87 deletions(-) delete mode 100644 package.json diff --git a/package.json b/package.json deleted file mode 100644 index 5281152..0000000 --- a/package.json +++ /dev/null @@ -1,87 +0,0 @@ -{ - "name": "caligator", - "productName": "caligator", - "version": "0.0.1-0", - "description": "The best app calculator ever", - "license": "MIT", - "repository": "sarthology/caligator", - "author": { - "name": "Sarthology", - "email": "dummy@mail.com", - "url": "https://sarthak.it" - }, - "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", - "release": "np" - }, - "husky": { - "hooks": { - "pre-commit": "lint-staged" - } - }, - "lint-staged": { - "{*,app}.js": "npm run lint" - }, - "dependencies": { - "axios": "^0.19.0", - "electron-context-menu": "^0.12.1", - "electron-debug": "^3.0.0", - "electron-store": "^3.3.0", - "electron-unhandled": "^2.2.0", - "electron-updater": "^4.0.6", - "electron-util": "^0.12.0", - "mathjs": "^6.2.2" - }, - "devDependencies": { - "electron": "4.0.1", - "electron-builder": "^20.43.0", - "husky": "^3.0.9", - "lint-staged": "^9.4.2", - "np": "^5.0.3", - "xo": "^0.24.0" - }, - "xo": { - "prettier": true, - "envs": [ - "node", - "browser" - ] - }, - "np": { - "publish": false, - "releaseDraft": false - }, - "build": { - "appId": "com.sarthak.caligator", - "mac": { - "category": "public.app-category.utility" - }, - "dmg": { - "iconSize": 160, - "contents": [ - { - "x": 180, - "y": 170 - }, - { - "x": 480, - "y": 170, - "type": "link", - "path": "/Applications" - } - ] - }, - "linux": { - "target": [ - "AppImage", - "deb" - ], - "category": "Utility" - } - } -} From 48da680acd0d16cfc8402eaab6f5b39567dadd0a Mon Sep 17 00:00:00 2001 From: Bastioner Date: Fri, 4 Dec 2020 10:12:42 +0100 Subject: [PATCH 06/12] Okay, so I have to track it otherwise, I cannot compile either because of unclean working tree. I really don't understand this js stuff --- package.json | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..c7a21c3 --- /dev/null +++ b/package.json @@ -0,0 +1,80 @@ +{ + "name": "caligator", + "productName": "caligator", + "version": "0.0.1-0", + "description": "The best app calculator ever", + "license": "MIT", + "repository": "sarthology/caligator", + "author": { + "name": "Sarthology", + "email": "dummy@mail.com", + "url": "https://sarthak.it" + }, + "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", + "release": "np" + }, + "husky": { + "hooks": { + "pre-commit": "lint-staged" + } + }, + "lint-staged": { + "{*,app}.js": "npm run lint" + }, + "dependencies": { + "axios": "^0.19.0", + "electron-context-menu": "^0.12.1", + "electron-debug": "^3.0.0", + "electron-store": "^3.3.0", + "electron-unhandled": "^2.2.0", + "electron-updater": "^4.0.6", + "electron-util": "^0.12.0", + "mathjs": "^6.2.2" + }, + "devDependencies": { + "electron": "4.0.1", + "electron-builder": "^20.43.0", + "husky": "^3.0.9", + "lint-staged": "^9.4.2", + "np": "^5.0.3", + "xo": "^0.24.0" + }, + "np": { + "publish": false, + "releaseDraft": false + }, + "build": { + "appId": "com.sarthak.caligator", + "mac": { + "category": "public.app-category.utility" + }, + "dmg": { + "iconSize": 160, + "contents": [ + { + "x": 180, + "y": 170 + }, + { + "x": 480, + "y": 170, + "type": "link", + "path": "/Applications" + } + ] + }, + "linux": { + "target": [ + "AppImage", + "deb" + ], + "category": "Utility" + } + } +} From 7009d2265b3d37d3fb9b081a776f26877908cddc Mon Sep 17 00:00:00 2001 From: Bastioner Date: Fri, 4 Dec 2020 10:15:10 +0100 Subject: [PATCH 07/12] Removing lint check at a second place --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index c7a21c3..141f754 100644 --- a/package.json +++ b/package.json @@ -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", From 11a5af3d1a94b5da3fab47349db6e6ba5e9f17a4 Mon Sep 17 00:00:00 2001 From: Bastioner Date: Fri, 4 Dec 2020 10:16:05 +0100 Subject: [PATCH 08/12] 0.1.0-0 --- package-lock.json | 2 +- package.json | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) 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 141f754..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", @@ -19,9 +19,9 @@ "release": "np" }, "husky": { - "hooks": { - "pre-commit": "lint-staged" - } + "hooks": { + "pre-commit": "lint-staged" + } }, "lint-staged": { "{*,app}.js": "npm run lint" From 9eaceeed1b6dcd2341992344cf9512391bfd6ffe Mon Sep 17 00:00:00 2001 From: BastionInCzech Date: Sat, 5 Dec 2020 19:20:26 +0100 Subject: [PATCH 09/12] Removed a default variable --- utils/main.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/utils/main.js b/utils/main.js index 20d109c..642a613 100644 --- a/utils/main.js +++ b/utils/main.js @@ -22,10 +22,7 @@ const textForOperators = { //Variables in calculations -let known_variables = { - "radius": 8.14, - -} +let known_variables = {} const containsOnlyLetters = str => {return /^[a-zA-Z]+$/.test(str)}; From f17a8f74b40a95ac4da26571996a69e3652b7344 Mon Sep 17 00:00:00 2001 From: BastionInCzech Date: Sat, 5 Dec 2020 19:30:43 +0100 Subject: [PATCH 10/12] Removed debug logs --- utils/main.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/utils/main.js b/utils/main.js index 642a613..9d8b620 100644 --- a/utils/main.js +++ b/utils/main.js @@ -44,7 +44,7 @@ const findVariablesInExp = str => { end_i++; current_name = str.slice(start_i, end_i) } else { - console.log("pushing") + if (end_i - 1 > start_i){ variables.push(str.slice(start_i, end_i).trim()) } @@ -60,22 +60,17 @@ const findVariablesInExp = str => { const replaceVariablesInExp = exp => { let variables = findVariablesInExp(exp); - console.log("variables") - console.log(variables) let mod_exp = exp; //Modified expression 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){ - console.log(variable + "-" + value + " index pair:") mod_exp = mod_exp.slice(0, loc) + value + mod_exp.slice(loc + variable.length) - console.log("Modified exp: " + mod_exp) } } return mod_exp - console.log("----------------") } @@ -88,9 +83,7 @@ const handlePossibleDeclarations = exp => { exp = exp.slice(loc+1) exp = replaceVariablesInExp(exp); let result = evaluate(exp) - console.log(variable + " - " + result) known_variables[variable] = result - console.log(known_variables) return exp } /** @const {string} */ From d7216139088b218efcedaeccf3f5a5ca5b27654c Mon Sep 17 00:00:00 2001 From: Bastioner Date: Mon, 7 Dec 2020 10:57:12 +0100 Subject: [PATCH 11/12] Fixed a bug when variables needed a space after them --- utils/main.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/utils/main.js b/utils/main.js index 9d8b620..e0dff83 100644 --- a/utils/main.js +++ b/utils/main.js @@ -38,13 +38,11 @@ const findVariablesInExp = str => { let current_name = "" let start_i = 0 let end_i = 1 - while (end_i < str.length){ + while (end_i < str.length - 1){ - if (containsOnlyLetters(str.slice(start_i, end_i))){ + if (containsOnlyLetters(str.slice(start_i, end_i+1))){ end_i++; - current_name = str.slice(start_i, end_i) } else { - if (end_i - 1 > start_i){ variables.push(str.slice(start_i, end_i).trim()) } @@ -52,8 +50,8 @@ const findVariablesInExp = str => { end_i++; } } - if (containsOnlyLetters(str.slice(start_i, end_i))){ - variables.push(str.slice(start_i, end_i).trim()) + if (containsOnlyLetters(str.slice(start_i))){ + variables.push(str.slice(start_i).trim()) } return variables } @@ -165,7 +163,6 @@ const main = exp => { } else { exp = replaceVariablesInExp(exp); } - try { return evaluate(exp) ? typeof evaluate(exp) !== 'function' // To filter function printing From 774e4a1f8225aaa2e1344aa8d115a444228837c1 Mon Sep 17 00:00:00 2001 From: BastionInCzech Date: Tue, 8 Dec 2020 00:12:13 +0100 Subject: [PATCH 12/12] Fixed one letter variable bug --- utils/main.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/utils/main.js b/utils/main.js index e0dff83..386a30c 100644 --- a/utils/main.js +++ b/utils/main.js @@ -39,19 +39,30 @@ const findVariablesInExp = str => { 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){ + 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++; } } - if (containsOnlyLetters(str.slice(start_i))){ - variables.push(str.slice(start_i).trim()) + + //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 } @@ -59,6 +70,10 @@ const findVariablesInExp = str => { 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) @@ -68,6 +83,7 @@ const replaceVariablesInExp = exp => { } } + console.log("modified exp: " + mod_exp) return mod_exp }