Skip to content

Commit

Permalink
Merge pull request #120 from imbrn/fix/range-rules
Browse files Browse the repository at this point in the history
Fix bug with `Infinity` values in range based rules
  • Loading branch information
imbrn authored Aug 29, 2018
2 parents 7fac227 + 655ff14 commit 0bfcd3d
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed

- Bug with polyfill for `integer` rule
- Bug with `Infinity` values in range-based rules

## [1.2.1] - 2018-08-17

Expand Down
8 changes: 4 additions & 4 deletions src/v8n.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ const availableRules = {

range: makeTestRange(undefined, undefined, true, true),

lessThan: makeTestRange(-Infinity),
lessThan: makeTestRange(-Infinity, undefined, true),

lessThanOrEqual: makeTestRange(-Infinity, undefined, undefined, true),
lessThanOrEqual: makeTestRange(-Infinity, undefined, true, true),

greaterThan: makeTestRange(undefined, Infinity),
greaterThan: makeTestRange(undefined, Infinity, false, true),

greaterThanOrEqual: makeTestRange(undefined, Infinity, true),
greaterThanOrEqual: makeTestRange(undefined, Infinity, true, true),

// Divisible

Expand Down
130 changes: 130 additions & 0 deletions src/v8n.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -634,13 +634,41 @@ describe("rules", () => {
expect(is.test(-4)).toBeTruthy();
expect(is.test(3)).toBeFalsy();
expect(is.test(4)).toBeFalsy();
expect(is.test(-Infinity)).toBeTruthy();
expect(is.test(Infinity)).toBeFalsy();

const not = v8n().not.lessThan(3);
expect(not.test(1)).toBeFalsy();
expect(not.test(2)).toBeFalsy();
expect(not.test(-4)).toBeFalsy();
expect(not.test(3)).toBeTruthy();
expect(not.test(4)).toBeTruthy();
expect(not.test(-Infinity)).toBeFalsy();
expect(not.test(Infinity)).toBeTruthy();

expect(
v8n()
.lessThan(-Infinity)
.test(-Infinity)
).toBeFalsy();

expect(
v8n()
.lessThan(-Infinity)
.test(Infinity)
).toBeFalsy();

expect(
v8n()
.lessThan(Infinity)
.test(-Infinity)
).toBeTruthy();

expect(
v8n()
.lessThan(Number.MIN_SAFE_INTEGER)
.test(-Infinity)
).toBeTruthy();
});

test("lessThanOrEqualTo", () => {
Expand All @@ -651,6 +679,8 @@ describe("rules", () => {
expect(is.test(2)).toBeTruthy();
expect(is.test(3)).toBeTruthy();
expect(is.test(4)).toBeFalsy();
expect(is.test(-Infinity)).toBeTruthy();
expect(is.test(Infinity)).toBeFalsy();

const not = v8n().not.lessThanOrEqual(3);
expect(not.test(-4)).toBeFalsy();
Expand All @@ -659,6 +689,32 @@ describe("rules", () => {
expect(not.test(2)).toBeFalsy();
expect(not.test(3)).toBeFalsy();
expect(not.test(4)).toBeTruthy();
expect(not.test(-Infinity)).toBeFalsy();
expect(not.test(Infinity)).toBeTruthy();

expect(
v8n()
.lessThanOrEqual(-Infinity)
.test(-Infinity)
).toBeTruthy();

expect(
v8n()
.lessThanOrEqual(-Infinity)
.test(Infinity)
).toBeFalsy();

expect(
v8n()
.lessThanOrEqual(Infinity)
.test(-Infinity)
).toBeTruthy();

expect(
v8n()
.lessThanOrEqual(Number.MIN_SAFE_INTEGER)
.test(-Infinity)
).toBeTruthy();
});

test("greaterThan", () => {
Expand All @@ -667,12 +723,46 @@ describe("rules", () => {
expect(is.test(-3)).toBeFalsy();
expect(is.test(3)).toBeFalsy();
expect(is.test(4)).toBeTruthy();
expect(is.test(-Infinity)).toBeFalsy();
expect(is.test(Infinity)).toBeTruthy();

const not = v8n().not.greaterThan(3);
expect(not.test(2)).toBeTruthy();
expect(not.test(-3)).toBeTruthy();
expect(not.test(3)).toBeTruthy();
expect(not.test(4)).toBeFalsy();
expect(not.test(-Infinity)).toBeTruthy();
expect(not.test(Infinity)).toBeFalsy();

expect(
v8n()
.greaterThan(-Infinity)
.test(-Infinity)
).toBeFalsy();

expect(
v8n()
.greaterThan(-Infinity)
.test(Infinity)
).toBeTruthy();

expect(
v8n()
.greaterThan(Infinity)
.test(-Infinity)
).toBeFalsy();

expect(
v8n()
.greaterThan(Number.MIN_SAFE_INTEGER)
.test(-Infinity)
).toBeFalsy();

expect(
v8n()
.greaterThan(Number.MAX_SAFE_INTEGER)
.test(Infinity)
).toBeTruthy();
});

test("greaterThanOrEqual", () => {
Expand All @@ -681,12 +771,52 @@ describe("rules", () => {
expect(is.test(-3)).toBeFalsy();
expect(is.test(3)).toBeTruthy();
expect(is.test(4)).toBeTruthy();
expect(is.test(-Infinity)).toBeFalsy();
expect(is.test(Infinity)).toBeTruthy();

const not = v8n().not.greaterThanOrEqual(3);
expect(not.test(2)).toBeTruthy();
expect(not.test(-3)).toBeTruthy();
expect(not.test(3)).toBeFalsy();
expect(not.test(4)).toBeFalsy();
expect(not.test(-Infinity)).toBeTruthy();
expect(not.test(Infinity)).toBeFalsy();

expect(
v8n()
.greaterThanOrEqual(-Infinity)
.test(-Infinity)
).toBeTruthy();

expect(
v8n()
.greaterThanOrEqual(-Infinity)
.test(Infinity)
).toBeTruthy();

expect(
v8n()
.greaterThanOrEqual(Infinity)
.test(-Infinity)
).toBeFalsy();

expect(
v8n()
.greaterThanOrEqual(Number.MIN_SAFE_INTEGER)
.test(-Infinity)
).toBeFalsy();

expect(
v8n()
.greaterThanOrEqual(Number.MAX_SAFE_INTEGER)
.test(Infinity)
).toBeTruthy();

expect(
v8n()
.greaterThanOrEqual(Infinity)
.test(Infinity)
).toBeTruthy();
});

test("range", () => {
Expand Down

0 comments on commit 0bfcd3d

Please sign in to comment.