Comparing version 0.2.3 to 0.2.4
132
index.js
@@ -0,11 +1,14 @@ | ||
var wrap = require('./wrap'); | ||
/** | ||
* Simple math utils. | ||
* | ||
* @module mumath | ||
*/ | ||
module.exports = { | ||
between: wrap(between), | ||
isBetween: wrap(isBetween), | ||
toPrecision: wrap(toPrecision), | ||
getPrecision: getPrecision, | ||
between: wrap(require('./between')), | ||
isBetween: wrap(require('./is-between')), | ||
toPrecision: wrap(require('./to-precision')), | ||
getPrecision: require('./get-precision'), | ||
min: wrap(Math.min), | ||
@@ -16,3 +19,3 @@ max: wrap(Math.max), | ||
div: wrap(function(a,b){return a/b}), | ||
mul: wrap(function(a,b){return a*b}), | ||
mult: wrap(function(a,b){return a*b}), | ||
mod: wrap(function(a,b){return a%b}), | ||
@@ -22,117 +25,2 @@ floor: wrap(function(a){return Math.floor(a)}), | ||
round: wrap(function(a){return Math.round(a)}) | ||
}; | ||
/** | ||
* Get fn wrapped with array/object attrs recognition | ||
* | ||
* @return {Function} Target function | ||
*/ | ||
function wrap(fn){ | ||
return function(a){ | ||
var args = arguments; | ||
if (a instanceof Array) { | ||
var result = new Array(a.length), slice; | ||
for (var i = 0; i < a.length; i++){ | ||
slice = []; | ||
for (var j = 0, l = args.length, val; j < l; j++){ | ||
val = args[j] instanceof Array ? args[j][i] : args[j]; | ||
val = val || 0; | ||
slice.push(val); | ||
} | ||
result[i] = fn.apply(this, slice); | ||
} | ||
return result; | ||
} | ||
else if (typeof a === 'object') { | ||
var result = {}, slice; | ||
for (var i in a){ | ||
slice = []; | ||
for (var j = 0, l = args.length, val; j < l; j++){ | ||
val = typeof args[j] === 'object' ? args[j][i] : args[j]; | ||
val = val || 0; | ||
slice.push(val); | ||
} | ||
result[i] = fn.apply(this, slice); | ||
} | ||
return result; | ||
} | ||
else { | ||
return fn.apply(this, args); | ||
} | ||
}; | ||
} | ||
/** | ||
* Clamper. | ||
* Detects proper clamp min/max. | ||
* | ||
* @param {number} a Current value to cut off | ||
* @param {number} min One side limit | ||
* @param {number} max Other side limit | ||
* | ||
* @return {number} Clamped value | ||
*/ | ||
function between(a, min, max){ | ||
return max > min ? Math.max(Math.min(a,max),min) : Math.max(Math.min(a,min),max); | ||
} | ||
/** | ||
* Whether element is between left & right including | ||
* | ||
* @param {number} a | ||
* @param {number} left | ||
* @param {number} right | ||
* | ||
* @return {Boolean} | ||
*/ | ||
function isBetween(a, left, right){ | ||
if (a <= right && a >= left) return true; | ||
return false; | ||
} | ||
/** | ||
* Precision round | ||
* | ||
* @param {number} value | ||
* @param {number} step Minimal discrete to round | ||
* | ||
* @return {number} | ||
* | ||
* @example | ||
* toPrecision(213.34, 1) == 213 | ||
* toPrecision(213.34, .1) == 213.3 | ||
* toPrecision(213.34, 10) == 210 | ||
*/ | ||
function toPrecision(value, step) { | ||
step = parseFloat(step); | ||
if (step === 0) return value; | ||
value = Math.round(value / step) * step; | ||
return parseFloat(value.toFixed(getPrecision(step))); | ||
} | ||
/** | ||
* Get precision from float: | ||
* | ||
* @example | ||
* 1.1 → 1, 1234 → 0, .1234 → 4 | ||
* | ||
* @param {number} n | ||
* | ||
* @return {number} decimap places | ||
*/ | ||
function getPrecision(n){ | ||
var s = n + '', | ||
d = s.indexOf('.') + 1; | ||
return !d ? 0 : s.length - d; | ||
} | ||
}; |
{ | ||
"name": "mumath", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "Micro math methods: isBetween, between, getPrecition, toPrecision", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4557
10
130
1