Debian Codegraph @

app/engine/formula.js

raw symbols SCIP
1 /*
2 * Copyright (C) 2017-2019 UBports
3 * Copyright (C) 2014 Canonical Ltd
4 *
5 * This file is part of Lomiri Calculator App
6 *
7 * Lomiri Calculator App is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 3 as
9 * published by the Free Software Foundation.
10 *
11 * Lomiri Calculator App is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 .pragma library
21
22 /**
23 * Function which will delete last element in the formula
24 * It could be literal, operator, const (eg. "pi") or function (eg. "sin(" )
25 *
26 * @param bool isLastCalculate: if in the textfield there is the result of a
27 * calc this is true and the function delete all
28 * @param string formulaToCheck: the formula in the textfield
29 * @return a string with the new formula
30 */
31 function deleteLastFormulaElement(isLastCalculate, formulaToCheck) {
32 if (isLastCalculate === true) {
33 return '';
34 }
35
36 if (formulaToCheck !== '') {
37 // We choose how many chars remove checking if in the end of the string
38 // there is a special operation. Default: 1
39 var removeSize = 1;
40
41 // 5 chars: sqrt(, asin(, acos(, atan(
42 if (formulaToCheck.slice(-5) === 'sqrt(' ||
43 formulaToCheck.slice(-5) === 'asin(' ||
44 formulaToCheck.slice(-5) === 'acos(' ||
45 formulaToCheck.slice(-5) === 'atan(') {
46 removeSize = 5;
47 }
48 // 4 chars: log(, exp(, sin(, cos(, tan(, abs(
49 else if (formulaToCheck.slice(-4) === 'log(' ||
50 formulaToCheck.slice(-4) === 'exp(' ||
51 formulaToCheck.slice(-4) === 'sin(' ||
52 formulaToCheck.slice(-4) === 'cos(' ||
53 formulaToCheck.slice(-4) === 'tan(' ||
54 formulaToCheck.slice(-4) === 'abs(') {
55 removeSize = 4;
56
57 }
58 // 2 chars: pi
59 else if (formulaToCheck.slice(-2) === 'pi') {
60 removeSize = 2;
61 }
62 formulaToCheck = formulaToCheck.substring(0, formulaToCheck.length - removeSize);
63 }
64
65 return formulaToCheck;
66 }
67
68
69 /**
70 * Function to check if a char is an operator
71 *
72 * @param char digit: the digit to check if is a valid operator
73 * @return bool that indicates if the digit is a valid operator
74 */
75 function isOperator(digit) {
76 switch(digit) {
77 case "+":
78 case "-":
79 case "*":
80 case "/":
81 case "%":
82 case "^":
83 case "!":
84 return true;
85 default:
86 return false;
87 }
88 }
89
90 function validateStringForAddingToFormula(formula, stringToAddToFormula) {
91 // We are checking first character to validate strings with number eg. "^3"
92 if (isOperator(stringToAddToFormula[0])) {
93 return couldAddOperator(formula, stringToAddToFormula[0]);
94 }
95
96 // After decimal separator only number is allowed
97 if (isNaN(stringToAddToFormula)) {
98 if (formula.slice(-1) === ".") {
99 return false;
100 }
101 }
102
103 // We are ckecking last char, because we want to handle "0." string
104 if (stringToAddToFormula.slice(-1) === ".") {
105 return couldAddDot(formula);
106 }
107
108 if (stringToAddToFormula === ")") {
109 return couldAddCloseBracket(formula);
110 }
111
112 // Validate complex numbers
113 if ((stringToAddToFormula === "i") || (!isNaN(stringToAddToFormula))){
114 if (formula.slice(-1) === "i") {
115 return false;
116 }
117 }
118 return true;
119 }
120
121 /**
122 * Function to check if could be add a close bracket at the end of a formula
123 *
124 * @param string formulaToCheck: the formula where we have to add the close bracket
125 * @return bool: true if the close bracket could be added, false otherwhise
126 */
127 function couldAddCloseBracket(formulaToCheck) {
128 // Don't close a bracket just after opened it
129 if (formulaToCheck.slice(-1) === "(") {
130 return false;
131 }
132
133 // Calculate how many brackets are opened
134 var numberOfOpenedBrackets = (formulaToCheck.match(/\(/g) || []).length -
135 (formulaToCheck.match(/\)/g) || []).length;
136
137 if (numberOfOpenedBrackets < 1) {
138 return false;
139 }
140
141 return true;
142 }
143
144 /**
145 * Function to determine what bracket needs to be added after press
146 * universal bracket
147 * @param string formulaToCheck: formula which will be analysed
148 * @return a string contains bracket to add
149 */
150 function determineBracketTypeToAdd(formulaToCheck) {
151 if (formulaToCheck === '') {
152 return "(";
153 }
154 var lastChar = formulaToCheck.substring(formulaToCheck.length - 1, formulaToCheck.length);
155
156 if (isNaN(lastChar) && lastChar !== ")" && lastChar !== "i" && lastChar !== "E" && lastChar !== "!") {
157 return "(";
158 } else if (couldAddCloseBracket(formulaToCheck) === true) {
159 return ")";
160 }
161
162 return "*("
163 }
164
165 /**
166 * Function to replace some chars in the visual textfield
167 *
168 * @param string engineFormulaToConvert: the string where we have to replace chars
169 * @return a string based on param with changes in chars
170 */
171 function returnFormulaToDisplay(engineFormulaToConvert, i18n, decimalPoint) {
172 // The deletion of " is necessary for MathJs.format function - it returns a
173 // string surrounded by ", and they're useless, so we remove them
174 var engineToVisualMap = {
175 '-': '−',
176 '\\/': '÷',
177 '\\*': '×',
178 '\\.': decimalPoint,
179 'NaN': i18n.tr("NaN"),
180 'E': 'ℯ',
181 'Infinity': '∞',
182 '"': '',
183 ' ': ''
184 }
185
186 if (engineFormulaToConvert !== undefined) {
187 for (var engineElement in engineToVisualMap) {
188 var regExp = new RegExp(engineElement, 'g');
189 engineFormulaToConvert = engineFormulaToConvert.replace(regExp, engineToVisualMap[engineElement]);
190 }
191 } else {
192 engineFormulaToConvert = '';
193 }
194
195 return engineFormulaToConvert;
196 }
197
198 /**
199 * Function to check if an operator could be added to the formula
200 *
201 * @param char operatorToAdd: the operator that will be added to the formula
202 * @param string formulaToCheck: the actual formula
203 * @return bool: true if the operator could be added, false otherwise
204 */
205 function couldAddOperator(formulaToCheck, operatorToAdd) {
206 // No two operators one after other, except factorial operator
207 if (isOperator(formulaToCheck.slice(-1)) && formulaToCheck.slice(-1) !== "!") {
208 // But a minus after every operator except + and - is allowed
209 if (!(operatorToAdd === "-" &&
210 formulaToCheck.slice(-1) !== "+" &&
211 formulaToCheck.slice(-1) !== "-")) {
212 return false;
213 }
214 }
215
216 // No operator after a dot
217 if (formulaToCheck.slice(-1) === ".") {
218 return false;
219 }
220
221 // No operator after an open brackets, but minus
222 if (formulaToCheck.slice(-1) === "(" && operatorToAdd !== "-") {
223 return false;
224 }
225
226 // No operator at beginning (but minus)
227 if (formulaToCheck === "" && operatorToAdd !== "-") {
228 return false;
229 }
230
231 return true;
232 }
233
234 /**
235 * Function to check if could be add a dot at the end of a formula
236 *
237 * @param string formulaToCheck: the formula where we have to add the dot
238 * @return bool: true if the dot could be added, false otherwhise
239 */
240 function couldAddDot(formulaToCheck) {
241 // If is after a number and it's the first dot of the calc it could be added
242 if (formulaToCheck.indexOf('.') === -1) {
243 return true;
244 }
245
246 // If there is already a dot we have to check if it isn't in the same operation
247 // So we take all the string since the last occurence of dot to the end
248 var lastOperation = formulaToCheck.substring(formulaToCheck.lastIndexOf('.') + 1);
249
250 // If there isn't something different from a number we can't add a dot
251 if (!isNaN(lastOperation)) {
252 return false;
253 }
254
255 // If the only thing different from a number is a pi we cannot add a dot
256 if (lastOperation.indexOf('pi') !== -1) {
257 if (!isNaN(lastOperation.replace('pi', ''))) {
258 return false;
259 }
260 }
261
262 return true;
263 }
264