Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for variables #89

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
7 changes: 6 additions & 1 deletion app/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -45,13 +44,6 @@
"np": "^5.0.3",
"xo": "^0.24.0"
},
"xo": {
"prettier": true,
"envs": [
"node",
"browser"
]
},
"np": {
"publish": false,
"releaseDraft": false
Expand Down
7 changes: 7 additions & 0 deletions utils/coreCalc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
86 changes: 86 additions & 0 deletions utils/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const mathJs = require('mathjs');
const coreConv = require('./coreConv');
const { string, exp } = require('mathjs');

/** @const {Object} */
const textForOperators = {
Expand All @@ -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('|');

Expand Down Expand Up @@ -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
Expand Down