UNPKG

hyperformula-dc

Version:

HyperFormula is a JavaScript engine for efficient processing of spreadsheet-like data and formulas

357 lines (307 loc) 12.5 kB
"use strict"; 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); } require("core-js/modules/es.reflect.construct.js"); require("core-js/modules/es.symbol.js"); require("core-js/modules/es.symbol.description.js"); require("core-js/modules/es.object.to-string.js"); require("core-js/modules/es.symbol.iterator.js"); require("core-js/modules/es.array.iterator.js"); require("core-js/modules/es.string.iterator.js"); require("core-js/modules/web.dom-collections.iterator.js"); exports.__esModule = true; exports.findNextOddNumber = findNextOddNumber; exports.findNextEvenNumber = findNextEvenNumber; exports.RoundingPlugin = void 0; require("core-js/modules/es.object.get-prototype-of.js"); var _Cell = require("../../Cell"); var _errorMessage = require("../../error-message"); var _FunctionPlugin2 = require("./FunctionPlugin"); 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); } function findNextOddNumber(arg) { var ceiled = Math.ceil(arg); return ceiled % 2 === 1 ? ceiled : ceiled + 1; } function findNextEvenNumber(arg) { var ceiled = Math.ceil(arg); return ceiled % 2 === 0 ? ceiled : ceiled + 1; } var RoundingPlugin = /*#__PURE__*/function (_FunctionPlugin) { _inherits(RoundingPlugin, _FunctionPlugin); var _super = _createSuper(RoundingPlugin); function RoundingPlugin() { _classCallCheck(this, RoundingPlugin); return _super.apply(this, arguments); } _createClass(RoundingPlugin, [{ key: "roundup", value: function roundup(ast, state) { return this.runFunction(ast.args, state, this.metadata('ROUNDDOWN'), function (numberToRound, places) { var placesMultiplier = Math.pow(10, places); if (numberToRound < 0) { return -Math.ceil(-numberToRound * placesMultiplier) / placesMultiplier; } else { return Math.ceil(numberToRound * placesMultiplier) / placesMultiplier; } }); } }, { key: "rounddown", value: function rounddown(ast, state) { return this.runFunction(ast.args, state, this.metadata('ROUNDDOWN'), function (numberToRound, places) { var placesMultiplier = Math.pow(10, places); if (numberToRound < 0) { return -Math.floor(-numberToRound * placesMultiplier) / placesMultiplier; } else { return Math.floor(numberToRound * placesMultiplier) / placesMultiplier; } }); } }, { key: "round", value: function round(ast, state) { return this.runFunction(ast.args, state, this.metadata('ROUND'), function (numberToRound, places) { var placesMultiplier = Math.pow(10, places); if (numberToRound < 0) { return -Math.round(-numberToRound * placesMultiplier) / placesMultiplier; } else { return Math.round(numberToRound * placesMultiplier) / placesMultiplier; } }); } }, { key: "intFunc", value: function intFunc(ast, state) { return this.runFunction(ast.args, state, this.metadata('INT'), function (coercedNumberToRound) { if (coercedNumberToRound < 0) { return -Math.floor(-coercedNumberToRound); } else { return Math.floor(coercedNumberToRound); } }); } }, { key: "even", value: function even(ast, state) { return this.runFunction(ast.args, state, this.metadata('EVEN'), function (coercedNumberToRound) { if (coercedNumberToRound < 0) { return -findNextEvenNumber(-coercedNumberToRound); } else { return findNextEvenNumber(coercedNumberToRound); } }); } }, { key: "odd", value: function odd(ast, state) { return this.runFunction(ast.args, state, this.metadata('ODD'), function (coercedNumberToRound) { if (coercedNumberToRound < 0) { return -findNextOddNumber(-coercedNumberToRound); } else { return findNextOddNumber(coercedNumberToRound); } }); } }, { key: "ceilingmath", value: function ceilingmath(ast, state) { return this.runFunction(ast.args, state, this.metadata('CEILING.MATH'), function (value, significance, mode) { if (significance === 0 || value === 0) { return 0; } significance = Math.abs(significance); if (mode === 1 && value < 0) { significance = -significance; } return Math.ceil(value / significance) * significance; }); } }, { key: "ceiling", value: function ceiling(ast, state) { return this.runFunction(ast.args, state, this.metadata('CEILING'), function (value, significance) { if (value === 0) { return 0; } if (significance === 0) { return new _Cell.CellError(_Cell.ErrorType.DIV_BY_ZERO); } if (value > 0 && significance < 0) { return new _Cell.CellError(_Cell.ErrorType.NUM, _errorMessage.ErrorMessage.DistinctSigns); } return Math.ceil(value / significance) * significance; }); } }, { key: "ceilingprecise", value: function ceilingprecise(ast, state) { return this.runFunction(ast.args, state, this.metadata('CEILING.PRECISE'), function (value, significance) { if (significance === 0 || value === 0) { return 0; } significance = Math.abs(significance); return Math.ceil(value / significance) * significance; }); } }, { key: "floormath", value: function floormath(ast, state) { return this.runFunction(ast.args, state, this.metadata('FLOOR.MATH'), function (value, significance, mode) { if (significance === 0 || value === 0) { return 0; } significance = Math.abs(significance); if (mode === 1 && value < 0) { significance *= -1; } return Math.floor(value / significance) * significance; }); } }, { key: "floor", value: function floor(ast, state) { return this.runFunction(ast.args, state, this.metadata('FLOOR'), function (value, significance) { if (value === 0) { return 0; } if (significance === 0) { return new _Cell.CellError(_Cell.ErrorType.DIV_BY_ZERO); } if (value > 0 && significance < 0) { return new _Cell.CellError(_Cell.ErrorType.NUM, _errorMessage.ErrorMessage.DistinctSigns); } return Math.floor(value / significance) * significance; }); } }, { key: "floorprecise", value: function floorprecise(ast, state) { return this.runFunction(ast.args, state, this.metadata('FLOOR.PRECISE'), function (value, significance) { if (significance === 0 || value === 0) { return 0; } significance = Math.abs(significance); return Math.floor(value / significance) * significance; }); } }]); return RoundingPlugin; }(_FunctionPlugin2.FunctionPlugin); exports.RoundingPlugin = RoundingPlugin; RoundingPlugin.implementedFunctions = { 'ROUNDUP': { method: 'roundup', parameters: [{ argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER }, { argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER, defaultValue: 0 }] }, 'ROUNDDOWN': { method: 'rounddown', parameters: [{ argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER }, { argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER, defaultValue: 0 }] }, 'ROUND': { method: 'round', parameters: [{ argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER }, { argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER, defaultValue: 0 }] }, 'INT': { method: 'intFunc', parameters: [{ argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER }] }, 'EVEN': { method: 'even', parameters: [{ argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER }] }, 'ODD': { method: 'odd', parameters: [{ argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER }] }, 'CEILING.MATH': { method: 'ceilingmath', parameters: [{ argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER }, { argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER, defaultValue: 1 }, { argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER, defaultValue: 0 }] }, 'CEILING': { method: 'ceiling', parameters: [{ argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER }, { argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER }] }, 'CEILING.PRECISE': { method: 'ceilingprecise', parameters: [{ argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER }, { argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER, defaultValue: 1 }] }, 'FLOOR.MATH': { method: 'floormath', parameters: [{ argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER }, { argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER, defaultValue: 1 }, { argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER, defaultValue: 0 }] }, 'FLOOR': { method: 'floor', parameters: [{ argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER }, { argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER }] }, 'FLOOR.PRECISE': { method: 'floorprecise', parameters: [{ argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER }, { argumentType: _FunctionPlugin2.ArgumentTypes.NUMBER, defaultValue: 1 }] } }; RoundingPlugin.aliases = { 'ISO.CEILING': 'CEILING.PRECISE', 'TRUNC': 'ROUNDDOWN' };