measure-fns
Advanced tools
Comparing version 0.1.0 to 0.3.0
@@ -5,2 +5,128 @@ 'use strict'; | ||
/** | ||
* @desc 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。 | ||
* 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998 | ||
*/ | ||
/** | ||
* 把错误的数据转正 | ||
* strip(0.09999999999999998)=0.1 | ||
*/ | ||
function strip(num, precision) { | ||
if (precision === void 0) { precision = 12; } | ||
return +parseFloat(num.toPrecision(precision)); | ||
} | ||
/** | ||
* Return digits length of a number | ||
* @param {*number} num Input number | ||
*/ | ||
function digitLength(num) { | ||
// Get digit length of e | ||
var eSplit = num.toString().split(/[eE]/); | ||
var len = (eSplit[0].split('.')[1] || '').length - (+(eSplit[1] || 0)); | ||
return len > 0 ? len : 0; | ||
} | ||
/** | ||
* 把小数转成整数,支持科学计数法。如果是小数则放大成整数 | ||
* @param {*number} num 输入数 | ||
*/ | ||
function float2Fixed(num) { | ||
if (num.toString().indexOf('e') === -1) { | ||
return Number(num.toString().replace('.', '')); | ||
} | ||
var dLen = digitLength(num); | ||
return dLen > 0 ? strip(num * Math.pow(10, dLen)) : num; | ||
} | ||
/** | ||
* 检测数字是否越界,如果越界给出提示 | ||
* @param {*number} num 输入数 | ||
*/ | ||
function checkBoundary(num) { | ||
if (_boundaryCheckingState) { | ||
if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) { | ||
console.warn(num + " is beyond boundary when transfer to integer, the results may not be accurate"); | ||
} | ||
} | ||
} | ||
/** | ||
* 精确乘法 | ||
*/ | ||
function times(num1, num2) { | ||
var others = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
others[_i - 2] = arguments[_i]; | ||
} | ||
if (others.length > 0) { | ||
return times.apply(void 0, [times(num1, num2), others[0]].concat(others.slice(1))); | ||
} | ||
var num1Changed = float2Fixed(num1); | ||
var num2Changed = float2Fixed(num2); | ||
var baseNum = digitLength(num1) + digitLength(num2); | ||
var leftValue = num1Changed * num2Changed; | ||
checkBoundary(leftValue); | ||
return leftValue / Math.pow(10, baseNum); | ||
} | ||
/** | ||
* 精确加法 | ||
*/ | ||
function plus(num1, num2) { | ||
var others = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
others[_i - 2] = arguments[_i]; | ||
} | ||
if (others.length > 0) { | ||
return plus.apply(void 0, [plus(num1, num2), others[0]].concat(others.slice(1))); | ||
} | ||
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2))); | ||
return (times(num1, baseNum) + times(num2, baseNum)) / baseNum; | ||
} | ||
/** | ||
* 精确减法 | ||
*/ | ||
function minus(num1, num2) { | ||
var others = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
others[_i - 2] = arguments[_i]; | ||
} | ||
if (others.length > 0) { | ||
return minus.apply(void 0, [minus(num1, num2), others[0]].concat(others.slice(1))); | ||
} | ||
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2))); | ||
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]; | ||
} | ||
if (others.length > 0) { | ||
return divide.apply(void 0, [divide(num1, num2), others[0]].concat(others.slice(1))); | ||
} | ||
var num1Changed = float2Fixed(num1); | ||
var num2Changed = float2Fixed(num2); | ||
checkBoundary(num1Changed); | ||
checkBoundary(num2Changed); | ||
// fix: 类似 10 ** -4 为 0.00009999999999999999,strip 修正 | ||
return times((num1Changed / num2Changed), strip(Math.pow(10, digitLength(num2) - digitLength(num1)))); | ||
} | ||
/** | ||
* 四舍五入 | ||
*/ | ||
function round(num, ratio) { | ||
var base = Math.pow(10, ratio); | ||
return divide(Math.round(times(num, base)), base); | ||
} | ||
var _boundaryCheckingState = true; | ||
/** | ||
* 是否进行边界检查,默认开启 | ||
* @param flag 标记开关,true 为开启,false 为关闭,默认为 true | ||
*/ | ||
function enableBoundaryChecking(flag) { | ||
if (flag === void 0) { flag = true; } | ||
_boundaryCheckingState = flag; | ||
} | ||
var index = { strip: strip, plus: plus, minus: minus, times: times, divide: divide, round: round, digitLength: digitLength, float2Fixed: float2Fixed, enableBoundaryChecking: enableBoundaryChecking }; | ||
var MeasurementType; | ||
@@ -33,3 +159,3 @@ | ||
function round(value, roundAmount) { | ||
function round$1(value, roundAmount) { | ||
if (roundAmount === 0 || roundAmount === true) { | ||
@@ -46,3 +172,3 @@ return Math.round(value); | ||
// TODO: verify round is 0> int | ||
return round(value, options.round); | ||
return round$1(value, options.round); | ||
} | ||
@@ -53,2 +179,4 @@ | ||
index.enableBoundaryChecking(false); | ||
(function (LengthUnit) { | ||
@@ -62,11 +190,11 @@ LengthUnit["metres"] = "m"; | ||
function centimetresToMetres(value) { | ||
return value / CENTIMETRES_IN_METRE; | ||
return index.divide(value, CENTIMETRES_IN_METRE); | ||
} | ||
function feetToMetres(value) { | ||
return value / FEET_IN_METRE; | ||
return index.divide(value, FEET_IN_METRE); | ||
} | ||
function inchesToMetres(value) { | ||
return value / INCHES_IN_METRE; | ||
return index.divide(value, INCHES_IN_METRE); | ||
} | ||
@@ -108,11 +236,13 @@ | ||
function lengthToCentimetres(_length, options = {}) { | ||
return applyOptions(_length.value * CENTIMETRES_IN_METRE, options); | ||
return applyOptions(index.times(_length.value, CENTIMETRES_IN_METRE), options); | ||
} | ||
function lengthToFeet(_length, options = {}) { | ||
return applyOptions(_length.value * FEET_IN_METRE, options); | ||
return applyOptions(index.times(_length.value, FEET_IN_METRE), options); | ||
} | ||
function lengthToInches(_length, options = {}) { | ||
return applyOptions(_length.value * INCHES_IN_METRE, options); | ||
return applyOptions(index.times(_length.value, INCHES_IN_METRE), options); | ||
} | ||
index.enableBoundaryChecking(false); | ||
(function (MassUnit) { | ||
@@ -126,11 +256,11 @@ MassUnit["kilograms"] = "kg"; | ||
function gramsToKilograms(value) { | ||
return value / GRAMS_IN_KILOGRAM; | ||
return index.divide(value, GRAMS_IN_KILOGRAM); | ||
} | ||
function poundsToKilograms(value) { | ||
return value / POUNDS_IN_KILOGRAM; | ||
return index.divide(value, POUNDS_IN_KILOGRAM); | ||
} | ||
function ouncesToKilograms(value) { | ||
return value / OUNCES_IN_KILOGRAM; | ||
return index.divide(value, OUNCES_IN_KILOGRAM); | ||
} | ||
@@ -172,11 +302,13 @@ | ||
function massToGrams(_mass, options = {}) { | ||
return applyOptions(_mass.value * GRAMS_IN_KILOGRAM, options); | ||
return applyOptions(index.times(_mass.value, GRAMS_IN_KILOGRAM), options); | ||
} | ||
function massToPounds(_mass, options = {}) { | ||
return applyOptions(_mass.value * POUNDS_IN_KILOGRAM, options); | ||
return applyOptions(index.times(_mass.value, POUNDS_IN_KILOGRAM), options); | ||
} | ||
function massToOunces(_mass, options = {}) { | ||
return applyOptions(_mass.value * OUNCES_IN_KILOGRAM, options); | ||
return applyOptions(index.times(_mass.value, OUNCES_IN_KILOGRAM), options); | ||
} | ||
index.enableBoundaryChecking(false); | ||
(function (PressureUnit) { | ||
@@ -191,15 +323,15 @@ PressureUnit["pascal"] = "pa"; | ||
function hectopascalToPascal(value) { | ||
return value * PASCALS_IN_HECTOPASCAL; | ||
return index.times(value, PASCALS_IN_HECTOPASCAL); | ||
} | ||
function barToPascal(value) { | ||
return value * PASCALS_IN_BAR; | ||
return index.times(value, PASCALS_IN_BAR); | ||
} | ||
function millibarToPascal(value) { | ||
return value * PASCALS_IN_MILLIBAR; | ||
return index.times(value, PASCALS_IN_MILLIBAR); | ||
} | ||
function mmhgToPascal(value) { | ||
return value * PASCALS_IN_MMHG; | ||
return index.times(value, PASCALS_IN_MMHG); | ||
} | ||
@@ -247,14 +379,16 @@ | ||
function pressureToHectopascal(pressure, options = {}) { | ||
return applyOptions(pressure.value / PASCALS_IN_HECTOPASCAL, options); | ||
return applyOptions(index.divide(pressure.value, PASCALS_IN_HECTOPASCAL), options); | ||
} | ||
function pressureToBar(pressure, options = {}) { | ||
return applyOptions(pressure.value / PASCALS_IN_BAR, options); | ||
return applyOptions(index.divide(pressure.value, PASCALS_IN_BAR), options); | ||
} | ||
function pressureToMillibar(pressure, options = {}) { | ||
return applyOptions(pressure.value / PASCALS_IN_MILLIBAR, options); | ||
return applyOptions(index.divide(pressure.value, PASCALS_IN_MILLIBAR), options); | ||
} | ||
function pressureToMmhg(pressure, options = {}) { | ||
return applyOptions(pressure.value / PASCALS_IN_MMHG, options); | ||
return applyOptions(index.divide(pressure.value, PASCALS_IN_MMHG), options); | ||
} | ||
index.enableBoundaryChecking(false); | ||
(function (SpeedUnit) { | ||
@@ -269,11 +403,11 @@ SpeedUnit["metresPerSecond"] = "ms"; | ||
function kmhToMs(value) { | ||
return value / SECONDS_IN_HOUR / KILOMETRES_IN_METRE; | ||
return index.divide(value, SECONDS_IN_HOUR, KILOMETRES_IN_METRE); | ||
} | ||
function mphToMs(value) { | ||
return value / SECONDS_IN_HOUR / MILES_IN_METRE; | ||
return index.divide(value, SECONDS_IN_HOUR, MILES_IN_METRE); | ||
} | ||
function knotsToMs(value) { | ||
return value / SECONDS_IN_HOUR / NAUTICAL_MILES_IN_METRE; | ||
return index.divide(value, SECONDS_IN_HOUR, NAUTICAL_MILES_IN_METRE); | ||
} | ||
@@ -283,3 +417,3 @@ | ||
// eslint-disable-next-line no-magic-numbers | ||
return value / FEET_IN_METRE; | ||
return index.divide(value, FEET_IN_METRE); | ||
} | ||
@@ -327,14 +461,16 @@ | ||
function speedToKmh(speed, options = {}) { | ||
return applyOptions(speed.value * SECONDS_IN_HOUR * KILOMETRES_IN_METRE, options); | ||
return applyOptions(index.times(speed.value, SECONDS_IN_HOUR, KILOMETRES_IN_METRE), options); | ||
} | ||
function speedToMph(speed, options = {}) { | ||
return applyOptions(speed.value * SECONDS_IN_HOUR * MILES_IN_METRE, options); | ||
return applyOptions(index.times(speed.value, SECONDS_IN_HOUR, MILES_IN_METRE), options); | ||
} | ||
function speedToKnots(speed, options = {}) { | ||
return applyOptions(speed.value * SECONDS_IN_HOUR * NAUTICAL_MILES_IN_METRE, options); | ||
return applyOptions(index.times(speed.value, SECONDS_IN_HOUR, NAUTICAL_MILES_IN_METRE), options); | ||
} | ||
function speedToFps(speed, options = {}) { | ||
return applyOptions(speed.value * FEET_IN_METRE, options); | ||
return applyOptions(index.times(speed.value, FEET_IN_METRE), options); | ||
} | ||
index.enableBoundaryChecking(false); | ||
(function (TemperatureUnit) { | ||
@@ -349,3 +485,3 @@ TemperatureUnit["kelvin"] = "k"; | ||
function celsiusToKelvin(value) { | ||
return value + KELIN_OFFSET; | ||
return index.plus(value, KELIN_OFFSET); | ||
} | ||
@@ -355,3 +491,3 @@ | ||
// eslint-disable-next-line no-magic-numbers | ||
return (value - 32) * (5 / 9) + KELIN_OFFSET; | ||
return index.plus(index.times(index.minus(value, 32), index.divide(5, 9)), KELIN_OFFSET); | ||
} | ||
@@ -387,7 +523,7 @@ | ||
function temperatureToCelsius(temperature, options = {}) { | ||
return applyOptions(temperature.value - KELIN_OFFSET, options); | ||
return applyOptions(index.minus(temperature.value, KELIN_OFFSET), options); | ||
} | ||
function temperatureToFahrenheit(temperature, options = {}) { | ||
// eslint-disable-next-line no-magic-numbers | ||
return applyOptions((temperature.value - KELIN_OFFSET) * (9 / 5) + 32, options); | ||
return applyOptions( // eslint-disable-next-line no-magic-numbers | ||
index.plus(index.times(index.minus(temperature.value, KELIN_OFFSET), index.divide(9, 5)), 32), options); | ||
} | ||
@@ -394,0 +530,0 @@ |
{ | ||
"name": "measure-fns", | ||
"version": "0.1.0", | ||
"version": "0.3.0", | ||
"description": "Functions for conversion and handling of measurements", | ||
@@ -58,3 +58,6 @@ "main": "dist/index.js", | ||
"functional" | ||
] | ||
], | ||
"dependencies": { | ||
"number-precision": "^1.4.0" | ||
} | ||
} |
23057
588
1
+ Addednumber-precision@^1.4.0
+ Addednumber-precision@1.6.0(transitive)