Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mumath

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mumath - npm Package Compare versions

Comparing version 0.2.3 to 0.2.4

.travis.yml

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",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc