measure-fns
Advanced tools
Comparing version 0.4.2 to 0.4.3
@@ -14,4 +14,4 @@ 'use strict'; | ||
function strip(num, precision) { | ||
if (precision === void 0) { precision = 12; } | ||
return +parseFloat(num.toPrecision(precision)); | ||
if (precision === void 0) { precision = 15; } | ||
return +parseFloat(Number(num).toPrecision(precision)); | ||
} | ||
@@ -25,3 +25,3 @@ /** | ||
var eSplit = num.toString().split(/[eE]/); | ||
var len = (eSplit[0].split('.')[1] || '').length - (+(eSplit[1] || 0)); | ||
var len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0); | ||
return len > 0 ? len : 0; | ||
@@ -38,3 +38,3 @@ } | ||
var dLen = digitLength(num); | ||
return dLen > 0 ? strip(num * Math.pow(10, dLen)) : num; | ||
return dLen > 0 ? strip(Number(num) * Math.pow(10, dLen)) : Number(num); | ||
} | ||
@@ -53,12 +53,24 @@ /** | ||
/** | ||
* 迭代操作 | ||
*/ | ||
function iteratorOperation(arr, operation) { | ||
var num1 = arr[0], num2 = arr[1], others = arr.slice(2); | ||
var res = operation(num1, num2); | ||
others.forEach(function (num) { | ||
res = operation(res, num); | ||
}); | ||
return res; | ||
} | ||
/** | ||
* 精确乘法 | ||
*/ | ||
function times(num1, num2) { | ||
var others = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
others[_i - 2] = arguments[_i]; | ||
function times() { | ||
var nums = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
nums[_i] = arguments[_i]; | ||
} | ||
if (others.length > 0) { | ||
return times.apply(void 0, [times(num1, num2), others[0]].concat(others.slice(1))); | ||
if (nums.length > 2) { | ||
return iteratorOperation(nums, times); | ||
} | ||
var num1 = nums[0], num2 = nums[1]; | ||
var num1Changed = float2Fixed(num1); | ||
@@ -74,11 +86,14 @@ var num2Changed = float2Fixed(num2); | ||
*/ | ||
function plus(num1, num2) { | ||
var others = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
others[_i - 2] = arguments[_i]; | ||
function plus() { | ||
var nums = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
nums[_i] = arguments[_i]; | ||
} | ||
if (others.length > 0) { | ||
return plus.apply(void 0, [plus(num1, num2), others[0]].concat(others.slice(1))); | ||
if (nums.length > 2) { | ||
return iteratorOperation(nums, plus); | ||
} | ||
var num1 = nums[0], num2 = nums[1]; | ||
// 取最大的小数位 | ||
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2))); | ||
// 把小数都转为整数然后再计算 | ||
return (times(num1, baseNum) + times(num2, baseNum)) / baseNum; | ||
@@ -89,10 +104,11 @@ } | ||
*/ | ||
function minus(num1, num2) { | ||
var others = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
others[_i - 2] = arguments[_i]; | ||
function minus() { | ||
var nums = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
nums[_i] = arguments[_i]; | ||
} | ||
if (others.length > 0) { | ||
return minus.apply(void 0, [minus(num1, num2), others[0]].concat(others.slice(1))); | ||
if (nums.length > 2) { | ||
return iteratorOperation(nums, minus); | ||
} | ||
var num1 = nums[0], num2 = nums[1]; | ||
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2))); | ||
@@ -104,10 +120,11 @@ return (times(num1, baseNum) - times(num2, baseNum)) / baseNum; | ||
*/ | ||
function divide(num1, num2) { | ||
var others = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
others[_i - 2] = arguments[_i]; | ||
function divide() { | ||
var nums = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
nums[_i] = arguments[_i]; | ||
} | ||
if (others.length > 0) { | ||
return divide.apply(void 0, [divide(num1, num2), others[0]].concat(others.slice(1))); | ||
if (nums.length > 2) { | ||
return iteratorOperation(nums, divide); | ||
} | ||
var num1 = nums[0], num2 = nums[1]; | ||
var num1Changed = float2Fixed(num1); | ||
@@ -118,3 +135,3 @@ var num2Changed = float2Fixed(num2); | ||
// fix: 类似 10 ** -4 为 0.00009999999999999999,strip 修正 | ||
return times((num1Changed / num2Changed), strip(Math.pow(10, digitLength(num2) - digitLength(num1)))); | ||
return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1)))); | ||
} | ||
@@ -177,2 +194,3 @@ var _boundaryCheckingState = true; | ||
enableBoundaryChecking(false); | ||
exports.LengthUnit = void 0; | ||
@@ -270,2 +288,3 @@ (function (LengthUnit) { | ||
enableBoundaryChecking(false); | ||
exports.MassUnit = void 0; | ||
@@ -335,2 +354,3 @@ (function (MassUnit) { | ||
enableBoundaryChecking(false); | ||
exports.PressureUnit = void 0; | ||
@@ -414,2 +434,3 @@ (function (PressureUnit) { | ||
enableBoundaryChecking(false); | ||
exports.SpeedUnit = void 0; | ||
@@ -494,2 +515,3 @@ (function (SpeedUnit) { | ||
enableBoundaryChecking(false); | ||
exports.TemperatureUnit = void 0; | ||
@@ -496,0 +518,0 @@ (function (TemperatureUnit) { |
{ | ||
"name": "measure-fns", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"description": "Functions for conversion and handling of measurements", | ||
@@ -61,4 +61,4 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"number-precision": "^1.4.0" | ||
"number-precision": "^1.5.1" | ||
} | ||
} |
24282
634
Updatednumber-precision@^1.5.1