@fileverse-dev/formula-parser 0.2.5-tally-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +261 -0
- package/es/error.js +45 -0
- package/es/evaluate-by-operator/evaluate-by-operator.js +70 -0
- package/es/evaluate-by-operator/operator/add.js +16 -0
- package/es/evaluate-by-operator/operator/ampersand.js +10 -0
- package/es/evaluate-by-operator/operator/divide.js +21 -0
- package/es/evaluate-by-operator/operator/equal.js +5 -0
- package/es/evaluate-by-operator/operator/formula-function.js +40 -0
- package/es/evaluate-by-operator/operator/greater-than-or-equal.js +5 -0
- package/es/evaluate-by-operator/operator/greater-than.js +5 -0
- package/es/evaluate-by-operator/operator/less-than-or-equal.js +5 -0
- package/es/evaluate-by-operator/operator/less-than.js +5 -0
- package/es/evaluate-by-operator/operator/minus.js +16 -0
- package/es/evaluate-by-operator/operator/multiply.js +16 -0
- package/es/evaluate-by-operator/operator/not-equal.js +5 -0
- package/es/evaluate-by-operator/operator/power.js +11 -0
- package/es/grammar-parser/grammar-parser.jison +230 -0
- package/es/grammar-parser/grammar-parser.js +1522 -0
- package/es/helper/cell.js +117 -0
- package/es/helper/number.js +25 -0
- package/es/helper/string.js +13 -0
- package/es/index.js +5 -0
- package/es/parser.js +318 -0
- package/es/supported-formulas.js +3 -0
- package/lib/error.js +53 -0
- package/lib/evaluate-by-operator/evaluate-by-operator.js +77 -0
- package/lib/evaluate-by-operator/operator/add.js +23 -0
- package/lib/evaluate-by-operator/operator/ampersand.js +17 -0
- package/lib/evaluate-by-operator/operator/divide.js +28 -0
- package/lib/evaluate-by-operator/operator/equal.js +12 -0
- package/lib/evaluate-by-operator/operator/formula-function.js +50 -0
- package/lib/evaluate-by-operator/operator/greater-than-or-equal.js +12 -0
- package/lib/evaluate-by-operator/operator/greater-than.js +12 -0
- package/lib/evaluate-by-operator/operator/less-than-or-equal.js +12 -0
- package/lib/evaluate-by-operator/operator/less-than.js +12 -0
- package/lib/evaluate-by-operator/operator/minus.js +23 -0
- package/lib/evaluate-by-operator/operator/multiply.js +23 -0
- package/lib/evaluate-by-operator/operator/not-equal.js +12 -0
- package/lib/evaluate-by-operator/operator/power.js +18 -0
- package/lib/grammar-parser/grammar-parser.jison +230 -0
- package/lib/grammar-parser/grammar-parser.js +1528 -0
- package/lib/helper/cell.js +128 -0
- package/lib/helper/number.js +32 -0
- package/lib/helper/string.js +19 -0
- package/lib/index.js +114 -0
- package/lib/parser.js +326 -0
- package/lib/supported-formulas.js +11 -0
- package/package.json +73 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
(The MIT License)
|
|
2
|
+
|
|
3
|
+
Copyright (c) Handsoncode sp. z o.o. <hello@handsoncode.net>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
:warning: This repository is deprecated. We've released new and improved [HyperFormula](https://github.com/handsontable/hyperformula/) engine.
|
|
2
|
+
|
|
3
|
+
<details>
|
|
4
|
+
|
|
5
|
+
# Formula Parser [](https://travis-ci.org/handsontable/formula-parser) [](https://codeclimate.com/github/handsontable/formula-parser/coverage) [](https://www.npmjs.com/package/hot-formula-parser)
|
|
6
|
+
|
|
7
|
+
Library provides a `Parser` class that evaluates excel and mathematical formulas.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
A recommended way to install Formula Parser is through [NPM](https://www.npmjs.com/) using the following command:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
$ npm install hot-formula-parser --save
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Node.js:
|
|
20
|
+
|
|
21
|
+
```js
|
|
22
|
+
var FormulaParser = require("hot-formula-parser").Parser;
|
|
23
|
+
var parser = new FormulaParser();
|
|
24
|
+
|
|
25
|
+
parser.parse("SUM(1, 6, 7)"); // It returns `Object {error: null, result: 14}`
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Browser:
|
|
29
|
+
|
|
30
|
+
```html
|
|
31
|
+
<script src="/node_modules/hot-formula-parser/dist/formula-parser.min.js"></script>
|
|
32
|
+
<script>
|
|
33
|
+
var parser = new formulaParser.Parser();
|
|
34
|
+
|
|
35
|
+
parser.parse("SUM(1, 6, 7)"); // It returns `Object {error: null, result: 14}`
|
|
36
|
+
</script>
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Features
|
|
40
|
+
|
|
41
|
+
It supports:
|
|
42
|
+
|
|
43
|
+
- Any numbers, negative and positive as float or integer;
|
|
44
|
+
- Arithmetic operations like `+`, `-`, `/`, `*`, `%`, `^`;
|
|
45
|
+
- Logical operations like `AND()`, `OR()`, `NOT()`, `XOR()`;
|
|
46
|
+
- Comparison operations like `=`, `>`, `>=`, `<`, `<=`, `<>`;
|
|
47
|
+
- All JavaScript Math constants like `PI()`, `E()`, `LN10()`, `LN2()`, `LOG10E()`, `LOG2E()`, `SQRT1_2()`, `SQRT2()`;
|
|
48
|
+
- String operations like `&` (concatenation eq. `parser.parse('-(2&5)');` will return `-25`);
|
|
49
|
+
- All excel formulas defined in [formula.js](https://github.com/handsontable/formula.js);
|
|
50
|
+
- Relative and absolute cell coordinates like `A1`, `$A1`, `A$1`, `$A$1`;
|
|
51
|
+
- Build-in variables like `TRUE`, `FALSE`, `NULL`
|
|
52
|
+
- Custom variables;
|
|
53
|
+
- Custom functions/formulas;
|
|
54
|
+
- Node and Browser environment.
|
|
55
|
+
|
|
56
|
+
## API (methods)
|
|
57
|
+
|
|
58
|
+
```js
|
|
59
|
+
var parser = new formulaParser.Parser();
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### .parse(expression)
|
|
63
|
+
|
|
64
|
+
Parses and evaluates provided expression. It always returns an object with `result` and `error` properties. `result` property
|
|
65
|
+
always keep evaluated value. If error occurs `error` property will be set as:
|
|
66
|
+
|
|
67
|
+
- `#ERROR!` General error;
|
|
68
|
+
- `#DIV/0!` Divide by zero error;
|
|
69
|
+
- `#NAME?` Not recognised function name or variable name;
|
|
70
|
+
- `#N/A` Indicates that a value is not available to a formula;
|
|
71
|
+
- `#NUM!` Occurs when formula encounters an invalid number;
|
|
72
|
+
- `#VALUE!` Occurs when one of formula arguments is of the wrong type.
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
parser.parse("(1 + 5 + (5 * 10)) / 10"); // returns `Object {error: null, result: 5.6}`
|
|
76
|
+
parser.parse("SUM(MY_VAR)"); // returns `Object {error: "#NAME?", result: null}`
|
|
77
|
+
parser.parse("1;;1"); // returns `Object {error: "#ERROR!", result: null}`
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### .setVariable(name, value)
|
|
81
|
+
|
|
82
|
+
Set predefined variable name which can be visible while parsing formula expression.
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
parser.setVariable("MY_VARIABLE", 5);
|
|
86
|
+
parser.setVariable("fooBar", 10);
|
|
87
|
+
|
|
88
|
+
parser.parse("(1 + MY_VARIABLE + (5 * fooBar)) / fooBar"); // returns `5.6`
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### .getVariable(name)
|
|
92
|
+
|
|
93
|
+
Get variable name.
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
parser.setVariable("fooBar", 10);
|
|
97
|
+
|
|
98
|
+
parser.getVariable("fooBar"); // returns `10`
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### .setFunction(name, fn)
|
|
102
|
+
|
|
103
|
+
Set custom function which can be visible while parsing formula expression.
|
|
104
|
+
|
|
105
|
+
```js
|
|
106
|
+
parser.setFunction("ADD_5", function (params) {
|
|
107
|
+
return params[0] + 5;
|
|
108
|
+
});
|
|
109
|
+
parser.setFunction("GET_LETTER", function (params) {
|
|
110
|
+
var string = params[0];
|
|
111
|
+
var index = params[1] - 1;
|
|
112
|
+
|
|
113
|
+
return string.charAt(index);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
parser.parse("SUM(4, ADD_5(1))"); // returns `10`
|
|
117
|
+
parser.parse('GET_LETTER("Some string", 3)'); // returns `m`
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### .getFunction(name)
|
|
121
|
+
|
|
122
|
+
Get custom function.
|
|
123
|
+
|
|
124
|
+
```js
|
|
125
|
+
parser.setFunction("ADD_5", function (params) {
|
|
126
|
+
return params[0] + 5;
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
parser.getFunction("ADD_5")([1]); // returns `6`
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### .SUPPORTED_FORMULAS
|
|
133
|
+
|
|
134
|
+
List of all supported formulas function.
|
|
135
|
+
|
|
136
|
+
```js
|
|
137
|
+
require("hot-formula-parser").SUPPORTED_FORMULAS; // An array of formula names
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
## API (hooks)
|
|
141
|
+
|
|
142
|
+
### 'callVariable' (name, done)
|
|
143
|
+
|
|
144
|
+
Fired while retrieving variable. If variable was defined earlier using `setVariable` you can overwrite it by this hook.
|
|
145
|
+
|
|
146
|
+
```js
|
|
147
|
+
parser.on("callVariable", function (name, done) {
|
|
148
|
+
if (name === "foo") {
|
|
149
|
+
done(Math.PI / 2);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
parser.parse("SUM(SIN(foo), COS(foo))"); // returns `1`
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### 'callFunction' (name, params, done)
|
|
157
|
+
|
|
158
|
+
Fired while calling function. If function was defined earlier using `setFunction` you can overwrite it's result by this hook.
|
|
159
|
+
You can also use this to override result of build-in formulas.
|
|
160
|
+
|
|
161
|
+
```js
|
|
162
|
+
parser.on("callFunction", function (name, params, done) {
|
|
163
|
+
if (name === "ADD_5") {
|
|
164
|
+
done(params[0] + 5);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
parser.parse("ADD_5(3)"); // returns `8`
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### 'callCellValue' (cellCoord, done)
|
|
172
|
+
|
|
173
|
+
Fired while retrieving cell value by its label (eq: `B3`, `B$3`, `B$3`, `$B$3`).
|
|
174
|
+
|
|
175
|
+
```js
|
|
176
|
+
parser.on("callCellValue", function (cellCoord, done) {
|
|
177
|
+
// using label
|
|
178
|
+
if (cellCoord.label === "B$6") {
|
|
179
|
+
done("hello");
|
|
180
|
+
}
|
|
181
|
+
// or using indexes
|
|
182
|
+
if (
|
|
183
|
+
cellCoord.row.index === 5 &&
|
|
184
|
+
cellCoord.row.isAbsolute &&
|
|
185
|
+
cellCoord.column.index === 1 &&
|
|
186
|
+
!cellCoord.column.isAbsolute
|
|
187
|
+
) {
|
|
188
|
+
done("hello");
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (cellCoord.label === "C6") {
|
|
192
|
+
done(0.75);
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
parser.parse("B$6"); // returns `"hello"`
|
|
197
|
+
parser.parse('B$6&" world"'); // returns `"hello world"`
|
|
198
|
+
parser.parse("FISHER(C6)"); // returns `0.9729550745276566`
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### 'callRangeValue' (startCellCoord, endCellCoord, done)
|
|
202
|
+
|
|
203
|
+
Fired while retrieving cells range value (eq: `A1:B3`, `$A1:B$3`, `A$1:B$3`, `$A$1:$B$3`).
|
|
204
|
+
|
|
205
|
+
```js
|
|
206
|
+
parser.on("callRangeValue", function (startCellCoord, endCellCoord, done) {
|
|
207
|
+
var data = [
|
|
208
|
+
[1, 2, 3, 4, 5],
|
|
209
|
+
[6, 7, 8, 9, 10],
|
|
210
|
+
[11, 12, 13, 14, 15],
|
|
211
|
+
[16, 17, 18, 19, 20],
|
|
212
|
+
];
|
|
213
|
+
var fragment = [];
|
|
214
|
+
|
|
215
|
+
for (
|
|
216
|
+
var row = startCellCoord.row.index;
|
|
217
|
+
row <= endCellCoord.row.index;
|
|
218
|
+
row++
|
|
219
|
+
) {
|
|
220
|
+
var rowData = data[row];
|
|
221
|
+
var colFragment = [];
|
|
222
|
+
|
|
223
|
+
for (
|
|
224
|
+
var col = startCellCoord.column.index;
|
|
225
|
+
col <= endCellCoord.column.index;
|
|
226
|
+
col++
|
|
227
|
+
) {
|
|
228
|
+
colFragment.push(rowData[col]);
|
|
229
|
+
}
|
|
230
|
+
fragment.push(colFragment);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if (fragment) {
|
|
234
|
+
done(fragment);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
parser.parse("JOIN(A1:E2)"); // returns `"1,2,3,4,5,6,7,8,9,10"`
|
|
239
|
+
parser.parse("COLUMNS(A1:E2)"); // returns `5`
|
|
240
|
+
parser.parse("ROWS(A1:E2)"); // returns `2`
|
|
241
|
+
parser.parse("COUNT(A1:E2)"); // returns `10`
|
|
242
|
+
parser.parse('COUNTIF(A1:E2, ">5")'); // returns `5`
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Want to help?
|
|
246
|
+
|
|
247
|
+
Please see [CONTRIBUTING.md](CONTRIBUTING.md).
|
|
248
|
+
|
|
249
|
+
### Changelog
|
|
250
|
+
|
|
251
|
+
To see the list of recent changes, see [Releases section](https://github.com/handsontable/formula-parser/releases).
|
|
252
|
+
|
|
253
|
+
### License
|
|
254
|
+
|
|
255
|
+
The MIT License (see the [LICENSE](https://github.com/handsontable/formula-parser/blob/master/LICENSE) file for the full text).
|
|
256
|
+
|
|
257
|
+
### Contact
|
|
258
|
+
|
|
259
|
+
You can contact us at hello@handsontable.com.
|
|
260
|
+
|
|
261
|
+
</details>
|
package/es/error.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
3
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
4
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
5
|
+
export var ERROR = "ERROR";
|
|
6
|
+
export var ERROR_DIV_ZERO = "DIV/0";
|
|
7
|
+
export var ERROR_NAME = "NAME";
|
|
8
|
+
export var ERROR_NOT_AVAILABLE = "N/A";
|
|
9
|
+
export var ERROR_NULL = "NULL";
|
|
10
|
+
export var ERROR_NUM = "NUM";
|
|
11
|
+
export var ERROR_REF = "REF";
|
|
12
|
+
export var ERROR_VALUE = "VALUE";
|
|
13
|
+
var errors = _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, ERROR, "Syntax error"), ERROR_DIV_ZERO, "#DIV/0!"), ERROR_NAME, "Wrong function name or parameter"), ERROR_NOT_AVAILABLE, "#N/A"), ERROR_NULL, "#NULL!"), ERROR_NUM, "#NUM!"), ERROR_REF, "#REF!"), ERROR_VALUE, "#VALUE!");
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Return error type based on provided error id.
|
|
17
|
+
*
|
|
18
|
+
* @param {String} type Error type.
|
|
19
|
+
* @returns {String|null} Returns error id.
|
|
20
|
+
*/
|
|
21
|
+
export default function error(type) {
|
|
22
|
+
var result;
|
|
23
|
+
type = (type + "").replace(/#|!|\?/g, "");
|
|
24
|
+
if (errors[type]) {
|
|
25
|
+
result = errors[type];
|
|
26
|
+
}
|
|
27
|
+
return result ? result : null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Check if error type is strict valid with knows errors.
|
|
32
|
+
*
|
|
33
|
+
* @param {String} Error type.
|
|
34
|
+
* @return {Boolean}
|
|
35
|
+
*/
|
|
36
|
+
export function isValidStrict(type) {
|
|
37
|
+
var valid = false;
|
|
38
|
+
for (var i in errors) {
|
|
39
|
+
if (Object.prototype.hasOwnProperty.call(errors, i) && errors[i] === type) {
|
|
40
|
+
valid = true;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return valid;
|
|
45
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
|
|
2
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
4
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
5
|
+
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
|
|
6
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
7
|
+
/* eslint-disable import/no-named-as-default-member */
|
|
8
|
+
import add from "./operator/add";
|
|
9
|
+
import ampersand from "./operator/ampersand";
|
|
10
|
+
import divide from "./operator/divide";
|
|
11
|
+
import equal from "./operator/equal";
|
|
12
|
+
import formulaFunction from "./operator/formula-function";
|
|
13
|
+
import greaterThan from "./operator/greater-than";
|
|
14
|
+
import greaterThanOrEqual from "./operator/greater-than-or-equal";
|
|
15
|
+
import lessThan from "./operator/less-than";
|
|
16
|
+
import lessThanOrEqual from "./operator/less-than-or-equal";
|
|
17
|
+
import minus from "./operator/minus";
|
|
18
|
+
import multiply from "./operator/multiply";
|
|
19
|
+
import notEqual from "./operator/not-equal";
|
|
20
|
+
import power from "./operator/power";
|
|
21
|
+
import { ERROR_NAME } from "./../error";
|
|
22
|
+
var availableOperators = Object.create(null);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Evaluate values by operator id.git
|
|
26
|
+
*
|
|
27
|
+
* @param {String} operator Operator id.
|
|
28
|
+
* @param {Array} [params=[]] Arguments to evaluate.
|
|
29
|
+
* @returns {*}
|
|
30
|
+
*/
|
|
31
|
+
export default function evaluateByOperator(operator) {
|
|
32
|
+
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
33
|
+
operator = operator.toUpperCase();
|
|
34
|
+
if (!availableOperators[operator]) {
|
|
35
|
+
throw Error(ERROR_NAME);
|
|
36
|
+
}
|
|
37
|
+
return availableOperators[operator].apply(availableOperators, _toConsumableArray(params));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Register operator.
|
|
42
|
+
*
|
|
43
|
+
* @param {String|Array} symbol Symbol to register.
|
|
44
|
+
* @param {Function} func Logic to register for this symbol.
|
|
45
|
+
*/
|
|
46
|
+
export function registerOperation(symbol, func) {
|
|
47
|
+
if (!Array.isArray(symbol)) {
|
|
48
|
+
symbol = [symbol.toUpperCase()];
|
|
49
|
+
}
|
|
50
|
+
symbol.forEach(function (s) {
|
|
51
|
+
if (func.isFactory) {
|
|
52
|
+
availableOperators[s] = func(s);
|
|
53
|
+
} else {
|
|
54
|
+
availableOperators[s] = func;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
registerOperation(add.SYMBOL, add);
|
|
59
|
+
registerOperation(ampersand.SYMBOL, ampersand);
|
|
60
|
+
registerOperation(divide.SYMBOL, divide);
|
|
61
|
+
registerOperation(equal.SYMBOL, equal);
|
|
62
|
+
registerOperation(power.SYMBOL, power);
|
|
63
|
+
registerOperation(formulaFunction.SYMBOL, formulaFunction);
|
|
64
|
+
registerOperation(greaterThan.SYMBOL, greaterThan);
|
|
65
|
+
registerOperation(greaterThanOrEqual.SYMBOL, greaterThanOrEqual);
|
|
66
|
+
registerOperation(lessThan.SYMBOL, lessThan);
|
|
67
|
+
registerOperation(lessThanOrEqual.SYMBOL, lessThanOrEqual);
|
|
68
|
+
registerOperation(multiply.SYMBOL, multiply);
|
|
69
|
+
registerOperation(notEqual.SYMBOL, notEqual);
|
|
70
|
+
registerOperation(minus.SYMBOL, minus);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { toNumber } from "./../../helper/number";
|
|
2
|
+
import { ERROR_VALUE } from "./../../error";
|
|
3
|
+
export var SYMBOL = "+";
|
|
4
|
+
export default function func(first) {
|
|
5
|
+
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
6
|
+
rest[_key - 1] = arguments[_key];
|
|
7
|
+
}
|
|
8
|
+
var result = rest.reduce(function (acc, value) {
|
|
9
|
+
return acc + toNumber(value || 0);
|
|
10
|
+
}, toNumber(first || 0));
|
|
11
|
+
if (isNaN(result)) {
|
|
12
|
+
throw Error(ERROR_VALUE);
|
|
13
|
+
}
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
func.SYMBOL = SYMBOL;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export var SYMBOL = "&";
|
|
2
|
+
export default function func() {
|
|
3
|
+
for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
4
|
+
params[_key] = arguments[_key];
|
|
5
|
+
}
|
|
6
|
+
return params.reduce(function (acc, value) {
|
|
7
|
+
return acc + value.toString();
|
|
8
|
+
}, "");
|
|
9
|
+
}
|
|
10
|
+
func.SYMBOL = SYMBOL;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { toNumber } from "./../../helper/number";
|
|
2
|
+
import { ERROR_DIV_ZERO, ERROR_VALUE } from "./../../error";
|
|
3
|
+
export var SYMBOL = "/";
|
|
4
|
+
export default function func(first) {
|
|
5
|
+
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
6
|
+
rest[_key - 1] = arguments[_key];
|
|
7
|
+
}
|
|
8
|
+
var result = rest.reduce(function (acc, value) {
|
|
9
|
+
return acc / toNumber(value);
|
|
10
|
+
}, toNumber(first));
|
|
11
|
+
if (result === Infinity) {
|
|
12
|
+
return ERROR_DIV_ZERO;
|
|
13
|
+
// throw Error(ERROR_DIV_ZERO);
|
|
14
|
+
}
|
|
15
|
+
if (isNaN(result)) {
|
|
16
|
+
return ERROR_VALUE;
|
|
17
|
+
// throw Error(ERROR_VALUE);
|
|
18
|
+
}
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
func.SYMBOL = SYMBOL;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as formulajs from "@fileverse-dev/formulajs";
|
|
2
|
+
import SUPPORTED_FORMULAS from "./../../supported-formulas";
|
|
3
|
+
import { ERROR_NAME } from "./../../error";
|
|
4
|
+
export var SYMBOL = SUPPORTED_FORMULAS;
|
|
5
|
+
export default function func(symbol) {
|
|
6
|
+
return function __formulaFunction() {
|
|
7
|
+
symbol = symbol.toUpperCase();
|
|
8
|
+
var symbolParts = symbol.split(".");
|
|
9
|
+
var foundFormula = false;
|
|
10
|
+
var result;
|
|
11
|
+
if (symbolParts.length === 1) {
|
|
12
|
+
if (formulajs[symbolParts[0]]) {
|
|
13
|
+
foundFormula = true;
|
|
14
|
+
result = formulajs[symbolParts[0]].apply(formulajs, arguments);
|
|
15
|
+
}
|
|
16
|
+
} else {
|
|
17
|
+
var length = symbolParts.length;
|
|
18
|
+
var index = 0;
|
|
19
|
+
var nestedFormula = formulajs;
|
|
20
|
+
while (index < length) {
|
|
21
|
+
nestedFormula = nestedFormula[symbolParts[index]];
|
|
22
|
+
index++;
|
|
23
|
+
if (!nestedFormula) {
|
|
24
|
+
nestedFormula = null;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (nestedFormula) {
|
|
29
|
+
foundFormula = true;
|
|
30
|
+
result = nestedFormula.apply(void 0, arguments);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (!foundFormula) {
|
|
34
|
+
throw Error(ERROR_NAME);
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
func.isFactory = true;
|
|
40
|
+
func.SYMBOL = SYMBOL;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { toNumber } from "./../../helper/number";
|
|
2
|
+
import { ERROR_VALUE } from "./../../error";
|
|
3
|
+
export var SYMBOL = "-";
|
|
4
|
+
export default function func(first) {
|
|
5
|
+
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
6
|
+
rest[_key - 1] = arguments[_key];
|
|
7
|
+
}
|
|
8
|
+
var result = rest.reduce(function (acc, value) {
|
|
9
|
+
return acc - toNumber(value);
|
|
10
|
+
}, toNumber(first));
|
|
11
|
+
if (isNaN(result)) {
|
|
12
|
+
throw Error(ERROR_VALUE);
|
|
13
|
+
}
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
func.SYMBOL = SYMBOL;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { toNumber } from "./../../helper/number";
|
|
2
|
+
import { ERROR_VALUE } from "./../../error";
|
|
3
|
+
export var SYMBOL = "*";
|
|
4
|
+
export default function func(first) {
|
|
5
|
+
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
6
|
+
rest[_key - 1] = arguments[_key];
|
|
7
|
+
}
|
|
8
|
+
var result = rest.reduce(function (acc, value) {
|
|
9
|
+
return acc * toNumber(value);
|
|
10
|
+
}, toNumber(first));
|
|
11
|
+
if (isNaN(result)) {
|
|
12
|
+
throw Error(ERROR_VALUE);
|
|
13
|
+
}
|
|
14
|
+
return result;
|
|
15
|
+
}
|
|
16
|
+
func.SYMBOL = SYMBOL;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { toNumber } from "./../../helper/number";
|
|
2
|
+
import { ERROR_VALUE } from "./../../error";
|
|
3
|
+
export var SYMBOL = "^";
|
|
4
|
+
export default function func(exp1, exp2) {
|
|
5
|
+
var result = Math.pow(toNumber(exp1), toNumber(exp2));
|
|
6
|
+
if (isNaN(result)) {
|
|
7
|
+
throw Error(ERROR_VALUE);
|
|
8
|
+
}
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
11
|
+
func.SYMBOL = SYMBOL;
|