UNPKG

react-calculatxr

Version:

A calculator similar to the windows calculator

146 lines (109 loc) 10.8 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _constants = require("../constants"); function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _wrapRegExp() { _wrapRegExp = function _wrapRegExp(re, groups) { return new BabelRegExp(re, void 0, groups); }; var _super = RegExp.prototype, _groups = new WeakMap(); function BabelRegExp(re, flags, groups) { var _this = new RegExp(re, flags); return _groups.set(_this, groups || _groups.get(re)), _setPrototypeOf(_this, BabelRegExp.prototype); } function buildGroups(result, re) { var g = _groups.get(re); return Object.keys(g).reduce(function (groups, name) { return groups[name] = result[g[name]], groups; }, Object.create(null)); } return _inherits(BabelRegExp, RegExp), BabelRegExp.prototype.exec = function (str) { var result = _super.exec.call(this, str); return result && (result.groups = buildGroups(result, this)), result; }, BabelRegExp.prototype[Symbol.replace] = function (str, substitution) { if ("string" == typeof substitution) { var groups = _groups.get(this); return _super[Symbol.replace].call(this, str, substitution.replace(/\$<([^>]+)>/g, function (_, name) { return "$" + groups[name]; })); } if ("function" == typeof substitution) { var _this = this; return _super[Symbol.replace].call(this, str, function () { var args = arguments; return "object" != _typeof(args[args.length - 1]) && (args = [].slice.call(args)).push(buildGroups(args, _this)), substitution.apply(this, args); }); } return _super[Symbol.replace].call(this, str, substitution); }, _wrapRegExp.apply(this, arguments); } 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 } }); Object.defineProperty(subClass, "prototype", { writable: false }); 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 _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); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var Utils = /*#__PURE__*/_createClass(function Utils() { var _this = this; _classCallCheck(this, Utils); _defineProperty(this, "extraInputsFilter", function (input) { var getTheNumberSign = function getTheNumberSign() { var pattern = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ""; return new RegExp("((?<ope>[-+\xD7\xF7])".concat(pattern, "|^").concat(pattern, ")(?<num>\\d+(\\.\\d*)?(e[+-]\\d+)?)(=|%)?\xB1$"), "g"); }; return input.join("").replace(/\.+/g, ".").replace(/((?<=[-+×÷])|-?\d+(\.?\d*)?(e[+-]\d+)?(=|%))\./g, "0.").replace(/(?<=\d{16})\.$/g, "").replace(/\d+(\.\d*)?$/g, function (result) { return result.includes(".") ? result.slice(0, 17) : result.slice(0, 16); }).replace(/^-?0\.?%|(?<=[-+×÷])-?0\.?%$|^0±|(?<=\d+(\.?\d*)?(e[+-]\d+)?[-+×÷])0±|-0\.?0*(?=[-+×÷])|^0+|(?<=[-+×÷])0+|∞=/g, "0").replace( /*#__PURE__*/_wrapRegExp(/(^0|(?<=[%\+\x2D\xD7\xF7])0)([1-9])/g, { num: 2 }), "$<num>").replace(getTheNumberSign("-"), "$<ope>$<num>").replace(getTheNumberSign(), "$<ope>-$<num>").replace( /*#__PURE__*/_wrapRegExp(/^\x2D?[0-9]+(\.?[0-9]*)?(e[\+\x2D][0-9]+)?(=|%)([0-9]+(\.?[0-9]*)?(e[\+\x2D][0-9]+)?)/g, { num: 4 }), "$<num>").replace( /*#__PURE__*/_wrapRegExp(/(\x2D?[0-9]+(\.?[0-9]*)?(e[\+\x2D][0-9]+)?)([%\+\x2D\xD7\xF7])=/g, { num: 1, ope: 4 }), "$<num>$<ope>$<num>=").replace( /*#__PURE__*/_wrapRegExp(/([%\+\x2D=\xD7\xF7](?![0-9]+(\.?[0-9]*)?(e[\+\x2D][0-9]+)?))+/g, { ope: 1 }), "$<ope>").replace(/(^-?|(?<=[×÷]))\d+(\.?\d*)?(e[+-]\d+)?(?==?%)/g, function (num) { return _this.roundingTheResult(num / 100); }).replace( /*#__PURE__*/_wrapRegExp(/(\x2D?[0-9]+(\.?[0-9]*)?(e[\+\x2D][0-9]+)?)([\+\x2D])(\x2D?[0-9]+(\.?[0-9]*)?(e[\+\x2D][0-9]+)?)(?=%)/, { firstValue: 1, ope: 4, secondValue: 5 }), function (_) { var _ref; var _ref2 = (_ref = (arguments.length <= 1 ? 0 : arguments.length - 1) - 1 + 1, _ref < 1 || arguments.length <= _ref ? undefined : arguments[_ref]), firstValue = _ref2.firstValue, ope = _ref2.ope, secondValue = _ref2.secondValue; secondValue = _this.roundingTheResult(firstValue * secondValue / 100); return "".concat(firstValue).concat(ope).concat(secondValue); }).replace(/(?<=-?\d+(\.?\d*)?(e[+-]\d+)?[-+×÷]-?\d+(\.?\d*)?(e[+-]\d+)?)%|∞(?=.)|(?<=^-?\d+(\.?\d*)?(e[+-]\d+)?[-+×÷%]?)=$|(?<=[-+×÷%]|(-?\d+(\.?\d*)?(e[+-]\d+)?[-+×÷%]\d+(\.?\d*)?(e[+-]\d+)?))±|(?<=\d+\.\d+)\.|(\.0*|(?<=\d+\.(0*[1-9]+)*)0+)(?=[-+=×÷%])/g, ""); }); _defineProperty(this, "matchTheEquation", function (input, pattern) { var regex = new RegExp("^-?\\d+(\\.?\\d*)?(e[+-]\\d+)?[-+\xD7\xF7]-?\\d+(\\.?\\d*)?(e[+-]\\d+)?".concat(pattern, "$"), "g"); return input === null || input === void 0 ? void 0 : input.match(regex); }); _defineProperty(this, "spacingBetweenOperations", function (input) { return input === null || input === void 0 ? void 0 : input.replace(/(?<=-?(\d+(\.?\d*)?(e[+-]\d+)?))[-+=×÷]/g, " $& "); }); _defineProperty(this, "showFirstEntry", function (input) { var _input$join$match; return _this.spacingBetweenOperations((_input$join$match = input.join("").match(/^-?\d+(\.?\d*)?(e[+-]\d+)?[-+×÷%]/g)) === null || _input$join$match === void 0 ? void 0 : _input$join$match.join("")); }); _defineProperty(this, "showLastEntry", function (input) { var _input$join$match2; return (_input$join$match2 = input.join("").match(/((?<=[-+×÷])|^)-?\d+(\.?\d*)?(e[+-]\d+)?[-+=×÷%]?$|∞/g)) === null || _input$join$match2 === void 0 ? void 0 : _input$join$match2.join("").replace(/(?<=\d+(\.?\d*)?)[-+=×÷%]/g, "").replace(/\B(?=(\d{3})+(?!\d))/g, ",").replace(/(?<=\..+),+/g, ""); }); _defineProperty(this, "roundingTheResult", function (input) { if (!isFinite(input)) { return String(input).replace(/Infinity/g, "∞"); } return String(input).replace(/\d+\.?\d*e[+-]\d+|^-?(\d{17,}|\d{16,}\.\d+)$/g, function (value) { return Number.parseFloat(value).toExponential(6); }).replace(/(?<=[1-9]+|\d+\.)0+(?=e)|e\+0/g, "").replace(/-?\d+\.\d+/g, function (value) { return Number(Math.round(value + "e6") + "e-6"); }); }); _defineProperty(this, "repeatEqualOperations", function (input, replace) { return input.replace(/-?\d+(\.?\d*)?(e[+-]\d+)?(?=[-+×÷]-?\d+(\.?\d*)?(e[+-]\d+)?=)/g, replace); }); _defineProperty(this, "isFinite", function (input) { return /∞/.test(input.join("")); }); _defineProperty(this, "theSliceKeyShouldWork", function (input) { return !/(^0|-?\d+(\.?\d*)?([-+×÷=%]0?|e[+-]\d+([-+×÷=%]0?)?))$/.test(input.join("")); }); _defineProperty(this, "sliceValue", function (input) { return input.join("").replace(/\d+(\.?\d*)?$/g, function (str) { return str.slice(0, -1); }).replace(/^-?\d+(\.?\d*)?(e[+-]\d+)?[-+×÷]$/g, "$&0").replace(/^('')?$|^-$|((?<=-?\d+(\.?\d*)?[-+×÷])-|(?<=[-+×÷])-0)$/g, 0).split(""); }); _defineProperty(this, "calculateTheResult", function (input) { var _this$spacingBetweenO = _this.spacingBetweenOperations(input).split(" "), _this$spacingBetweenO2 = _slicedToArray(_this$spacingBetweenO, 3), firstValue = _this$spacingBetweenO2[0], operation = _this$spacingBetweenO2[1], secondValue = _this$spacingBetweenO2[2]; return _constants.operations.find(function (_ref3) { var character = _ref3.character; return character === operation; }).onClick(Number(firstValue), Number(secondValue)); }); }); var utils = new Utils(); var _default = utils; exports["default"] = _default;