@cdjs/js-math
Advanced tools
| !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.jsMath=e():t.jsMath=e()}(window,(function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var i=e[n]={i:n,l:!1,exports:{}};return t[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var i in t)r.d(n,i,function(e){return t[e]}.bind(null,i));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,r){"use strict";r.r(e),r.d(e,"add",(function(){return o})),r.d(e,"subtract",(function(){return u})),r.d(e,"multiply",(function(){return l})),r.d(e,"devide",(function(){return a})),r.d(e,"round",(function(){return s}));var n=function(){for(var t=0,e=0,r=arguments.length;e<r;e++)t+=arguments[e].length;var n=Array(t),i=0;for(e=0;e<r;e++)for(var o=arguments[e],u=0,l=o.length;u<l;u++,i++)n[i]=o[u];return n},i=new(function(){function t(){this.result=NaN,this.precision=12,this.needCheck=!1}return t.prototype.setPrecision=function(t){this.precision=t},t.prototype.enableCheck=function(t){this.needCheck=t},t.prototype.check=function(t){this.needCheck&&(t<Number.MIN_SAFE_INTEGER||t>Number.MAX_SAFE_INTEGER)&&console.warn(t+" is beyond boundary when transfer to integer, the results may not be accurate")},t.prototype.strip=function(t,e){return void 0===e&&(e=this.precision),Number.parseFloat(t.toPrecision(e))},t.prototype.digitDecimalLength=function(t){var e=String(t).split(/[eE]/),r=(e[0].split(".")[1]||"").length-+(e[1]||0);return r>0?r:0},t.prototype.floatToInteger=function(t){if(!String(t).includes("e"))return Number(String(t).replace(".",""));var e=this.digitDecimalLength(t);return e>0?this.strip(t*Math.pow(10,e)):t},t.prototype.init=function(t){return void 0===t&&(t=0),this.result=t,this},t.prototype.value=function(){var t=this.result;return this.result=NaN,t},t.prototype._handle=function(t,e){return Object.is(this.result,NaN)?this.result=t[0]:t.unshift(this.result),t.length>=2&&(this.result=e.call.apply(e,n([this,t[0],t[1]],t.slice(2)))),this},t.prototype._multiply=function(t,e){for(var r=[],i=2;i<arguments.length;i++)r[i-2]=arguments[i];if(r.length>0)return this._multiply.apply(this,n([this._multiply(t,e),r[0]],r.slice(1)));var o=this.digitDecimalLength(t)+this.digitDecimalLength(e),u=this.floatToInteger(t)*this.floatToInteger(e);return this.check(u),u/Math.pow(10,o)},t.prototype.multiply=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return this._handle(t,this._multiply)},t.prototype._add=function(t,e){for(var r=[],i=2;i<arguments.length;i++)r[i-2]=arguments[i];if(r.length>0)return this._add.apply(this,n([this._add(t,e),r[0]],r.slice(1)));var o=Math.pow(10,Math.max(this.digitDecimalLength(t),this.digitDecimalLength(e)));return(this._multiply(t,o)+this._multiply(e,o))/o},t.prototype.add=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return this._handle(t,this._add)},t.prototype._subtract=function(t,e){for(var r=[],i=2;i<arguments.length;i++)r[i-2]=arguments[i];if(r.length>0)return this._subtract.apply(this,n([this._subtract(t,e),r[0]],r.slice(1)));var o=Math.pow(10,Math.max(this.digitDecimalLength(t),this.digitDecimalLength(e)));return(this._multiply(t,o)-this._multiply(e,o))/o},t.prototype.subtract=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return this._handle(t,this._subtract)},t.prototype._devide=function(t,e){for(var r=[],i=2;i<arguments.length;i++)r[i-2]=arguments[i];if(r.length>0)return this._devide.apply(this,n([this._devide(t,e),r[0]],r.slice(1)));var o=this.floatToInteger(t)/this.floatToInteger(e),u=Math.pow(10,this.digitDecimalLength(e)-this.digitDecimalLength(t));return this._multiply(o,u)},t.prototype.devide=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return this._handle(t,this._devide)},t.prototype.round=function(t){void 0===t&&(t=2);var e=Math.pow(10,t);return this.result=Math.round(this.multiply(e).result),this.devide(e)},t}()),o=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return i.add.apply(i,t).value()},u=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return i.subtract.apply(i,t).value()},l=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return i.multiply.apply(i,t).value()},a=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return i.devide.apply(i,t).value()},s=function(t,e){return i.init(e).round(t).value()};e.default=i}])})); |
+19
-12
| { | ||
| "name": "@cdjs/js-math", | ||
| "version": "0.0.6", | ||
| "version": "1.0.0", | ||
| "description": "A simple math calculation library for JavaScript and Node.js", | ||
| "main": "dist/bundle.js", | ||
| "module": "dist/bundle.esm.js", | ||
| "types": "types/index.d.ts", | ||
| "main": "./dist/index.js", | ||
| "repository": "https://github.com/BlueBlueBlueSky/js-math.git", | ||
| "author": "<15380598700@163.com>", | ||
| "license": "MIT", | ||
| "scripts": { | ||
| "build": "tsc && rollup --config rollup.config.js", | ||
| "test": "jest" | ||
| }, | ||
| "typings": "./types/index.d.ts", | ||
| "keywords": [ | ||
@@ -23,17 +18,29 @@ "Javascipt", | ||
| ], | ||
| "scripts": { | ||
| "serve": "webpack-dev-server --mode=development --config ./build/webpack.config.js", | ||
| "build": "tsc && webpack --mode=production --config ./build/webpack.config.js", | ||
| "lint": "eslint src --ext .js,.ts", | ||
| "test": "jest" | ||
| }, | ||
| "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", | ||
| "eslint-config-prettier": "^6.11.0", | ||
| "eslint-plugin-prettier": "^3.1.4", | ||
| "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", | ||
| "tslib": "^1.10.0", | ||
| "typescript": "^3.7.3" | ||
| "ts-loader": "^6.2.1", | ||
| "typescript": "^3.7.3", | ||
| "webpack": "^4.42.1", | ||
| "webpack-cli": "^3.3.11", | ||
| "webpack-dev-server": "^3.10.3", | ||
| "webpack-merge": "^4.2.2" | ||
| } | ||
| } |
+25
-58
@@ -1,70 +0,37 @@ | ||
| ## js-math | ||
| ## 构建 Webpack + TypeScript JS 库开发框架 | ||
| A lightweight Javascript math library. | ||
| ### Webpack 相关 | ||
| - webpack | ||
| - webpack-cli | ||
| - webpack-merge | ||
| - webpack-dev-server: 启动开发环境,在 webpack.dev.config.js 中配置 | ||
| - html-webpack-plugin: 输入到页面,在 webpack.base.config.js 中配置 | ||
| - clean-webpack-plugin: 用于在下一次打包时清除之前打包的文件,3.0.0 注意导入方式和传参 | ||
| ### TypeScript 相关 | ||
| ### Installing | ||
| - typescript | ||
| - ts-loader: 用于加载 ts 文件 | ||
| - transpileOnly `true`失去类型检查 `false` 降低编译速度 | ||
| - fork-ts-checker-webpack-plugin: 在 `transpileOnly: true` 时配置,开启一个独立的进程进行类型检查 [Webpack 文档](https://webpack.docschina.org/guides/build-performance/) | ||
| ```bash | ||
| npm install @cdjs/js-math | ||
| // or | ||
| yarn add @cdjs/js-math | ||
| ``` | ||
| ### Babel | ||
| ### 测试 | ||
| `npx ts-jest config:init`: 生成 jest 配置文件 | ||
| ### Usage | ||
| - `jest` | ||
| - `@types/jest` | ||
| - `ts-jest`: 测试用例中可以进行类型检查 | ||
| ```javascript | ||
| // es6 module | ||
| import jsMath from '@cdjs/js-math' | ||
| // commonjs | ||
| const jsMath = require('@cdjs/js-math') | ||
| // browser | ||
| <script src="@cdjs/js-math"></script> | ||
| ``` | ||
| `jest.config.js`中新增`preset: 'ts-jest', testEnvironment: 'node'` | ||
| ### eslint | ||
| - eslint | ||
| - @typescript-eslint/eslint-plugin | ||
| - @typescript-eslint/parser | ||
| ### Documentation | ||
| | Method | desc | args | | ||
| | ------------ | -------------------------------------------------------- | ------------------------------------------------------ | | ||
| | multiply | 乘 | ...Array | | ||
| | add | 加 | ...Array | | ||
| | subtract | 减 | ...Array | | ||
| | devide | 除 | ...Array | | ||
| | round | 四舍五入 | ratio: Number 精度<br />number?: Number 需要处理的数字 | | ||
| | chain | 链式调用开始的标志 | number: Number 初始值 | | ||
| | done | 链式调用结束的标志 | / | | ||
| | setPrecision | 设置浮点数最大保留精度,默认 12 位 | number: Number | | ||
| | enableCheck | 是否开启对运算结果是否超出 js 安全数值的提醒,默认 false | check: Boolean | | ||
| ### How to use | ||
| #####Simple use | ||
| ```javascript | ||
| // 乘 | ||
| jsMath.multiply(3, 2) // 6 | ||
| // 四舍五入 | ||
| jsMath.round(0.345, 2) // 0.34 | ||
| ``` | ||
| #####Chained use | ||
| ```javascript | ||
| jsMath.chain(0.1) | ||
| .multiply(0.2, 10, 100) | ||
| .add(1, 2, 3, 5, 10, 1000) | ||
| .subtract(0.1, 0.3, 0.4) | ||
| .devide(2, 5, 5, 20) | ||
| .round(2) | ||
| .done() // 1.04 | ||
| ``` | ||
| `"lint": "eslint src --ext .js,.ts"` |
+26
-0
| import JSMath from './jsMath'; | ||
| declare const jsMath: JSMath; | ||
| /** | ||
| * 加 | ||
| * @param rest 入参 | ||
| */ | ||
| export declare const add: (...rest: number[]) => number; | ||
| /** | ||
| * 减 | ||
| * @param rest 入参 | ||
| */ | ||
| export declare const subtract: (...rest: number[]) => number; | ||
| /** | ||
| * 乘 | ||
| * @param rest 入参 | ||
| */ | ||
| export declare const multiply: (...rest: number[]) => number; | ||
| /** | ||
| * 除 | ||
| * @param rest 入参 | ||
| */ | ||
| export declare const devide: (...rest: number[]) => number; | ||
| /** | ||
| * 四舍五入 | ||
| * @param ratio 四舍五入精度 | ||
| * @param float 需要精确的小数 | ||
| */ | ||
| export declare const round: (ratio: number, float: number) => number; | ||
| export default jsMath; |
+9
-10
| declare class JSMath { | ||
| private result; | ||
| private isChain; | ||
| private precision; | ||
@@ -38,10 +37,10 @@ private needCheck; | ||
| /** | ||
| * 设置链式调用 | ||
| * @param init 初始值 默认 0 | ||
| * 给 result 赋初始值 | ||
| * @param init 初始值 | ||
| */ | ||
| chain(init?: number): JSMath; | ||
| init(init?: number): this; | ||
| /** | ||
| * 结束链式调用并返回最终值 | ||
| */ | ||
| done(): number; | ||
| value(): number; | ||
| /** | ||
@@ -64,3 +63,3 @@ * 链式调用统一的处理 | ||
| */ | ||
| multiply(...rest: number[]): number | JSMath; | ||
| multiply(...rest: number[]): this; | ||
| /** | ||
@@ -77,3 +76,3 @@ * 实现加的链式调用 | ||
| */ | ||
| add(...rest: number[]): number | JSMath; | ||
| add(...rest: number[]): this; | ||
| /** | ||
@@ -90,3 +89,3 @@ * 实现减的链式调用 | ||
| */ | ||
| subtract(...rest: number[]): number | JSMath; | ||
| subtract(...rest: number[]): this; | ||
| /** | ||
@@ -103,3 +102,3 @@ * 实现除的链式调用 | ||
| */ | ||
| devide(...rest: number[]): number | JSMath; | ||
| devide(...rest: number[]): this; | ||
| /** | ||
@@ -110,4 +109,4 @@ * 四舍五入 | ||
| */ | ||
| round(ratio?: number, float?: number): JSMath | void | number; | ||
| round(ratio?: number): this; | ||
| } | ||
| export default JSMath; |
Sorry, the diff of this file is not supported yet
| /*! ***************************************************************************** | ||
| 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 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) { | ||
| var int = this.multiply(base); | ||
| this.result = Math.round(int.result); | ||
| return this.devide(base); | ||
| } | ||
| else { | ||
| if (typeof float === 'number') { | ||
| return this.devide(Math.round(this._multiply(base, float)), base); | ||
| } | ||
| else { | ||
| console.warn('round function should has two arguments but got one'); | ||
| } | ||
| } | ||
| }; | ||
| 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 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) { | ||
| var int = this.multiply(base); | ||
| this.result = Math.round(int.result); | ||
| return this.devide(base); | ||
| } | ||
| else { | ||
| if (typeof float === 'number') { | ||
| return this.devide(Math.round(this._multiply(base, float)), base); | ||
| } | ||
| else { | ||
| console.warn('round function should has two arguments but got one'); | ||
| } | ||
| } | ||
| }; | ||
| return JSMath; | ||
| }()); | ||
| var jsMath = new JSMath(); | ||
| return 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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Mixed license
LicensePackage contains multiple licenses.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
-100%10494
-57.86%19
46.15%5
-28.57%135
-79.04%38
-46.48%1
Infinity%