@cdjs/js-math
Advanced tools
| /*! ***************************************************************************** | ||
| Copyright (c) Microsoft Corporation. All rights reserved. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
| this file except in compliance with the License. You may obtain a copy of the | ||
| License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED | ||
| WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
| MERCHANTABLITY OR NON-INFRINGEMENT. | ||
| See the Apache Version 2.0 License for specific language governing permissions | ||
| and limitations under the License. | ||
| ***************************************************************************** */ | ||
| function __spreadArrays() { | ||
| for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
| for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
| for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
| r[k] = a[j]; | ||
| return r; | ||
| } | ||
| var isUndefined = function (u) { return u === void 0; }; | ||
| var JSMath = /** @class */ (function () { | ||
| function JSMath() { | ||
| this.result = NaN; | ||
| this.isChain = false; | ||
| this.precision = 12; | ||
| this.needCheck = false; | ||
| } | ||
| /** | ||
| * 重置精度 | ||
| * @param precision 精度值 | ||
| */ | ||
| JSMath.prototype.setPrecision = function (precision) { | ||
| this.precision = precision; | ||
| }; | ||
| /** | ||
| * 是否开启检测 | ||
| * @param check | ||
| */ | ||
| JSMath.prototype.enableCheck = function (check) { | ||
| this.needCheck = check; | ||
| }; | ||
| /** | ||
| * 检测数字是否在安全范围内 | ||
| * @param number 被检测的数字 | ||
| */ | ||
| JSMath.prototype.check = function (number) { | ||
| if (this.needCheck) { | ||
| if (number < Number.MIN_SAFE_INTEGER || | ||
| number > Number.MAX_SAFE_INTEGER) { | ||
| console.warn(number + " is beyond boundary when transfer to integer, the results may not be accurate"); | ||
| } | ||
| } | ||
| }; | ||
| /** | ||
| * 对浮点数精确指定位数 | ||
| * @param float 浮点数 | ||
| * @param precision 小数位数 | ||
| */ | ||
| JSMath.prototype.strip = function (float, precision) { | ||
| if (precision === void 0) { precision = this.precision; } | ||
| return Number.parseFloat(float.toPrecision(precision)); | ||
| }; | ||
| /** | ||
| * 计算浮点数小数位数 | ||
| * @param float 浮点数 | ||
| */ | ||
| JSMath.prototype.digitDecimalLength = function (float) { | ||
| var eSplit = String(float).split(/[eE]/); // 兼容科学计数法 | ||
| var len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0); | ||
| return len > 0 ? len : 0; | ||
| }; | ||
| /** | ||
| * 将小数转化成整数 | ||
| * @param float 浮点数 | ||
| */ | ||
| JSMath.prototype.floatToInteger = function (float) { | ||
| if (!String(float).includes('e')) { | ||
| return Number(String(float).replace('.', '')); | ||
| } | ||
| var len = this.digitDecimalLength(float); | ||
| return len > 0 ? this.strip(float * Math.pow(10, len)) : float; | ||
| }; | ||
| /** | ||
| * 设置链式调用 | ||
| * @param init 初始值 默认 0 | ||
| */ | ||
| JSMath.prototype.chain = function (init) { | ||
| if (init === void 0) { init = 0; } | ||
| this.result = init; | ||
| this.isChain = true; | ||
| return this; | ||
| }; | ||
| /** | ||
| * 结束链式调用并返回最终值 | ||
| */ | ||
| JSMath.prototype.done = function () { | ||
| var result = this.result; | ||
| this.result = NaN; | ||
| this.isChain = false; | ||
| return result; | ||
| }; | ||
| /** | ||
| * 链式调用统一的处理 | ||
| * @param rest 参数数组 | ||
| * @param fn 具体方法 | ||
| */ | ||
| JSMath.prototype._handle = function (rest, fn) { | ||
| if (this.isChain) | ||
| rest.unshift(this.result); | ||
| var res = fn.call.apply(fn, __spreadArrays([this, rest[0], rest[1]], rest.slice(2))); | ||
| if (this.isChain) { | ||
| this.result = res; | ||
| return this; | ||
| } | ||
| else { | ||
| return res; | ||
| } | ||
| }; | ||
| /** | ||
| * 实现乘的链式调用 | ||
| * @param n1 数字1 | ||
| * @param n2 数字2 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype._multiply = function (n1, n2) { | ||
| var rest = []; | ||
| for (var _i = 2; _i < arguments.length; _i++) { | ||
| rest[_i - 2] = arguments[_i]; | ||
| } | ||
| if (rest.length > 0) { | ||
| return this._multiply.apply(this, __spreadArrays([this._multiply(n1, n2), rest[0]], rest.slice(1))); | ||
| } | ||
| // 获取小数的总位数 | ||
| var total = this.digitDecimalLength(n1) + this.digitDecimalLength(n2); | ||
| // 转成整数后相乘的结果 | ||
| var value = this.floatToInteger(n1) * this.floatToInteger(n2); | ||
| this.check(value); | ||
| return value / Math.pow(10, total); | ||
| }; | ||
| /** | ||
| * 乘 | ||
| * @param rest 参数 | ||
| */ | ||
| JSMath.prototype.multiply = function () { | ||
| var rest = []; | ||
| for (var _i = 0; _i < arguments.length; _i++) { | ||
| rest[_i] = arguments[_i]; | ||
| } | ||
| return this._handle(rest, this._multiply); | ||
| }; | ||
| /** | ||
| * 实现加的链式调用 | ||
| * @param n1 数字1 | ||
| * @param n2 数字2 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype._add = function (n1, n2) { | ||
| var rest = []; | ||
| for (var _i = 2; _i < arguments.length; _i++) { | ||
| rest[_i - 2] = arguments[_i]; | ||
| } | ||
| if (rest.length > 0) { | ||
| return this._add.apply(this, __spreadArrays([this._add(n1, n2), rest[0]], rest.slice(1))); | ||
| } | ||
| // 获取较大位数的小数个数 | ||
| var max = Math.pow(10, Math.max(this.digitDecimalLength(n1), this.digitDecimalLength(n2))); | ||
| return (this._multiply(n1, max) + this._multiply(n2, max)) / max; | ||
| }; | ||
| /** | ||
| * 加 | ||
| * @param rest 参数 | ||
| */ | ||
| JSMath.prototype.add = function () { | ||
| var rest = []; | ||
| for (var _i = 0; _i < arguments.length; _i++) { | ||
| rest[_i] = arguments[_i]; | ||
| } | ||
| return this._handle(rest, this._add); | ||
| }; | ||
| /** | ||
| * 实现减的链式调用 | ||
| * @param n1 数字1 | ||
| * @param n2 数字2 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype._subtract = function (n1, n2) { | ||
| var rest = []; | ||
| for (var _i = 2; _i < arguments.length; _i++) { | ||
| rest[_i - 2] = arguments[_i]; | ||
| } | ||
| if (rest.length > 0) { | ||
| return this._subtract.apply(this, __spreadArrays([this._subtract(n1, n2), rest[0]], rest.slice(1))); | ||
| } | ||
| // 获取较大位数的小数个数 | ||
| var max = Math.pow(10, Math.max(this.digitDecimalLength(n1), this.digitDecimalLength(n2))); | ||
| return (this._multiply(n1, max) - this._multiply(n2, max)) / max; | ||
| }; | ||
| /** | ||
| * 减 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype.subtract = function () { | ||
| var rest = []; | ||
| for (var _i = 0; _i < arguments.length; _i++) { | ||
| rest[_i] = arguments[_i]; | ||
| } | ||
| return this._handle(rest, this._subtract); | ||
| }; | ||
| /** | ||
| * 实现除的链式调用 | ||
| * @param n1 数字1 | ||
| * @param n2 数字2 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype._devide = function (n1, n2) { | ||
| var rest = []; | ||
| for (var _i = 2; _i < arguments.length; _i++) { | ||
| rest[_i - 2] = arguments[_i]; | ||
| } | ||
| if (rest.length > 0) { | ||
| return this._devide.apply(this, __spreadArrays([this._devide(n1, n2), rest[0]], rest.slice(1))); | ||
| } | ||
| // 转换成整数后计算除 | ||
| var intDevide = this.floatToInteger(n1) / this.floatToInteger(n2); | ||
| // 小数的个数差 | ||
| var digitDecimal = Math.pow(10, (this.digitDecimalLength(n2) - this.digitDecimalLength(n1))); | ||
| return this._multiply(intDevide, digitDecimal); | ||
| }; | ||
| /** | ||
| * 除 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype.devide = function () { | ||
| var rest = []; | ||
| for (var _i = 0; _i < arguments.length; _i++) { | ||
| rest[_i] = arguments[_i]; | ||
| } | ||
| return this._handle(rest, this._devide); | ||
| }; | ||
| /** | ||
| * 四舍五入 | ||
| * @param ratio 四舍五入精度 | ||
| * @param float 需要精确的小数 | ||
| */ | ||
| JSMath.prototype.round = function (ratio, float) { | ||
| if (ratio === void 0) { ratio = 2; } | ||
| var base = Math.pow(10, ratio); | ||
| if (this.isChain) { | ||
| this.result = Math.round(this.multiply(base).result); | ||
| return this.devide(base); | ||
| } | ||
| else { | ||
| if (isUndefined(float)) { | ||
| console.warn('round function should has two arguments but got one'); | ||
| } | ||
| else { | ||
| return this.devide(Math.round(this._multiply(base, float)), base); | ||
| } | ||
| } | ||
| }; | ||
| return JSMath; | ||
| }()); | ||
| var jsMath = new JSMath(); | ||
| export default jsMath; |
+278
| (function (global, factory) { | ||
| typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
| typeof define === 'function' && define.amd ? define(factory) : | ||
| (global = global || self, global.jsMath = factory()); | ||
| }(this, (function () { 'use strict'; | ||
| /*! ***************************************************************************** | ||
| Copyright (c) Microsoft Corporation. All rights reserved. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use | ||
| this file except in compliance with the License. You may obtain a copy of the | ||
| License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED | ||
| WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
| MERCHANTABLITY OR NON-INFRINGEMENT. | ||
| See the Apache Version 2.0 License for specific language governing permissions | ||
| and limitations under the License. | ||
| ***************************************************************************** */ | ||
| function __spreadArrays() { | ||
| for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
| for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
| for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
| r[k] = a[j]; | ||
| return r; | ||
| } | ||
| var isUndefined = function (u) { return u === void 0; }; | ||
| var JSMath = /** @class */ (function () { | ||
| function JSMath() { | ||
| this.result = NaN; | ||
| this.isChain = false; | ||
| this.precision = 12; | ||
| this.needCheck = false; | ||
| } | ||
| /** | ||
| * 重置精度 | ||
| * @param precision 精度值 | ||
| */ | ||
| JSMath.prototype.setPrecision = function (precision) { | ||
| this.precision = precision; | ||
| }; | ||
| /** | ||
| * 是否开启检测 | ||
| * @param check | ||
| */ | ||
| JSMath.prototype.enableCheck = function (check) { | ||
| this.needCheck = check; | ||
| }; | ||
| /** | ||
| * 检测数字是否在安全范围内 | ||
| * @param number 被检测的数字 | ||
| */ | ||
| JSMath.prototype.check = function (number) { | ||
| if (this.needCheck) { | ||
| if (number < Number.MIN_SAFE_INTEGER || | ||
| number > Number.MAX_SAFE_INTEGER) { | ||
| console.warn(number + " is beyond boundary when transfer to integer, the results may not be accurate"); | ||
| } | ||
| } | ||
| }; | ||
| /** | ||
| * 对浮点数精确指定位数 | ||
| * @param float 浮点数 | ||
| * @param precision 小数位数 | ||
| */ | ||
| JSMath.prototype.strip = function (float, precision) { | ||
| if (precision === void 0) { precision = this.precision; } | ||
| return Number.parseFloat(float.toPrecision(precision)); | ||
| }; | ||
| /** | ||
| * 计算浮点数小数位数 | ||
| * @param float 浮点数 | ||
| */ | ||
| JSMath.prototype.digitDecimalLength = function (float) { | ||
| var eSplit = String(float).split(/[eE]/); // 兼容科学计数法 | ||
| var len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0); | ||
| return len > 0 ? len : 0; | ||
| }; | ||
| /** | ||
| * 将小数转化成整数 | ||
| * @param float 浮点数 | ||
| */ | ||
| JSMath.prototype.floatToInteger = function (float) { | ||
| if (!String(float).includes('e')) { | ||
| return Number(String(float).replace('.', '')); | ||
| } | ||
| var len = this.digitDecimalLength(float); | ||
| return len > 0 ? this.strip(float * Math.pow(10, len)) : float; | ||
| }; | ||
| /** | ||
| * 设置链式调用 | ||
| * @param init 初始值 默认 0 | ||
| */ | ||
| JSMath.prototype.chain = function (init) { | ||
| if (init === void 0) { init = 0; } | ||
| this.result = init; | ||
| this.isChain = true; | ||
| return this; | ||
| }; | ||
| /** | ||
| * 结束链式调用并返回最终值 | ||
| */ | ||
| JSMath.prototype.done = function () { | ||
| var result = this.result; | ||
| this.result = NaN; | ||
| this.isChain = false; | ||
| return result; | ||
| }; | ||
| /** | ||
| * 链式调用统一的处理 | ||
| * @param rest 参数数组 | ||
| * @param fn 具体方法 | ||
| */ | ||
| JSMath.prototype._handle = function (rest, fn) { | ||
| if (this.isChain) | ||
| rest.unshift(this.result); | ||
| var res = fn.call.apply(fn, __spreadArrays([this, rest[0], rest[1]], rest.slice(2))); | ||
| if (this.isChain) { | ||
| this.result = res; | ||
| return this; | ||
| } | ||
| else { | ||
| return res; | ||
| } | ||
| }; | ||
| /** | ||
| * 实现乘的链式调用 | ||
| * @param n1 数字1 | ||
| * @param n2 数字2 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype._multiply = function (n1, n2) { | ||
| var rest = []; | ||
| for (var _i = 2; _i < arguments.length; _i++) { | ||
| rest[_i - 2] = arguments[_i]; | ||
| } | ||
| if (rest.length > 0) { | ||
| return this._multiply.apply(this, __spreadArrays([this._multiply(n1, n2), rest[0]], rest.slice(1))); | ||
| } | ||
| // 获取小数的总位数 | ||
| var total = this.digitDecimalLength(n1) + this.digitDecimalLength(n2); | ||
| // 转成整数后相乘的结果 | ||
| var value = this.floatToInteger(n1) * this.floatToInteger(n2); | ||
| this.check(value); | ||
| return value / Math.pow(10, total); | ||
| }; | ||
| /** | ||
| * 乘 | ||
| * @param rest 参数 | ||
| */ | ||
| JSMath.prototype.multiply = function () { | ||
| var rest = []; | ||
| for (var _i = 0; _i < arguments.length; _i++) { | ||
| rest[_i] = arguments[_i]; | ||
| } | ||
| return this._handle(rest, this._multiply); | ||
| }; | ||
| /** | ||
| * 实现加的链式调用 | ||
| * @param n1 数字1 | ||
| * @param n2 数字2 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype._add = function (n1, n2) { | ||
| var rest = []; | ||
| for (var _i = 2; _i < arguments.length; _i++) { | ||
| rest[_i - 2] = arguments[_i]; | ||
| } | ||
| if (rest.length > 0) { | ||
| return this._add.apply(this, __spreadArrays([this._add(n1, n2), rest[0]], rest.slice(1))); | ||
| } | ||
| // 获取较大位数的小数个数 | ||
| var max = Math.pow(10, Math.max(this.digitDecimalLength(n1), this.digitDecimalLength(n2))); | ||
| return (this._multiply(n1, max) + this._multiply(n2, max)) / max; | ||
| }; | ||
| /** | ||
| * 加 | ||
| * @param rest 参数 | ||
| */ | ||
| JSMath.prototype.add = function () { | ||
| var rest = []; | ||
| for (var _i = 0; _i < arguments.length; _i++) { | ||
| rest[_i] = arguments[_i]; | ||
| } | ||
| return this._handle(rest, this._add); | ||
| }; | ||
| /** | ||
| * 实现减的链式调用 | ||
| * @param n1 数字1 | ||
| * @param n2 数字2 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype._subtract = function (n1, n2) { | ||
| var rest = []; | ||
| for (var _i = 2; _i < arguments.length; _i++) { | ||
| rest[_i - 2] = arguments[_i]; | ||
| } | ||
| if (rest.length > 0) { | ||
| return this._subtract.apply(this, __spreadArrays([this._subtract(n1, n2), rest[0]], rest.slice(1))); | ||
| } | ||
| // 获取较大位数的小数个数 | ||
| var max = Math.pow(10, Math.max(this.digitDecimalLength(n1), this.digitDecimalLength(n2))); | ||
| return (this._multiply(n1, max) - this._multiply(n2, max)) / max; | ||
| }; | ||
| /** | ||
| * 减 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype.subtract = function () { | ||
| var rest = []; | ||
| for (var _i = 0; _i < arguments.length; _i++) { | ||
| rest[_i] = arguments[_i]; | ||
| } | ||
| return this._handle(rest, this._subtract); | ||
| }; | ||
| /** | ||
| * 实现除的链式调用 | ||
| * @param n1 数字1 | ||
| * @param n2 数字2 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype._devide = function (n1, n2) { | ||
| var rest = []; | ||
| for (var _i = 2; _i < arguments.length; _i++) { | ||
| rest[_i - 2] = arguments[_i]; | ||
| } | ||
| if (rest.length > 0) { | ||
| return this._devide.apply(this, __spreadArrays([this._devide(n1, n2), rest[0]], rest.slice(1))); | ||
| } | ||
| // 转换成整数后计算除 | ||
| var intDevide = this.floatToInteger(n1) / this.floatToInteger(n2); | ||
| // 小数的个数差 | ||
| var digitDecimal = Math.pow(10, (this.digitDecimalLength(n2) - this.digitDecimalLength(n1))); | ||
| return this._multiply(intDevide, digitDecimal); | ||
| }; | ||
| /** | ||
| * 除 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype.devide = function () { | ||
| var rest = []; | ||
| for (var _i = 0; _i < arguments.length; _i++) { | ||
| rest[_i] = arguments[_i]; | ||
| } | ||
| return this._handle(rest, this._devide); | ||
| }; | ||
| /** | ||
| * 四舍五入 | ||
| * @param ratio 四舍五入精度 | ||
| * @param float 需要精确的小数 | ||
| */ | ||
| JSMath.prototype.round = function (ratio, float) { | ||
| if (ratio === void 0) { ratio = 2; } | ||
| var base = Math.pow(10, ratio); | ||
| if (this.isChain) { | ||
| this.result = Math.round(this.multiply(base).result); | ||
| return this.devide(base); | ||
| } | ||
| else { | ||
| if (isUndefined(float)) { | ||
| console.warn('round function should has two arguments but got one'); | ||
| } | ||
| else { | ||
| return this.devide(Math.round(this._multiply(base, float)), base); | ||
| } | ||
| } | ||
| }; | ||
| return JSMath; | ||
| }()); | ||
| var jsMath = new JSMath(); | ||
| return jsMath; | ||
| }))); |
+12
-17
| { | ||
| "name": "@cdjs/js-math", | ||
| "version": "0.0.4", | ||
| "version": "0.0.5", | ||
| "description": "A simple math calculation library for JavaScript and Node.js", | ||
| "main": "dist/index.js", | ||
| "main": "dist/bundle.js", | ||
| "module": "dist/bundle.esm.js", | ||
| "types": "types/index.d.ts", | ||
| "repository": "https://github.com/BlueBlueBlueSky/js-math.git", | ||
| "author": "<15380598700@163.com>", | ||
| "license": "MIT", | ||
| "scripts": { | ||
| "build": "rollup --config rollup.config.js", | ||
| "test": "jest" | ||
| }, | ||
| "keywords": [ | ||
@@ -17,27 +23,16 @@ "Javascipt", | ||
| ], | ||
| "scripts": { | ||
| "serve": "webpack-dev-server --mode=development --config ./build/webpack.config.js", | ||
| "build": "tsc", | ||
| "test": "jest" | ||
| }, | ||
| "typings": "./types/index.d.ts", | ||
| "devDependencies": { | ||
| "@rollup/plugin-typescript": "^2.1.0", | ||
| "@types/jest": "^24.0.24", | ||
| "@types/source-map": "^0.5.7", | ||
| "@typescript-eslint/eslint-plugin": "^2.14.0", | ||
| "@typescript-eslint/parser": "^2.14.0", | ||
| "clean-webpack-plugin": "^3.0.0", | ||
| "eslint": "^6.8.0", | ||
| "fork-ts-checker-webpack-plugin": "^3.1.1", | ||
| "html-webpack-plugin": "^3.2.0", | ||
| "jest": "^24.9.0", | ||
| "prettier": "^1.19.1", | ||
| "rollup": "^1.29.0", | ||
| "ts-jest": "^24.2.0", | ||
| "ts-loader": "^6.2.1", | ||
| "typescript": "^3.7.3", | ||
| "webpack": "^4.41.3", | ||
| "webpack-cli": "^3.3.10", | ||
| "webpack-dev-server": "^3.9.0", | ||
| "webpack-merge": "^4.2.2" | ||
| "tslib": "^1.10.0", | ||
| "typescript": "^3.7.3" | ||
| } | ||
| } |
| import JSMath from './jsMath'; | ||
| var jsMath = new JSMath(); | ||
| export default jsMath; |
-251
| var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
| for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
| for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
| for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
| r[k] = a[j]; | ||
| return r; | ||
| }; | ||
| var isUndefined = function (u) { return u === void 0; }; | ||
| var JSMath = /** @class */ (function () { | ||
| function JSMath() { | ||
| this.result = NaN; | ||
| this.isChain = false; | ||
| this.precision = 12; | ||
| this.needCheck = false; | ||
| } | ||
| /** | ||
| * 重置精度 | ||
| * @param precision 精度值 | ||
| */ | ||
| JSMath.prototype.setPrecision = function (precision) { | ||
| this.precision = precision; | ||
| }; | ||
| /** | ||
| * 是否开启检测 | ||
| * @param check | ||
| */ | ||
| JSMath.prototype.enableCheck = function (check) { | ||
| this.needCheck = check; | ||
| }; | ||
| /** | ||
| * 检测数字是否在安全范围内 | ||
| * @param number 被检测的数字 | ||
| */ | ||
| JSMath.prototype.check = function (number) { | ||
| if (this.needCheck) { | ||
| if (number < Number.MIN_SAFE_INTEGER || | ||
| number > Number.MAX_SAFE_INTEGER) { | ||
| console.warn(number + " is beyond boundary when transfer to integer, the results may not be accurate"); | ||
| } | ||
| } | ||
| }; | ||
| /** | ||
| * 对浮点数精确指定位数 | ||
| * @param float 浮点数 | ||
| * @param precision 小数位数 | ||
| */ | ||
| JSMath.prototype.strip = function (float, precision) { | ||
| if (precision === void 0) { precision = this.precision; } | ||
| return Number.parseFloat(float.toPrecision(precision)); | ||
| }; | ||
| /** | ||
| * 计算浮点数小数位数 | ||
| * @param float 浮点数 | ||
| */ | ||
| JSMath.prototype.digitDecimalLength = function (float) { | ||
| var eSplit = String(float).split(/[eE]/); // 兼容科学计数法 | ||
| var len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0); | ||
| return len > 0 ? len : 0; | ||
| }; | ||
| /** | ||
| * 将小数转化成整数 | ||
| * @param float 浮点数 | ||
| */ | ||
| JSMath.prototype.floatToInteger = function (float) { | ||
| if (!String(float).includes('e')) { | ||
| return Number(String(float).replace('.', '')); | ||
| } | ||
| var len = this.digitDecimalLength(float); | ||
| return len > 0 ? this.strip(float * Math.pow(10, len)) : float; | ||
| }; | ||
| /** | ||
| * 设置链式调用 | ||
| * @param init 初始值 默认 0 | ||
| */ | ||
| JSMath.prototype.chain = function (init) { | ||
| if (init === void 0) { init = 0; } | ||
| this.result = init; | ||
| this.isChain = true; | ||
| return this; | ||
| }; | ||
| /** | ||
| * 结束链式调用并返回最终值 | ||
| */ | ||
| JSMath.prototype.done = function () { | ||
| var result = this.result; | ||
| this.result = NaN; | ||
| this.isChain = false; | ||
| return result; | ||
| }; | ||
| /** | ||
| * 链式调用统一的处理 | ||
| * @param rest 参数数组 | ||
| * @param fn 具体方法 | ||
| */ | ||
| JSMath.prototype._handle = function (rest, fn) { | ||
| if (this.isChain) | ||
| rest.unshift(this.result); | ||
| var res = fn.call.apply(fn, __spreadArrays([this, rest[0], rest[1]], rest.slice(2))); | ||
| if (this.isChain) { | ||
| this.result = res; | ||
| return this; | ||
| } | ||
| else { | ||
| return res; | ||
| } | ||
| }; | ||
| /** | ||
| * 实现乘的链式调用 | ||
| * @param n1 数字1 | ||
| * @param n2 数字2 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype._multiply = function (n1, n2) { | ||
| var rest = []; | ||
| for (var _i = 2; _i < arguments.length; _i++) { | ||
| rest[_i - 2] = arguments[_i]; | ||
| } | ||
| if (rest.length > 0) { | ||
| return this._multiply.apply(this, __spreadArrays([this._multiply(n1, n2), rest[0]], rest.slice(1))); | ||
| } | ||
| // 获取小数的总位数 | ||
| var total = this.digitDecimalLength(n1) + this.digitDecimalLength(n2); | ||
| // 转成整数后相乘的结果 | ||
| var value = this.floatToInteger(n1) * this.floatToInteger(n2); | ||
| this.check(value); | ||
| return value / Math.pow(10, total); | ||
| }; | ||
| /** | ||
| * 乘 | ||
| * @param rest 参数 | ||
| */ | ||
| JSMath.prototype.multiply = function () { | ||
| var rest = []; | ||
| for (var _i = 0; _i < arguments.length; _i++) { | ||
| rest[_i] = arguments[_i]; | ||
| } | ||
| return this._handle(rest, this._multiply); | ||
| }; | ||
| /** | ||
| * 实现加的链式调用 | ||
| * @param n1 数字1 | ||
| * @param n2 数字2 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype._add = function (n1, n2) { | ||
| var rest = []; | ||
| for (var _i = 2; _i < arguments.length; _i++) { | ||
| rest[_i - 2] = arguments[_i]; | ||
| } | ||
| if (rest.length > 0) { | ||
| return this._add.apply(this, __spreadArrays([this._add(n1, n2), rest[0]], rest.slice(1))); | ||
| } | ||
| // 获取较大位数的小数个数 | ||
| var max = Math.pow(10, Math.max(this.digitDecimalLength(n1), this.digitDecimalLength(n2))); | ||
| return (this._multiply(n1, max) + this._multiply(n2, max)) / max; | ||
| }; | ||
| /** | ||
| * 加 | ||
| * @param rest 参数 | ||
| */ | ||
| JSMath.prototype.add = function () { | ||
| var rest = []; | ||
| for (var _i = 0; _i < arguments.length; _i++) { | ||
| rest[_i] = arguments[_i]; | ||
| } | ||
| return this._handle(rest, this._add); | ||
| }; | ||
| /** | ||
| * 实现减的链式调用 | ||
| * @param n1 数字1 | ||
| * @param n2 数字2 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype._subtract = function (n1, n2) { | ||
| var rest = []; | ||
| for (var _i = 2; _i < arguments.length; _i++) { | ||
| rest[_i - 2] = arguments[_i]; | ||
| } | ||
| if (rest.length > 0) { | ||
| return this._subtract.apply(this, __spreadArrays([this._subtract(n1, n2), rest[0]], rest.slice(1))); | ||
| } | ||
| // 获取较大位数的小数个数 | ||
| var max = Math.pow(10, Math.max(this.digitDecimalLength(n1), this.digitDecimalLength(n2))); | ||
| return (this._multiply(n1, max) - this._multiply(n2, max)) / max; | ||
| }; | ||
| /** | ||
| * 减 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype.subtract = function () { | ||
| var rest = []; | ||
| for (var _i = 0; _i < arguments.length; _i++) { | ||
| rest[_i] = arguments[_i]; | ||
| } | ||
| return this._handle(rest, this._subtract); | ||
| }; | ||
| /** | ||
| * 实现除的链式调用 | ||
| * @param n1 数字1 | ||
| * @param n2 数字2 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype._devide = function (n1, n2) { | ||
| var rest = []; | ||
| for (var _i = 2; _i < arguments.length; _i++) { | ||
| rest[_i - 2] = arguments[_i]; | ||
| } | ||
| if (rest.length > 0) { | ||
| return this._devide.apply(this, __spreadArrays([this._devide(n1, n2), rest[0]], rest.slice(1))); | ||
| } | ||
| // 转换成整数后计算除 | ||
| var intDevide = this.floatToInteger(n1) / this.floatToInteger(n2); | ||
| // 小数的个数差 | ||
| var digitDecimal = Math.pow(10, (this.digitDecimalLength(n2) - this.digitDecimalLength(n1))); | ||
| return this._multiply(intDevide, digitDecimal); | ||
| }; | ||
| /** | ||
| * 除 | ||
| * @param rest 数字数组 | ||
| */ | ||
| JSMath.prototype.devide = function () { | ||
| var rest = []; | ||
| for (var _i = 0; _i < arguments.length; _i++) { | ||
| rest[_i] = arguments[_i]; | ||
| } | ||
| return this._handle(rest, this._devide); | ||
| }; | ||
| /** | ||
| * 四舍五入 | ||
| * @param ratio 四舍五入精度 | ||
| * @param float 需要精确的小数 | ||
| */ | ||
| JSMath.prototype.round = function (ratio, float) { | ||
| if (ratio === void 0) { ratio = 2; } | ||
| var base = Math.pow(10, ratio); | ||
| if (this.isChain) { | ||
| this.result = Math.round(this.multiply(base).result); | ||
| return this.devide(base); | ||
| } | ||
| else { | ||
| if (isUndefined(float)) { | ||
| console.warn('round function should has two arguments but got one'); | ||
| } | ||
| else { | ||
| return this.devide(Math.round(this._multiply(base, float)), base); | ||
| } | ||
| } | ||
| }; | ||
| return JSMath; | ||
| }()); | ||
| export default JSMath; |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Mixed license
LicensePackage contains multiple licenses.
Found 1 instance in 1 package
24791
77.09%12
-29.41%644
76.92%1
Infinity%1
Infinity%