@leankylin-sheet/formula-parser 3.1.29 → 3.1.31

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.
Files changed (69) hide show
  1. package/dist/error.d.ts +10 -0
  2. package/dist/evaluate-by-operator/evaluate-by-operator.d.ts +2 -0
  3. package/dist/evaluate-by-operator/operator/add.d.ts +6 -0
  4. package/dist/evaluate-by-operator/operator/ampersand.d.ts +6 -0
  5. package/dist/evaluate-by-operator/operator/divide.d.ts +6 -0
  6. package/dist/evaluate-by-operator/operator/equal.d.ts +6 -0
  7. package/dist/evaluate-by-operator/operator/formula-function.d.ts +7 -0
  8. package/dist/evaluate-by-operator/operator/greater-than-or-equal.d.ts +6 -0
  9. package/dist/evaluate-by-operator/operator/greater-than.d.ts +6 -0
  10. package/dist/evaluate-by-operator/operator/less-than-or-equal.d.ts +6 -0
  11. package/dist/evaluate-by-operator/operator/less-than.d.ts +6 -0
  12. package/dist/evaluate-by-operator/operator/minus.d.ts +6 -0
  13. package/dist/evaluate-by-operator/operator/multiply.d.ts +6 -0
  14. package/dist/evaluate-by-operator/operator/not-equal.d.ts +6 -0
  15. package/dist/evaluate-by-operator/operator/power.d.ts +6 -0
  16. package/dist/grammar-parser/grammar-parser.d.ts +4 -0
  17. package/dist/helper/cell.d.ts +6 -0
  18. package/dist/helper/number.d.ts +2 -0
  19. package/dist/helper/string.d.ts +1 -0
  20. package/{es/index.js → dist/index.d.ts} +5 -5
  21. package/{lib/grammar-parser/grammar-parser.js → dist/index.esm.js} +856 -46
  22. package/{es/grammar-parser/grammar-parser.js → dist/index.js} +903 -42
  23. package/dist/parser.d.ts +20 -0
  24. package/dist/supported-formulas.d.ts +2 -0
  25. package/package.json +6 -18
  26. package/LICENSE +0 -21
  27. package/es/error.js +0 -45
  28. package/es/evaluate-by-operator/evaluate-by-operator.js +0 -70
  29. package/es/evaluate-by-operator/operator/add.js +0 -16
  30. package/es/evaluate-by-operator/operator/ampersand.js +0 -10
  31. package/es/evaluate-by-operator/operator/divide.js +0 -21
  32. package/es/evaluate-by-operator/operator/equal.js +0 -5
  33. package/es/evaluate-by-operator/operator/formula-function.js +0 -40
  34. package/es/evaluate-by-operator/operator/greater-than-or-equal.js +0 -5
  35. package/es/evaluate-by-operator/operator/greater-than.js +0 -5
  36. package/es/evaluate-by-operator/operator/less-than-or-equal.js +0 -5
  37. package/es/evaluate-by-operator/operator/less-than.js +0 -5
  38. package/es/evaluate-by-operator/operator/minus.js +0 -16
  39. package/es/evaluate-by-operator/operator/multiply.js +0 -16
  40. package/es/evaluate-by-operator/operator/not-equal.js +0 -5
  41. package/es/evaluate-by-operator/operator/power.js +0 -11
  42. package/es/grammar-parser/grammar-parser.jison +0 -230
  43. package/es/helper/cell.js +0 -117
  44. package/es/helper/number.js +0 -25
  45. package/es/helper/string.js +0 -13
  46. package/es/parser.js +0 -306
  47. package/es/supported-formulas.js +0 -3
  48. package/lib/error.js +0 -53
  49. package/lib/evaluate-by-operator/evaluate-by-operator.js +0 -77
  50. package/lib/evaluate-by-operator/operator/add.js +0 -23
  51. package/lib/evaluate-by-operator/operator/ampersand.js +0 -17
  52. package/lib/evaluate-by-operator/operator/divide.js +0 -28
  53. package/lib/evaluate-by-operator/operator/equal.js +0 -12
  54. package/lib/evaluate-by-operator/operator/formula-function.js +0 -51
  55. package/lib/evaluate-by-operator/operator/greater-than-or-equal.js +0 -12
  56. package/lib/evaluate-by-operator/operator/greater-than.js +0 -12
  57. package/lib/evaluate-by-operator/operator/less-than-or-equal.js +0 -12
  58. package/lib/evaluate-by-operator/operator/less-than.js +0 -12
  59. package/lib/evaluate-by-operator/operator/minus.js +0 -23
  60. package/lib/evaluate-by-operator/operator/multiply.js +0 -23
  61. package/lib/evaluate-by-operator/operator/not-equal.js +0 -12
  62. package/lib/evaluate-by-operator/operator/power.js +0 -18
  63. package/lib/grammar-parser/grammar-parser.jison +0 -230
  64. package/lib/helper/cell.js +0 -128
  65. package/lib/helper/number.js +0 -32
  66. package/lib/helper/string.js +0 -19
  67. package/lib/index.js +0 -115
  68. package/lib/parser.js +0 -314
  69. package/lib/supported-formulas.js +0 -12
@@ -1,3 +1,481 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var Emitter = require('tiny-emitter');
6
+ var formulajs = require('@formulajs/formulajs');
7
+
8
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
+
10
+ function _interopNamespace(e) {
11
+ if (e && e.__esModule) return e;
12
+ var n = Object.create(null);
13
+ if (e) {
14
+ Object.keys(e).forEach(function (k) {
15
+ if (k !== 'default') {
16
+ var d = Object.getOwnPropertyDescriptor(e, k);
17
+ Object.defineProperty(n, k, d.get ? d : {
18
+ enumerable: true,
19
+ get: function () {
20
+ return e[k];
21
+ }
22
+ });
23
+ }
24
+ });
25
+ }
26
+ n['default'] = e;
27
+ return Object.freeze(n);
28
+ }
29
+
30
+ var Emitter__default = /*#__PURE__*/_interopDefaultLegacy(Emitter);
31
+ var formulajs__namespace = /*#__PURE__*/_interopNamespace(formulajs);
32
+
33
+ function _callSuper(t, o, e) {
34
+ return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e));
35
+ }
36
+ function _isNativeReflectConstruct() {
37
+ try {
38
+ var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
39
+ } catch (t) {}
40
+ return (_isNativeReflectConstruct = function () {
41
+ return !!t;
42
+ })();
43
+ }
44
+ function _iterableToArrayLimit(r, l) {
45
+ var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
46
+ if (null != t) {
47
+ var e,
48
+ n,
49
+ i,
50
+ u,
51
+ a = [],
52
+ f = !0,
53
+ o = !1;
54
+ try {
55
+ if (i = (t = t.call(r)).next, 0 === l) {
56
+ if (Object(t) !== t) return;
57
+ f = !1;
58
+ } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0);
59
+ } catch (r) {
60
+ o = !0, n = r;
61
+ } finally {
62
+ try {
63
+ if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
64
+ } finally {
65
+ if (o) throw n;
66
+ }
67
+ }
68
+ return a;
69
+ }
70
+ }
71
+ function _toPrimitive(t, r) {
72
+ if ("object" != typeof t || !t) return t;
73
+ var e = t[Symbol.toPrimitive];
74
+ if (void 0 !== e) {
75
+ var i = e.call(t, r || "default");
76
+ if ("object" != typeof i) return i;
77
+ throw new TypeError("@@toPrimitive must return a primitive value.");
78
+ }
79
+ return ("string" === r ? String : Number)(t);
80
+ }
81
+ function _toPropertyKey(t) {
82
+ var i = _toPrimitive(t, "string");
83
+ return "symbol" == typeof i ? i : i + "";
84
+ }
85
+ function _classCallCheck(instance, Constructor) {
86
+ if (!(instance instanceof Constructor)) {
87
+ throw new TypeError("Cannot call a class as a function");
88
+ }
89
+ }
90
+ function _defineProperties(target, props) {
91
+ for (var i = 0; i < props.length; i++) {
92
+ var descriptor = props[i];
93
+ descriptor.enumerable = descriptor.enumerable || false;
94
+ descriptor.configurable = true;
95
+ if ("value" in descriptor) descriptor.writable = true;
96
+ Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
97
+ }
98
+ }
99
+ function _createClass(Constructor, protoProps, staticProps) {
100
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
101
+ if (staticProps) _defineProperties(Constructor, staticProps);
102
+ Object.defineProperty(Constructor, "prototype", {
103
+ writable: false
104
+ });
105
+ return Constructor;
106
+ }
107
+ function _defineProperty(obj, key, value) {
108
+ key = _toPropertyKey(key);
109
+ if (key in obj) {
110
+ Object.defineProperty(obj, key, {
111
+ value: value,
112
+ enumerable: true,
113
+ configurable: true,
114
+ writable: true
115
+ });
116
+ } else {
117
+ obj[key] = value;
118
+ }
119
+ return obj;
120
+ }
121
+ function _inherits(subClass, superClass) {
122
+ if (typeof superClass !== "function" && superClass !== null) {
123
+ throw new TypeError("Super expression must either be null or a function");
124
+ }
125
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
126
+ constructor: {
127
+ value: subClass,
128
+ writable: true,
129
+ configurable: true
130
+ }
131
+ });
132
+ Object.defineProperty(subClass, "prototype", {
133
+ writable: false
134
+ });
135
+ if (superClass) _setPrototypeOf(subClass, superClass);
136
+ }
137
+ function _getPrototypeOf(o) {
138
+ _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) {
139
+ return o.__proto__ || Object.getPrototypeOf(o);
140
+ };
141
+ return _getPrototypeOf(o);
142
+ }
143
+ function _setPrototypeOf(o, p) {
144
+ _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
145
+ o.__proto__ = p;
146
+ return o;
147
+ };
148
+ return _setPrototypeOf(o, p);
149
+ }
150
+ function _assertThisInitialized(self) {
151
+ if (self === void 0) {
152
+ throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
153
+ }
154
+ return self;
155
+ }
156
+ function _possibleConstructorReturn(self, call) {
157
+ if (call && (typeof call === "object" || typeof call === "function")) {
158
+ return call;
159
+ } else if (call !== void 0) {
160
+ throw new TypeError("Derived constructors may only return object or undefined");
161
+ }
162
+ return _assertThisInitialized(self);
163
+ }
164
+ function _slicedToArray(arr, i) {
165
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
166
+ }
167
+ function _toConsumableArray(arr) {
168
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
169
+ }
170
+ function _arrayWithoutHoles(arr) {
171
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
172
+ }
173
+ function _arrayWithHoles(arr) {
174
+ if (Array.isArray(arr)) return arr;
175
+ }
176
+ function _iterableToArray(iter) {
177
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
178
+ }
179
+ function _unsupportedIterableToArray(o, minLen) {
180
+ if (!o) return;
181
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
182
+ var n = Object.prototype.toString.call(o).slice(8, -1);
183
+ if (n === "Object" && o.constructor) n = o.constructor.name;
184
+ if (n === "Map" || n === "Set") return Array.from(o);
185
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
186
+ }
187
+ function _arrayLikeToArray(arr, len) {
188
+ if (len == null || len > arr.length) len = arr.length;
189
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
190
+ return arr2;
191
+ }
192
+ function _nonIterableSpread() {
193
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
194
+ }
195
+ function _nonIterableRest() {
196
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
197
+ }
198
+
199
+ /**
200
+ * Convert value into number.
201
+ *
202
+ * @param {String|Number} number
203
+ * @returns {*}
204
+ */
205
+ function toNumber(number) {
206
+ var result;
207
+ if (typeof number === 'number') {
208
+ result = number;
209
+ } else if (typeof number === 'string') {
210
+ result = number.indexOf('.') > -1 ? parseFloat(number) : parseInt(number, 10);
211
+ }
212
+ return result;
213
+ }
214
+
215
+ /**
216
+ * Invert provided number.
217
+ *
218
+ * @param {Number} number
219
+ * @returns {Number} Returns inverted number.
220
+ */
221
+ function invertNumber(number) {
222
+ return -1 * toNumber(number);
223
+ }
224
+
225
+ var ERROR = 'ERROR';
226
+ var ERROR_DIV_ZERO = 'DIV/0';
227
+ var ERROR_NAME = 'NAME';
228
+ var ERROR_NOT_AVAILABLE = 'N/A';
229
+ var ERROR_NULL = 'NULL';
230
+ var ERROR_NUM = 'NUM';
231
+ var ERROR_REF = 'REF';
232
+ var ERROR_VALUE = 'VALUE';
233
+ var errors = _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, ERROR, '#ERROR!'), ERROR_DIV_ZERO, '#DIV/0!'), ERROR_NAME, '#NAME?'), ERROR_NOT_AVAILABLE, '#N/A'), ERROR_NULL, '#NULL!'), ERROR_NUM, '#NUM!'), ERROR_REF, '#REF!'), ERROR_VALUE, '#VALUE!');
234
+
235
+ /**
236
+ * Return error type based on provided error id.
237
+ *
238
+ * @param {String} type Error type.
239
+ * @returns {String|null} Returns error id.
240
+ */
241
+ function error(type) {
242
+ var result;
243
+ type = (type + '').replace(/#|!|\?/g, '');
244
+ if (errors[type]) {
245
+ result = errors[type];
246
+ }
247
+ return result ? result : null;
248
+ }
249
+
250
+ /**
251
+ * Check if error type is strict valid with knows errors.
252
+ *
253
+ * @param {String} Error type.
254
+ * @return {Boolean}
255
+ */
256
+ function isValidStrict(type) {
257
+ var valid = false;
258
+ for (var i in errors) {
259
+ if (Object.prototype.hasOwnProperty.call(errors, i) && errors[i] === type) {
260
+ valid = true;
261
+ break;
262
+ }
263
+ }
264
+ return valid;
265
+ }
266
+
267
+ var SYMBOL = '+';
268
+ function func(first) {
269
+ for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
270
+ rest[_key - 1] = arguments[_key];
271
+ }
272
+ var result = rest.reduce(function (acc, value) {
273
+ return acc + toNumber(value || 0);
274
+ }, toNumber(first || 0));
275
+ if (isNaN(result)) {
276
+ throw Error(ERROR_VALUE);
277
+ }
278
+ return result;
279
+ }
280
+ func.SYMBOL = SYMBOL;
281
+
282
+ var SYMBOL$1 = '&';
283
+ function func$1() {
284
+ for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
285
+ params[_key] = arguments[_key];
286
+ }
287
+ return params.reduce(function (acc, value) {
288
+ return acc + value.toString();
289
+ }, '');
290
+ }
291
+ func$1.SYMBOL = SYMBOL$1;
292
+
293
+ var SYMBOL$2 = '/';
294
+ function func$2(first) {
295
+ for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
296
+ rest[_key - 1] = arguments[_key];
297
+ }
298
+ var result = rest.reduce(function (acc, value) {
299
+ return acc / toNumber(value);
300
+ }, toNumber(first));
301
+ if (result === Infinity) {
302
+ return ERROR_DIV_ZERO;
303
+ // throw Error(ERROR_DIV_ZERO);
304
+ }
305
+ if (isNaN(result)) {
306
+ return ERROR_VALUE;
307
+ // throw Error(ERROR_VALUE);
308
+ }
309
+ return result;
310
+ }
311
+ func$2.SYMBOL = SYMBOL$2;
312
+
313
+ var SYMBOL$3 = '=';
314
+ function func$3(exp1, exp2) {
315
+ return exp1 === exp2;
316
+ }
317
+ func$3.SYMBOL = SYMBOL$3;
318
+
319
+ var SUPPORTED_FORMULAS = Object.keys(formulajs__namespace);
320
+
321
+ var SYMBOL$4 = SUPPORTED_FORMULAS;
322
+ function func$4(symbol) {
323
+ return function __formulaFunction() {
324
+ symbol = symbol.toUpperCase();
325
+ var symbolParts = symbol.split('.');
326
+ var foundFormula = false;
327
+ var result;
328
+ if (symbolParts.length === 1) {
329
+ if (formulajs__namespace[symbolParts[0]]) {
330
+ foundFormula = true;
331
+ result = formulajs__namespace[symbolParts[0]].apply(formulajs__namespace, arguments);
332
+ }
333
+ } else {
334
+ var length = symbolParts.length;
335
+ var index = 0;
336
+ var nestedFormula = formulajs__namespace;
337
+ while (index < length) {
338
+ nestedFormula = nestedFormula[symbolParts[index]];
339
+ index++;
340
+ if (!nestedFormula) {
341
+ nestedFormula = null;
342
+ break;
343
+ }
344
+ }
345
+ if (nestedFormula) {
346
+ foundFormula = true;
347
+ result = nestedFormula.apply(void 0, arguments);
348
+ }
349
+ }
350
+ if (!foundFormula) {
351
+ throw Error(ERROR_NAME);
352
+ }
353
+ return result;
354
+ };
355
+ }
356
+ func$4.isFactory = true;
357
+ func$4.SYMBOL = SYMBOL$4;
358
+
359
+ var SYMBOL$5 = '>';
360
+ function func$5(exp1, exp2) {
361
+ return exp1 > exp2;
362
+ }
363
+ func$5.SYMBOL = SYMBOL$5;
364
+
365
+ var SYMBOL$6 = '>=';
366
+ function func$6(exp1, exp2) {
367
+ return exp1 >= exp2;
368
+ }
369
+ func$6.SYMBOL = SYMBOL$6;
370
+
371
+ var SYMBOL$7 = '<';
372
+ function func$7(exp1, exp2) {
373
+ return exp1 < exp2;
374
+ }
375
+ func$7.SYMBOL = SYMBOL$7;
376
+
377
+ var SYMBOL$8 = '<=';
378
+ function func$8(exp1, exp2) {
379
+ return exp1 <= exp2;
380
+ }
381
+ func$8.SYMBOL = SYMBOL$8;
382
+
383
+ var SYMBOL$9 = '-';
384
+ function func$9(first) {
385
+ for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
386
+ rest[_key - 1] = arguments[_key];
387
+ }
388
+ var result = rest.reduce(function (acc, value) {
389
+ return acc - toNumber(value);
390
+ }, toNumber(first));
391
+ if (isNaN(result)) {
392
+ throw Error(ERROR_VALUE);
393
+ }
394
+ return result;
395
+ }
396
+ func$9.SYMBOL = SYMBOL$9;
397
+
398
+ var SYMBOL$a = '*';
399
+ function func$a(first) {
400
+ for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
401
+ rest[_key - 1] = arguments[_key];
402
+ }
403
+ var result = rest.reduce(function (acc, value) {
404
+ return acc * toNumber(value);
405
+ }, toNumber(first));
406
+ if (isNaN(result)) {
407
+ throw Error(ERROR_VALUE);
408
+ }
409
+ return result;
410
+ }
411
+ func$a.SYMBOL = SYMBOL$a;
412
+
413
+ var SYMBOL$b = '<>';
414
+ function func$b(exp1, exp2) {
415
+ return exp1 !== exp2;
416
+ }
417
+ func$b.SYMBOL = SYMBOL$b;
418
+
419
+ var SYMBOL$c = '^';
420
+ function func$c(exp1, exp2) {
421
+ var result = Math.pow(toNumber(exp1), toNumber(exp2));
422
+ if (isNaN(result)) {
423
+ throw Error(ERROR_VALUE);
424
+ }
425
+ return result;
426
+ }
427
+ func$c.SYMBOL = SYMBOL$c;
428
+
429
+ var availableOperators = Object.create(null);
430
+
431
+ /**
432
+ * Evaluate values by operator id.git
433
+ *
434
+ * @param {String} operator Operator id.
435
+ * @param {Array} [params=[]] Arguments to evaluate.
436
+ * @returns {*}
437
+ */
438
+ function evaluateByOperator(operator) {
439
+ var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
440
+ operator = operator.toUpperCase();
441
+ if (!availableOperators[operator]) {
442
+ throw Error(ERROR_NAME);
443
+ }
444
+ return availableOperators[operator].apply(availableOperators, _toConsumableArray(params));
445
+ }
446
+
447
+ /**
448
+ * Register operator.
449
+ *
450
+ * @param {String|Array} symbol Symbol to register.
451
+ * @param {Function} func Logic to register for this symbol.
452
+ */
453
+ function registerOperation(symbol, func) {
454
+ if (!Array.isArray(symbol)) {
455
+ symbol = [symbol.toUpperCase()];
456
+ }
457
+ symbol.forEach(function (s) {
458
+ if (func.isFactory) {
459
+ availableOperators[s] = func(s);
460
+ } else {
461
+ availableOperators[s] = func;
462
+ }
463
+ });
464
+ }
465
+ registerOperation(func.SYMBOL, func);
466
+ registerOperation(func$1.SYMBOL, func$1);
467
+ registerOperation(func$2.SYMBOL, func$2);
468
+ registerOperation(func$3.SYMBOL, func$3);
469
+ registerOperation(func$c.SYMBOL, func$c);
470
+ registerOperation(func$4.SYMBOL, func$4);
471
+ registerOperation(func$5.SYMBOL, func$5);
472
+ registerOperation(func$6.SYMBOL, func$6);
473
+ registerOperation(func$7.SYMBOL, func$7);
474
+ registerOperation(func$8.SYMBOL, func$8);
475
+ registerOperation(func$a.SYMBOL, func$a);
476
+ registerOperation(func$b.SYMBOL, func$b);
477
+ registerOperation(func$9.SYMBOL, func$9);
478
+
1
479
  /* parser generated by jison 0.4.18 */
2
480
  /*
3
481
  Returns a Parser object of the following structure:
@@ -192,7 +670,6 @@ var parser = {
192
670
  switch (yystate) {
193
671
  case 1:
194
672
  return $$[$0 - 1];
195
- break;
196
673
  case 2:
197
674
  this.$ = yy.callVariable($$[$0][0]);
198
675
  break;
@@ -854,7 +1331,6 @@ var parser = {
854
1331
  var _this = this;
855
1332
  var self = this,
856
1333
  stack = [0],
857
- tstack = [],
858
1334
  // token stack
859
1335
  vstack = [null],
860
1336
  // semantic value stack
@@ -913,7 +1389,6 @@ var parser = {
913
1389
  preErrorSymbol,
914
1390
  state,
915
1391
  action,
916
- a,
917
1392
  r,
918
1393
  yyval = {},
919
1394
  p,
@@ -1390,119 +1865,82 @@ var lexer = function () {
1390
1865
  },
1391
1866
  options: {},
1392
1867
  performAction: function anonymous(yy, yy_, $avoiding_name_collisions, YY_START) {
1393
- var YYSTATE = YY_START;
1394
1868
  switch ($avoiding_name_collisions) {
1395
1869
  case 0:
1396
1870
  /* skip whitespace */
1397
1871
  break;
1398
1872
  case 1:
1399
1873
  return 8;
1400
- break;
1401
1874
  case 2:
1402
1875
  return 8;
1403
- break;
1404
1876
  case 3:
1405
1877
  return 21;
1406
- break;
1407
1878
  case 4:
1408
1879
  return 35;
1409
- break;
1410
1880
  case 5:
1411
1881
  return 24;
1412
- break;
1413
1882
  case 6:
1414
1883
  return 26;
1415
- break;
1416
1884
  case 7:
1417
1885
  return 26;
1418
- break;
1419
1886
  case 8:
1420
1887
  return 25;
1421
- break;
1422
1888
  case 9:
1423
1889
  return 21;
1424
- break;
1425
1890
  case 10:
1426
1891
  return 31;
1427
- break;
1428
1892
  case 11:
1429
1893
  return 31;
1430
- break;
1431
1894
  case 12:
1432
1895
  return 33;
1433
- break;
1434
1896
  case 13:
1435
1897
  return 28;
1436
- break;
1437
1898
  case 14:
1438
1899
  return 9;
1439
- break;
1440
1900
  case 15:
1441
1901
  return ' ';
1442
- break;
1443
1902
  case 16:
1444
1903
  return 32;
1445
- break;
1446
1904
  case 17:
1447
1905
  return 27;
1448
- break;
1449
1906
  case 18:
1450
1907
  return 29;
1451
- break;
1452
1908
  case 19:
1453
1909
  return 30;
1454
- break;
1455
1910
  case 20:
1456
1911
  return 18;
1457
- break;
1458
1912
  case 21:
1459
1913
  return 19;
1460
- break;
1461
1914
  case 22:
1462
1915
  return 17;
1463
- break;
1464
1916
  case 23:
1465
1917
  return 11;
1466
- break;
1467
1918
  case 24:
1468
1919
  return 20;
1469
- break;
1470
1920
  case 25:
1471
1921
  return 12;
1472
- break;
1473
1922
  case 26:
1474
1923
  return 13;
1475
- break;
1476
1924
  case 27:
1477
1925
  return 15;
1478
- break;
1479
1926
  case 28:
1480
1927
  return 14;
1481
- break;
1482
1928
  case 29:
1483
1929
  return 16;
1484
- break;
1485
1930
  case 30:
1486
1931
  return '"';
1487
- break;
1488
1932
  case 31:
1489
1933
  return "'";
1490
- break;
1491
1934
  case 32:
1492
1935
  return "!";
1493
- break;
1494
1936
  case 33:
1495
1937
  return 10;
1496
- break;
1497
1938
  case 34:
1498
1939
  return 34;
1499
- break;
1500
1940
  case 35:
1501
1941
  return '#';
1502
- break;
1503
1942
  case 36:
1504
1943
  return 5;
1505
- break;
1506
1944
  }
1507
1945
  },
1508
1946
  rules: [/^(?:\s+)/, /^(?:"(\\["]|[^"])*")/, /^(?:'(\\[']|[^'])*'[^'!])/, /^(?:[A-Za-z]{1,}[A-Za-z_0-9\.]+(?=[(]))/, /^(?:#[A-Z0-9\/]+(!|\?)?)/, ABSOLUTE_CELL, MIXED_CELL_COL, MIXED_CELL_ROW, RELATIVE_CELL, /^(?:[A-Za-z\.]+(?=[(]))/, /^(?:[A-Za-z]{1,}[A-Za-z_0-9]+)/, /^(?:[A-Za-z_]+)/, /^(?:[0-9]+)/, /^(?:\[(.*)?\])/, /^(?:&)/, /^(?: )/, /^(?:[.])/, /^(?::)/, /^(?:;)/, /^(?:,)/, /^(?:\*)/, /^(?:\/)/, /^(?:-)/, /^(?:\+)/, /^(?:\^)/, /^(?:\()/, /^(?:\))/, /^(?:>)/, /^(?:<)/, /^(?:NOT\b)/, /^(?:")/, /^(?:')/, /^(?:!)/, /^(?:=)/, /^(?:%)/, /^(?:[#])/, /^(?:$)/],
@@ -1516,8 +1954,431 @@ var lexer = function () {
1516
1954
  return lexer;
1517
1955
  }();
1518
1956
  parser.lexer = lexer;
1519
- export function Parser() {
1957
+ function Parser() {
1520
1958
  this.yy = {};
1521
1959
  }
1522
1960
  Parser.prototype = parser;
1523
- parser.Parser = Parser;
1961
+ parser.Parser = Parser;
1962
+
1963
+ /* eslint-disable import/prefer-default-export */
1964
+ /**
1965
+ * Trim value by cutting character starting from the beginning and ending at the same time.
1966
+ *
1967
+ * @param {String} string String to trimming.
1968
+ * @param {Number} [margin=1] Number of character to cut.
1969
+ * @returns {String}
1970
+ */
1971
+ function trimEdges(string) {
1972
+ var margin = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
1973
+ string = string.substring(margin, string.length - margin);
1974
+ return string;
1975
+ }
1976
+
1977
+ /**
1978
+ * Convert row label to index.
1979
+ *
1980
+ * @param {String} label Row label (eq. '1', '5')
1981
+ * @returns {Number} Returns -1 if label is not recognized otherwise proper row index.
1982
+ */
1983
+ function rowLabelToIndex(label) {
1984
+ var result = parseInt(label, 10);
1985
+ if (isNaN(result)) {
1986
+ result = -1;
1987
+ } else {
1988
+ result = Math.max(result - 1, -1);
1989
+ }
1990
+ return result;
1991
+ }
1992
+
1993
+ /**
1994
+ * Convert row index to label.
1995
+ *
1996
+ * @param {Number} row Row index.
1997
+ * @returns {String} Returns row label (eq. '1', '7').
1998
+ */
1999
+ function rowIndexToLabel(row) {
2000
+ var result = '';
2001
+ if (row >= 0) {
2002
+ result = "".concat(row + 1);
2003
+ }
2004
+ return result;
2005
+ }
2006
+ var COLUMN_LABEL_BASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2007
+ var COLUMN_LABEL_BASE_LENGTH = COLUMN_LABEL_BASE.length;
2008
+
2009
+ /**
2010
+ * Convert column label to index.
2011
+ *
2012
+ * @param {String} label Column label (eq. 'ABB', 'CNQ')
2013
+ * @returns {Number} Returns -1 if label is not recognized otherwise proper column index.
2014
+ */
2015
+ function columnLabelToIndex(label) {
2016
+ var result = 0;
2017
+ if (typeof label === 'string') {
2018
+ label = label.toUpperCase();
2019
+ for (var i = 0, j = label.length - 1; i < label.length; i += 1, j -= 1) {
2020
+ result += Math.pow(COLUMN_LABEL_BASE_LENGTH, j) * (COLUMN_LABEL_BASE.indexOf(label[i]) + 1);
2021
+ }
2022
+ }
2023
+ --result;
2024
+ return result;
2025
+ }
2026
+
2027
+ /**
2028
+ * Convert column index to label.
2029
+ *
2030
+ * @param {Number} column Column index.
2031
+ * @returns {String} Returns column label (eq. 'ABB', 'CNQ').
2032
+ */
2033
+ function columnIndexToLabel(column) {
2034
+ var result = '';
2035
+ while (column >= 0) {
2036
+ result = String.fromCharCode(column % COLUMN_LABEL_BASE_LENGTH + 97) + result;
2037
+ column = Math.floor(column / COLUMN_LABEL_BASE_LENGTH) - 1;
2038
+ }
2039
+ return result.toUpperCase();
2040
+ }
2041
+ var simpleSheetName$1 = "[A-Za-z0-9_\xC0-\u02AF]+";
2042
+ var quotedSheetName$1 = "'(?:(?!').|'')*'";
2043
+ var sheetNameRegexp$1 = "(".concat(simpleSheetName$1, "|").concat(quotedSheetName$1, ")!");
2044
+ var LABEL_EXTRACT_REGEXP = new RegExp("^(?:".concat(sheetNameRegexp$1, ")?([$])?([A-Za-z]*)([$])?([0-9]*)$"));
2045
+
2046
+ /**
2047
+ * Extract cell coordinates.
2048
+ *
2049
+ * @param {String} label Cell coordinates (eq. 'A1', '$B6', '$N$98').
2050
+ * @returns {Array} Returns an array of objects.
2051
+ */
2052
+ function extractLabel(label) {
2053
+ if (typeof label !== 'string' || !LABEL_EXTRACT_REGEXP.test(label)) {
2054
+ return [];
2055
+ }
2056
+ var _label$toUpperCase$ma = label.toUpperCase().match(LABEL_EXTRACT_REGEXP),
2057
+ _label$toUpperCase$ma2 = _slicedToArray(_label$toUpperCase$ma, 6),
2058
+ sheetNameStr = _label$toUpperCase$ma2[1],
2059
+ columnAbs = _label$toUpperCase$ma2[2],
2060
+ column = _label$toUpperCase$ma2[3],
2061
+ rowAbs = _label$toUpperCase$ma2[4],
2062
+ row = _label$toUpperCase$ma2[5];
2063
+ if (column == null && row == null) return [];
2064
+ var sheetName = sheetNameStr == null ? null : label.slice(0, sheetNameStr.length).replace(/^'|'$/g, "").replace(/''/g, "'");
2065
+ return [{
2066
+ index: rowLabelToIndex(row),
2067
+ label: row,
2068
+ isAbsolute: rowAbs === '$'
2069
+ }, {
2070
+ index: columnLabelToIndex(column),
2071
+ label: column,
2072
+ isAbsolute: columnAbs === '$'
2073
+ }, sheetName];
2074
+ }
2075
+
2076
+ /**
2077
+ * Convert row and column indexes into cell label.
2078
+ *
2079
+ * @param {Object} row Object with `index` and `isAbsolute` properties.
2080
+ * @param {Object} column Object with `index` and `isAbsolute` properties.
2081
+ * @returns {String} Returns cell label.
2082
+ */
2083
+ function toLabel(row, column) {
2084
+ var rowLabel = (row.isAbsolute ? '$' : '') + rowIndexToLabel(row.index);
2085
+ var columnLabel = (column.isAbsolute ? '$' : '') + columnIndexToLabel(column.index);
2086
+ return columnLabel + rowLabel;
2087
+ }
2088
+
2089
+ /**
2090
+ * @class Parser
2091
+ */
2092
+ var Parser$1 = /*#__PURE__*/function (_Emitter) {
2093
+ function Parser$1() {
2094
+ var _this;
2095
+ _classCallCheck(this, Parser$1);
2096
+ _this = _callSuper(this, Parser$1);
2097
+ _this.parser = new Parser();
2098
+ _this.parser.yy = {
2099
+ toNumber: toNumber,
2100
+ trimEdges: trimEdges,
2101
+ invertNumber: invertNumber,
2102
+ throwError: function throwError(errorName) {
2103
+ return _this._throwError(errorName);
2104
+ },
2105
+ callVariable: function callVariable(variable) {
2106
+ return _this._callVariable(variable);
2107
+ },
2108
+ evaluateByOperator: evaluateByOperator,
2109
+ callFunction: function callFunction(name, params) {
2110
+ return _this._callFunction(name, params);
2111
+ },
2112
+ cellValue: function cellValue(value) {
2113
+ return _this._callCellValue(value);
2114
+ },
2115
+ rangeValue: function rangeValue(start, end) {
2116
+ return _this._callRangeValue(start, end);
2117
+ }
2118
+ };
2119
+ _this.variables = Object.create(null);
2120
+ _this.functions = Object.create(null);
2121
+ _this.options = Object.create(null);
2122
+ _this.setVariable('TRUE', true).setVariable('FALSE', false).setVariable('NULL', null);
2123
+ return _this;
2124
+ }
2125
+
2126
+ /**
2127
+ * Parse formula expression.
2128
+ *
2129
+ * @param {string} expression to parse.
2130
+ * @param {object} options additional params.
2131
+ * @param {string} options.sheetId id of sheet which the formula expression belongs to.
2132
+ * @return {*} Returns an object with tow properties `error` and `result`.
2133
+ */
2134
+ _inherits(Parser$1, _Emitter);
2135
+ return _createClass(Parser$1, [{
2136
+ key: "parse",
2137
+ value: function parse(expression, options) {
2138
+ var result = null;
2139
+ var error$1 = null;
2140
+ this.options = options;
2141
+ try {
2142
+ if (expression === '') {
2143
+ result = '';
2144
+ } else {
2145
+ result = this.parser.parse(expression);
2146
+ }
2147
+ } catch (ex) {
2148
+ var message = error(ex.message);
2149
+ if (message) {
2150
+ error$1 = message;
2151
+ } else {
2152
+ error$1 = error(ERROR);
2153
+ }
2154
+ }
2155
+ if (result instanceof Error) {
2156
+ error$1 = error(result.message) || error(ERROR);
2157
+ result = null;
2158
+ }
2159
+ return {
2160
+ error: error$1,
2161
+ result: result
2162
+ };
2163
+ }
2164
+
2165
+ /**
2166
+ * Set predefined variable name which can be visible while parsing formula expression.
2167
+ *
2168
+ * @param {String} name Variable name.
2169
+ * @param {*} value Variable value.
2170
+ * @returns {Parser}
2171
+ */
2172
+ }, {
2173
+ key: "setVariable",
2174
+ value: function setVariable(name, value) {
2175
+ this.variables[name] = value;
2176
+ return this;
2177
+ }
2178
+
2179
+ /**
2180
+ * Get variable name.
2181
+ *
2182
+ * @param {String} name Variable name.
2183
+ * @returns {*}
2184
+ */
2185
+ }, {
2186
+ key: "getVariable",
2187
+ value: function getVariable(name) {
2188
+ return this.variables[name];
2189
+ }
2190
+
2191
+ /**
2192
+ * Retrieve variable value by its name.
2193
+ *
2194
+ * @param name Variable name.
2195
+ * @returns {*}
2196
+ * @private
2197
+ */
2198
+ }, {
2199
+ key: "_callVariable",
2200
+ value: function _callVariable(name) {
2201
+ var value = this.getVariable(name);
2202
+ this.emit('callVariable', name, function (newValue) {
2203
+ if (newValue !== void 0) {
2204
+ value = newValue;
2205
+ }
2206
+ });
2207
+ if (value === void 0) {
2208
+ throw Error(ERROR_NAME);
2209
+ }
2210
+ return value;
2211
+ }
2212
+
2213
+ /**
2214
+ * Set custom function which can be visible while parsing formula expression.
2215
+ *
2216
+ * @param {String} name Custom function name.
2217
+ * @param {Function} fn Custom function.
2218
+ * @returns {Parser}
2219
+ */
2220
+ }, {
2221
+ key: "setFunction",
2222
+ value: function setFunction(name, fn) {
2223
+ this.functions[name] = fn;
2224
+ return this;
2225
+ }
2226
+
2227
+ /**
2228
+ * Get custom function.
2229
+ *
2230
+ * @param {String} name Custom function name.
2231
+ * @returns {*}
2232
+ */
2233
+ }, {
2234
+ key: "getFunction",
2235
+ value: function getFunction(name) {
2236
+ return this.functions[name];
2237
+ }
2238
+
2239
+ /**
2240
+ * Call function with provided params.
2241
+ *
2242
+ * @param name Function name.
2243
+ * @param params Function params.
2244
+ * @returns {*}
2245
+ * @private
2246
+ */
2247
+ }, {
2248
+ key: "_callFunction",
2249
+ value: function _callFunction(name) {
2250
+ var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
2251
+ var fn = this.getFunction(name);
2252
+ var value;
2253
+ if (fn) {
2254
+ value = fn(params);
2255
+ }
2256
+ this.emit('callFunction', name, params, function (newValue) {
2257
+ if (newValue !== void 0) {
2258
+ value = newValue;
2259
+ }
2260
+ });
2261
+ return value === void 0 ? evaluateByOperator(name, params) : value;
2262
+ }
2263
+
2264
+ /**
2265
+ * Retrieve value by its label (`B3`, `B$3`, `B$3`, `$B$3`).
2266
+ *
2267
+ * @param {String} label Coordinates.
2268
+ * @returns {*}
2269
+ * @private
2270
+ */
2271
+ }, {
2272
+ key: "_callCellValue",
2273
+ value: function _callCellValue(label) {
2274
+ var _extractLabel = extractLabel(label),
2275
+ _extractLabel2 = _slicedToArray(_extractLabel, 3),
2276
+ row = _extractLabel2[0],
2277
+ column = _extractLabel2[1],
2278
+ sheetName = _extractLabel2[2];
2279
+ if ((column === null || column === void 0 ? void 0 : column.index) === -1) {
2280
+ if (row.isAbsolute || column.isAbsolute) {
2281
+ throw Error(ERROR_NAME);
2282
+ }
2283
+ return row.index + 1;
2284
+ } else if ((row === null || row === void 0 ? void 0 : row.index) === -1) {
2285
+ throw Error(ERROR_NAME);
2286
+ }
2287
+ var value = void 0;
2288
+ this.emit('callCellValue', {
2289
+ label: label,
2290
+ row: row,
2291
+ column: column,
2292
+ sheetName: sheetName
2293
+ }, this.options, function (_value) {
2294
+ value = _value;
2295
+ });
2296
+ return value;
2297
+ }
2298
+
2299
+ /**
2300
+ * Retrieve value by its label (`B3:A1`, `B$3:A1`, `B$3:$A1`, `$B$3:A$1`).
2301
+ *
2302
+ * @param {String} startLabel Coordinates of the first cell.
2303
+ * @param {String} endLabel Coordinates of the last cell.
2304
+ * @returns {Array} Returns an array of mixed values.
2305
+ * @private
2306
+ */
2307
+ }, {
2308
+ key: "_callRangeValue",
2309
+ value: function _callRangeValue(startLabel, endLabel) {
2310
+ var _extractLabel3 = extractLabel(startLabel),
2311
+ _extractLabel4 = _slicedToArray(_extractLabel3, 3),
2312
+ startRow = _extractLabel4[0],
2313
+ startColumn = _extractLabel4[1],
2314
+ startSheetName = _extractLabel4[2];
2315
+ var _extractLabel5 = extractLabel(endLabel),
2316
+ _extractLabel6 = _slicedToArray(_extractLabel5, 3),
2317
+ endRow = _extractLabel6[0],
2318
+ endColumn = _extractLabel6[1],
2319
+ endSheetName = _extractLabel6[2];
2320
+ if (endSheetName != null && startSheetName != endSheetName) {
2321
+ throw Error(ERROR_VALUE);
2322
+ }
2323
+ var startCell = {};
2324
+ var endCell = {};
2325
+ startCell.sheetName = startSheetName;
2326
+ if (startRow.index <= endRow.index) {
2327
+ startCell.row = startRow;
2328
+ endCell.row = endRow;
2329
+ } else {
2330
+ startCell.row = endRow;
2331
+ endCell.row = startRow;
2332
+ }
2333
+ if (startColumn.index <= endColumn.index) {
2334
+ startCell.column = startColumn;
2335
+ endCell.column = endColumn;
2336
+ } else {
2337
+ startCell.column = endColumn;
2338
+ endCell.column = startColumn;
2339
+ }
2340
+ startCell.label = toLabel(startCell.row, startCell.column);
2341
+ endCell.label = toLabel(endCell.row, endCell.column);
2342
+ var value = [];
2343
+ this.emit('callRangeValue', startCell, endCell, this.options, function () {
2344
+ var _value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
2345
+ value = _value;
2346
+ });
2347
+ return value;
2348
+ }
2349
+
2350
+ /**
2351
+ * Try to throw error by its name.
2352
+ *
2353
+ * @param {String} errorName Error name.
2354
+ * @returns {String}
2355
+ * @private
2356
+ */
2357
+ }, {
2358
+ key: "_throwError",
2359
+ value: function _throwError(errorName) {
2360
+ if (isValidStrict(errorName)) {
2361
+ throw Error(errorName);
2362
+ }
2363
+ throw Error(ERROR);
2364
+ }
2365
+ }]);
2366
+ }(Emitter__default['default']);
2367
+
2368
+ exports.ERROR = ERROR;
2369
+ exports.ERROR_DIV_ZERO = ERROR_DIV_ZERO;
2370
+ exports.ERROR_NAME = ERROR_NAME;
2371
+ exports.ERROR_NOT_AVAILABLE = ERROR_NOT_AVAILABLE;
2372
+ exports.ERROR_NULL = ERROR_NULL;
2373
+ exports.ERROR_NUM = ERROR_NUM;
2374
+ exports.ERROR_REF = ERROR_REF;
2375
+ exports.ERROR_VALUE = ERROR_VALUE;
2376
+ exports.Parser = Parser$1;
2377
+ exports.SUPPORTED_FORMULAS = SUPPORTED_FORMULAS;
2378
+ exports.columnIndexToLabel = columnIndexToLabel;
2379
+ exports.columnLabelToIndex = columnLabelToIndex;
2380
+ exports.error = error;
2381
+ exports.extractLabel = extractLabel;
2382
+ exports.rowIndexToLabel = rowIndexToLabel;
2383
+ exports.rowLabelToIndex = rowLabelToIndex;
2384
+ exports.toLabel = toLabel;