hyperformula-dc
Version:
HyperFormula is a JavaScript engine for efficient processing of spreadsheet-like data and formulas
447 lines (407 loc) • 14.8 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
import "core-js/modules/es.array.concat.js";
import "core-js/modules/es.regexp.exec.js";
import "core-js/modules/es.string.split.js";
import "core-js/modules/es.string.replace.js";
import "core-js/modules/es.string.repeat.js";
import "core-js/modules/es.array.slice.js";
import "core-js/modules/es.regexp.constructor.js";
import "core-js/modules/es.regexp.to-string.js";
import "core-js/modules/es.object.get-prototype-of.js";
import "core-js/modules/es.reflect.construct.js";
import "core-js/modules/es.symbol.js";
import "core-js/modules/es.symbol.description.js";
import "core-js/modules/es.object.to-string.js";
import "core-js/modules/es.symbol.iterator.js";
import "core-js/modules/es.array.iterator.js";
import "core-js/modules/es.string.iterator.js";
import "core-js/modules/web.dom-collections.iterator.js";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
/**
* @license
* Copyright (c) 2021 Handsoncode. All rights reserved.
*/
import { CellError, ErrorType } from '../../Cell';
import { ErrorMessage } from '../../error-message';
import { ArgumentTypes, FunctionPlugin } from './FunctionPlugin';
/**
* Interpreter plugin containing text-specific functions
*/
export var TextPlugin = /*#__PURE__*/function (_FunctionPlugin) {
_inherits(TextPlugin, _FunctionPlugin);
var _super = _createSuper(TextPlugin);
function TextPlugin() {
_classCallCheck(this, TextPlugin);
return _super.apply(this, arguments);
}
_createClass(TextPlugin, [{
key: "concatenate",
value:
/**
* Corresponds to CONCATENATE(value1, [value2, ...])
*
* Concatenates provided arguments to one string.
*
* @param ast
* @param state
*/
function concatenate(ast, state) {
return this.runFunction(ast.args, state, this.metadata('CONCATENATE'), function () {
var _ref;
return (_ref = '').concat.apply(_ref, arguments);
});
}
/**
* Corresponds to SPLIT(string, index)
*
* Splits provided string using space separator and returns chunk at zero-based position specified by second argument
*
* @param ast
* @param state
*/
}, {
key: "split",
value: function split(ast, state) {
return this.runFunction(ast.args, state, this.metadata('SPLIT'), function (stringToSplit, indexToUse) {
var splittedString = stringToSplit.split(' ');
if (indexToUse >= splittedString.length || indexToUse < 0) {
return new CellError(ErrorType.VALUE, ErrorMessage.IndexBounds);
}
return splittedString[indexToUse];
});
}
}, {
key: "len",
value: function len(ast, state) {
return this.runFunction(ast.args, state, this.metadata('LEN'), function (arg) {
return arg.length;
});
}
}, {
key: "lower",
value: function lower(ast, state) {
return this.runFunction(ast.args, state, this.metadata('LOWER'), function (arg) {
return arg.toLowerCase();
});
}
}, {
key: "trim",
value: function trim(ast, state) {
return this.runFunction(ast.args, state, this.metadata('TRIM'), function (arg) {
return arg.replace(/^ +| +$/g, '').replace(/ +/g, ' ');
});
}
}, {
key: "proper",
value: function proper(ast, state) {
return this.runFunction(ast.args, state, this.metadata('PROPER'), function (arg) {
return arg.replace(/[^\s]+/g, function (word) {
return word.charAt(0).toUpperCase() + word.substring(1).toLowerCase();
});
});
}
}, {
key: "clean",
value: function clean(ast, state) {
return this.runFunction(ast.args, state, this.metadata('CLEAN'), function (arg) {
// eslint-disable-next-line no-control-regex
return arg.replace(/[\u0000-\u001F]/g, '');
});
}
}, {
key: "exact",
value: function exact(ast, state) {
return this.runFunction(ast.args, state, this.metadata('EXACT'), function (left, right) {
return left === right;
});
}
}, {
key: "rept",
value: function rept(ast, state) {
return this.runFunction(ast.args, state, this.metadata('REPT'), function (text, count) {
if (count < 0) {
return new CellError(ErrorType.VALUE, ErrorMessage.NegativeCount);
}
return text.repeat(count);
});
}
}, {
key: "right",
value: function right(ast, state) {
return this.runFunction(ast.args, state, this.metadata('RIGHT'), function (text, length) {
if (length < 0) {
return new CellError(ErrorType.VALUE, ErrorMessage.NegativeLength);
} else if (length === 0) {
return '';
}
return text.slice(-length);
});
}
}, {
key: "left",
value: function left(ast, state) {
return this.runFunction(ast.args, state, this.metadata('LEFT'), function (text, length) {
if (length < 0) {
return new CellError(ErrorType.VALUE, ErrorMessage.NegativeLength);
}
return text.slice(0, length);
});
}
}, {
key: "mid",
value: function mid(ast, state) {
return this.runFunction(ast.args, state, this.metadata('MID'), function (text, startPosition, numberOfChars) {
if (startPosition < 1) {
return new CellError(ErrorType.VALUE, ErrorMessage.LessThanOne);
}
if (numberOfChars < 0) {
return new CellError(ErrorType.VALUE, ErrorMessage.NegativeLength);
}
return text.substring(startPosition - 1, startPosition + numberOfChars - 1);
});
}
}, {
key: "replace",
value: function replace(ast, state) {
return this.runFunction(ast.args, state, this.metadata('REPLACE'), function (text, startPosition, numberOfChars, newText) {
if (startPosition < 1) {
return new CellError(ErrorType.VALUE, ErrorMessage.LessThanOne);
}
if (numberOfChars < 0) {
return new CellError(ErrorType.VALUE, ErrorMessage.NegativeLength);
}
return text.substring(0, startPosition - 1) + newText + text.substring(startPosition + numberOfChars - 1);
});
}
}, {
key: "search",
value: function search(ast, state) {
var _this = this;
return this.runFunction(ast.args, state, this.metadata('SEARCH'), function (pattern, text, startIndex) {
if (startIndex < 1 || startIndex > text.length) {
return new CellError(ErrorType.VALUE, ErrorMessage.LengthBounds);
}
var normalizedText = text.substring(startIndex - 1).toLowerCase();
var index;
if (_this.arithmeticHelper.requiresRegex(pattern)) {
index = _this.arithmeticHelper.searchString(pattern, normalizedText);
} else {
index = normalizedText.indexOf(pattern.toLowerCase());
}
index = index + startIndex;
return index > 0 ? index : new CellError(ErrorType.VALUE, ErrorMessage.PatternNotFound);
});
}
}, {
key: "substitute",
value: function substitute(ast, state) {
return this.runFunction(ast.args, state, this.metadata('SUBSTITUTE'), function (text, oldText, newText, occurrence) {
var oldTextRegexp = new RegExp(oldText, 'g');
if (occurrence === undefined) {
return text.replace(oldTextRegexp, newText);
}
if (occurrence < 1) {
return new CellError(ErrorType.VALUE, ErrorMessage.LessThanOne);
}
var match;
var i = 0;
while ((match = oldTextRegexp.exec(text)) !== null) {
if (occurrence === ++i) {
return text.substring(0, match.index) + newText + text.substring(oldTextRegexp.lastIndex);
}
}
return text;
});
}
}, {
key: "find",
value: function find(ast, state) {
return this.runFunction(ast.args, state, this.metadata('FIND'), function (pattern, text, startIndex) {
if (startIndex < 1 || startIndex > text.length) {
return new CellError(ErrorType.VALUE, ErrorMessage.IndexBounds);
}
var shiftedText = text.substring(startIndex - 1);
var index = shiftedText.indexOf(pattern) + startIndex;
return index > 0 ? index : new CellError(ErrorType.VALUE, ErrorMessage.PatternNotFound);
});
}
}, {
key: "t",
value: function t(ast, state) {
return this.runFunction(ast.args, state, this.metadata('T'), function (arg) {
if (arg instanceof CellError) {
return arg;
}
return typeof arg === 'string' ? arg : '';
});
}
}, {
key: "upper",
value: function upper(ast, state) {
return this.runFunction(ast.args, state, this.metadata('UPPER'), function (arg) {
return arg.toUpperCase();
});
}
}]);
return TextPlugin;
}(FunctionPlugin);
TextPlugin.implementedFunctions = {
'CONCATENATE': {
method: 'concatenate',
parameters: [{
argumentType: ArgumentTypes.STRING
}],
repeatLastArgs: 1,
expandRanges: true
},
'EXACT': {
method: 'exact',
parameters: [{
argumentType: ArgumentTypes.STRING
}, {
argumentType: ArgumentTypes.STRING
}]
},
'SPLIT': {
method: 'split',
parameters: [{
argumentType: ArgumentTypes.STRING
}, {
argumentType: ArgumentTypes.NUMBER
}]
},
'LEN': {
method: 'len',
parameters: [{
argumentType: ArgumentTypes.STRING
}]
},
'LOWER': {
method: 'lower',
parameters: [{
argumentType: ArgumentTypes.STRING
}]
},
'MID': {
method: 'mid',
parameters: [{
argumentType: ArgumentTypes.STRING
}, {
argumentType: ArgumentTypes.NUMBER
}, {
argumentType: ArgumentTypes.NUMBER
}]
},
'TRIM': {
method: 'trim',
parameters: [{
argumentType: ArgumentTypes.STRING
}]
},
'T': {
method: 't',
parameters: [{
argumentType: ArgumentTypes.SCALAR
}]
},
'PROPER': {
method: 'proper',
parameters: [{
argumentType: ArgumentTypes.STRING
}]
},
'CLEAN': {
method: 'clean',
parameters: [{
argumentType: ArgumentTypes.STRING
}]
},
'REPT': {
method: 'rept',
parameters: [{
argumentType: ArgumentTypes.STRING
}, {
argumentType: ArgumentTypes.NUMBER
}]
},
'RIGHT': {
method: 'right',
parameters: [{
argumentType: ArgumentTypes.STRING
}, {
argumentType: ArgumentTypes.NUMBER,
defaultValue: 1
}]
},
'LEFT': {
method: 'left',
parameters: [{
argumentType: ArgumentTypes.STRING
}, {
argumentType: ArgumentTypes.NUMBER,
defaultValue: 1
}]
},
'REPLACE': {
method: 'replace',
parameters: [{
argumentType: ArgumentTypes.STRING
}, {
argumentType: ArgumentTypes.NUMBER
}, {
argumentType: ArgumentTypes.NUMBER
}, {
argumentType: ArgumentTypes.STRING
}]
},
'SEARCH': {
method: 'search',
parameters: [{
argumentType: ArgumentTypes.STRING
}, {
argumentType: ArgumentTypes.STRING
}, {
argumentType: ArgumentTypes.NUMBER,
defaultValue: 1
}]
},
'SUBSTITUTE': {
method: 'substitute',
parameters: [{
argumentType: ArgumentTypes.STRING
}, {
argumentType: ArgumentTypes.STRING
}, {
argumentType: ArgumentTypes.STRING
}, {
argumentType: ArgumentTypes.NUMBER,
optionalArg: true
}]
},
'FIND': {
method: 'find',
parameters: [{
argumentType: ArgumentTypes.STRING
}, {
argumentType: ArgumentTypes.STRING
}, {
argumentType: ArgumentTypes.NUMBER,
defaultValue: 1
}]
},
'UPPER': {
method: 'upper',
parameters: [{
argumentType: ArgumentTypes.STRING
}]
}
};