hyperformula-dc
Version:
HyperFormula is a JavaScript engine for efficient processing of spreadsheet-like data and formulas
175 lines (141 loc) • 7.05 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.sort.js";
import "core-js/modules/es.math.trunc.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 MEDIAN function
*/
export var MedianPlugin = /*#__PURE__*/function (_FunctionPlugin) {
_inherits(MedianPlugin, _FunctionPlugin);
var _super = _createSuper(MedianPlugin);
function MedianPlugin() {
_classCallCheck(this, MedianPlugin);
return _super.apply(this, arguments);
}
_createClass(MedianPlugin, [{
key: "median",
value:
/**
* Corresponds to MEDIAN(Number1, Number2, ...).
*
* Returns a median of given numbers.
*
* @param ast
* @param state
*/
function median(ast, state) {
var _this = this;
return this.runFunction(ast.args, state, this.metadata('MEDIAN'), function () {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
var values = _this.arithmeticHelper.coerceNumbersExactRanges(args);
if (values instanceof CellError) {
return values;
}
if (values.length === 0) {
return new CellError(ErrorType.NUM, ErrorMessage.OneValue);
}
values.sort(function (a, b) {
return a - b;
});
if (values.length % 2 === 0) {
return (values[values.length / 2 - 1] + values[values.length / 2]) / 2;
} else {
return values[Math.floor(values.length / 2)];
}
});
}
}, {
key: "large",
value: function large(ast, state) {
var _this2 = this;
return this.runFunction(ast.args, state, this.metadata('LARGE'), function (range, n) {
var vals = _this2.arithmeticHelper.manyToExactNumbers(range.valuesFromTopLeftCorner());
if (vals instanceof CellError) {
return vals;
}
vals.sort(function (a, b) {
return a - b;
});
n = Math.trunc(n);
if (n > vals.length) {
return new CellError(ErrorType.NUM, ErrorMessage.ValueLarge);
}
return vals[vals.length - n];
});
}
}, {
key: "small",
value: function small(ast, state) {
var _this3 = this;
return this.runFunction(ast.args, state, this.metadata('SMALL'), function (range, n) {
var vals = _this3.arithmeticHelper.manyToExactNumbers(range.valuesFromTopLeftCorner());
if (vals instanceof CellError) {
return vals;
}
vals.sort(function (a, b) {
return a - b;
});
n = Math.trunc(n);
if (n > vals.length) {
return new CellError(ErrorType.NUM, ErrorMessage.ValueLarge);
}
return vals[n - 1];
});
}
}]);
return MedianPlugin;
}(FunctionPlugin);
MedianPlugin.implementedFunctions = {
'MEDIAN': {
method: 'median',
parameters: [{
argumentType: ArgumentTypes.ANY
}],
repeatLastArgs: 1
},
'LARGE': {
method: 'large',
parameters: [{
argumentType: ArgumentTypes.RANGE
}, {
argumentType: ArgumentTypes.NUMBER,
minValue: 1
}]
},
'SMALL': {
method: 'small',
parameters: [{
argumentType: ArgumentTypes.RANGE
}, {
argumentType: ArgumentTypes.NUMBER,
minValue: 1
}]
}
};