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

bounds

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bounds - npm Package Compare versions

Comparing version 0.0.1 to 1.0.0

6

component.json

@@ -5,3 +5,3 @@ {

"description": "Mixin for checking if value is inside or outside of bounds",
"version": "0.0.1",
"version": "1.0.0",
"keywords": [

@@ -11,3 +11,5 @@ "range",

],
"dependencies": {},
"dependencies": {
"component/clone": "*"
},
"development": {},

@@ -14,0 +16,0 @@ "license": "MIT",

@@ -0,3 +1,18 @@

var clone;
if ('undefined' == typeof window) {
clone = require('clone-component');
} else {
clone = require('clone');
}
module.exports = Bounds;
function calculateReversed(self) {
return self._min
&& self._max
&& self.before(self._max);
}
function Bounds(obj) {

@@ -19,4 +34,13 @@ if (obj) return mixin(obj);

Bounds.prototype.distance = function(fn) {
this._distance = fn;
return this;
};
Bounds.prototype.min = function(v) {
if (!arguments.length) {
return this._min;
}
this._min = v;
delete this._reversed;
return this;

@@ -26,3 +50,7 @@ };

Bounds.prototype.max = function(v) {
if (!arguments.length) {
return this._max;
}
this._max = v;
delete this._reversed;
return this;

@@ -39,3 +67,2 @@ };

Bounds.prototype.invalid =
Bounds.prototype.out = function(v) {

@@ -45,3 +72,2 @@ return this.before(v) || this.after(v);

Bounds.prototype.valid =
Bounds.prototype.in = function(v) {

@@ -51,1 +77,37 @@ return !this.out(v);

Bounds.prototype.valid = function(v) {
if (this.reversed()) {
return !this.after(v) || !this.before(v);
}
return this.in(v);
};
Bounds.prototype.invalid = function(v) {
return !this.valid(v);
};
Bounds.prototype.reversed = function() {
if (this._reversed === undefined) {
this._reversed = calculateReversed(this);
}
return this._reversed;
};
Bounds.prototype.restrict = function(v) {
if (this.reversed()) {
if(this.after(v) && this.before(v)) {
// select closer bound
return (this._distance(this._max, v) < this._distance(v, this._min))
? clone(this._max)
: clone(this._min);
}
return v;
}
if(this.before(v)) {
return clone(this._min);
}
if(this.after(v)) {
return clone(this._max);
}
return v;
};
{
"name": "bounds",
"version": "0.0.1",
"version": "1.0.0",
"description": "Mixin for checking if value is inside or outside of bounds",

@@ -25,3 +25,6 @@ "main": "index.js",

"jshint": "~0.9.1"
},
"dependencies": {
"clone-component": "~0.1.0"
}
}
# bounds
Mixin for checking if value is inside or outside of bounds
You can use Bounds for any objects for which you can define compare function (dates, vectors etc.)
Mixin for checking if value is inside or outside of bounds You can use `Bounds` for any objects for
which you can define compare function (dates, vectors etc.)
In addition to regular range checking `Bounds` supports reversed ranges: if
min is bigger than max it considers values outside ot the max, min range as valid.
## Installation

@@ -11,2 +14,6 @@

or
$ npm install bounds
## API

@@ -36,12 +43,27 @@

### .before
### .before(v)
Return `true` if `v < min`
### .after
### .after(v)
Return `true` if `v > max`
### .valid(v)
For regular ranges it's the same as `in`. For reversed ranges it considers as valid values that are
outside of the range (it's still inclusive so `min` and `max` are still considered valid)
### .restrict(v)
Returns the passed value for `valid` values. For invalid values returns the closest boundary (`min`
or `max`). `restrict` only works for reverse ranges if `distance` function is defined.
### .distance(fn)
Optional distance function: it's only used when calculating proper restriction for reversed ranges.
If restricted value is closed to `min` than to `max`, then the `min` is returned.
## License
MIT

@@ -9,2 +9,6 @@ var Bounds = require('../index.js');

function numdistance(a, b) {
return Math.abs(a - b);
}
function strcmp(a, b) {

@@ -23,3 +27,3 @@ return (a < b) ? -1 : (a > b ? 1 : 0);

it('should consider dates inside of the range as valid', function(){
it('should consider values inside of the range as valid', function(){
var b = new Bounds()

@@ -36,4 +40,18 @@ .compare(numcmp)

assert.ok(b.after(17));
assert.equal(b.min(), -2);
assert.equal(b.max(), 15);
});
it('should consider values outside of the range as valid, when reversed', function(){
var b = new Bounds()
.compare(numcmp)
.max(-2)
.min(15);
assert.ok(b.valid(-3));
assert.ok(b.valid(-2));
assert.ok(b.invalid(0));
assert.ok(b.valid(15));
assert.ok(b.valid(16));
});
it('should work if only min is specified', function(){

@@ -56,2 +74,68 @@ var b = new Bounds()

});
it('should restrict values to min or max', function() {
var b = new Bounds()
.compare(strcmp)
.min('abc')
.max('pqrs');
assert.equal('pqrs', b.restrict('z'));
assert.equal('abc', b.restrict('a'));
assert.equal('bef', b.restrict('bef'));
assert.equal('pqrs', b.restrict('pqrs'));
});
it('should restrict reverse ranges to min or max', function() {
var b = new Bounds()
.compare(strcmp)
.max('abc')
.min('pqrs');
assert.equal('z', b.restrict('z'));
assert.equal('a', b.restrict('a'));
assert.equal('pqrs', b.restrict('pqrs'));
});
it('should restrict to min or max depending on the distance', function(){
var b = new Bounds()
.distance(numdistance)
.compare(numcmp)
.max(-2)
.min(15);
assert.equal(-2, b.restrict(-2));
assert.equal(-2, b.restrict(0));
assert.equal(15, b.restrict(13));
assert.equal(15, b.restrict(15));
});
it('should reset reversed state', function(){
var b = new Bounds();
assert.ok(!b.reversed());
b.compare(numcmp);
assert.ok(!b.reversed());
b.min(3);
assert.ok(!b.reversed());
b.max(10);
assert.ok(!b.reversed());
b.min(13);
assert.ok(b.reversed());
b.min(undefined);
assert.ok(!b.reversed());
b.min(13);
assert.ok(b.reversed());
b.max(undefined);
assert.ok(!b.reversed());
});
});
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