Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
use strange to provide full-featured range class
Browse files Browse the repository at this point in the history
  • Loading branch information
benesch committed Jul 17, 2016
1 parent f4b2f18 commit e3c2434
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 98 deletions.
49 changes: 13 additions & 36 deletions lib/range.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var util = require("util");

var VALID_BOUNDS = ["[]", "[)", "(]", "()"];
var Range = require("strange")
, util = require("util");

function formatBound(value, prepare) {
if (value === null) {
Expand All @@ -15,47 +14,25 @@ function formatBound(value, prepare) {
return value;
}

function Range(lower, upper, bounds) {
if (!(this instanceof Range)) {
return new Range(lower, upper, bounds);
}

if (!lower && !upper && !bounds) {
this.empty = true;
return;
}

bounds = bounds || "[)";
if (VALID_BOUNDS.indexOf(bounds) === -1) {
throw new Error(util.format("invalid bounds: %s", bounds));
function PGRange(begin, end, bounds) {
if (!(this instanceof PGRange)) {
return new PGRange(begin, end, bounds);
}

this.lower = lower;
this.upper = upper;
this.bounds = bounds;
Range.call(this, begin, end, bounds);
}

Range.prototype.toPostgres = function (prepare) {
if (this.empty) {
util.inherits(PGRange, Range);

PGRange.prototype.toPostgres = function (prepare) {
if (this.isEmpty()) {
return "empty";
}

return util.format("%s%s,%s%s",
this.bounds[0],
formatBound(this.lower, prepare),
formatBound(this.upper, prepare),
formatBound(this.begin, prepare),
formatBound(this.end, prepare),
this.bounds[1]);
};

Range.prototype.toJSON = function () {
if (this.empty) {
return { lower: null, upper: null, bounds: null };
}
return {
lower: this.lower,
upper: this.upper,
bounds: this.bounds
};
};

module.exports = Range;
module.exports = PGRange;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"sinon-chai": "^2.5.0"
},
"dependencies": {
"lodash": "^2.4.1"
"lodash": "^2.4.1",
"strange": "^1.3.0"
}
}
40 changes: 20 additions & 20 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,64 +21,64 @@ describe("parser", function () {
describe(".parseRange", function () {
it("should parse [] finite ranges", function () {
var range = parser.parseRange(_.identity, "[1,2]");
range.lower.should.equal("1");
range.upper.should.equal("2");
range.begin.should.equal("1");
range.end.should.equal("2");
range.bounds.should.equal("[]");
});

it("should parse [) finite ranges", function () {
var range = parser.parseRange(_.identity, "[1,2)");
range.lower.should.equal("1");
range.upper.should.equal("2");
range.begin.should.equal("1");
range.end.should.equal("2");
range.bounds.should.equal("[)");
});

it("should parse (] finite ranges", function () {
var range = parser.parseRange(_.identity, "(1,2]");
range.lower.should.equal("1");
range.upper.should.equal("2");
range.begin.should.equal("1");
range.end.should.equal("2");
range.bounds.should.equal("(]");
});

it("should parse () finite ranges", function () {
var range = parser.parseRange(_.identity, "(1,2)");
range.lower.should.equal("1");
range.upper.should.equal("2");
range.begin.should.equal("1");
range.end.should.equal("2");
range.bounds.should.equal("()");
});

it("should parse quoted range bounds", function () {
var range = parser.parseRange(_.identity, '("1, 2","3, 4")'); // jshint ignore:line
range.lower.should.equal("1, 2");
range.upper.should.equal("3, 4");
range.begin.should.equal("1, 2");
range.end.should.equal("3, 4");
});

it("should unescape quoted range bounds", function () {
// jshint ignore:start
var range = parser.parseRange(_.identity, '("\\"cows\\"","\\"moos\\"")');
range.lower.should.equal('"cows"');
range.upper.should.equal('"moos"');
range.begin.should.equal('"cows"');
range.end.should.equal('"moos"');
// jshint ignore:end
});

it("should parse lower bound infinite ranges", function () {
it("should parse begin bound infinite ranges", function () {
var range = parser.parseRange(_.identity, "[,2)");
should.not.exist(range.lower);
range.upper.should.equal("2");
should.not.exist(range.begin);
range.end.should.equal("2");
range.bounds.should.equal("[)");
});

it("should parse upper bound infinite ranges", function () {
it("should parse end bound infinite ranges", function () {
var range = parser.parseRange(_.identity, "[2,)");
range.lower.should.equal("2");
should.not.exist(range.upper);
range.begin.should.equal("2");
should.not.exist(range.end);
range.bounds.should.equal("[)");
});

it("should parse both bound infinite ranges", function () {
var range = parser.parseRange(_.identity, "[,)");
should.not.exist(range.lower);
should.not.exist(range.upper);
should.not.exist(range.begin);
should.not.exist(range.end);
range.bounds.should.equal("[)");
});
});
Expand Down
44 changes: 3 additions & 41 deletions test/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,12 @@ describe("Range", function () {
}).should.not.throw();
});

it("should reject ranges with invalid string bounds", function () {
(function () {
Range(7, 9, ")(");
}).should.throw(/invalid bounds/);
});

it("should reject ranges with non-string bounds", function () {
(function () {
Range(7, 9, {});
}).should.throw(/invalid bounds/);
});

it("should use default bounds [) if unspecified", function () {
Range(1, 2).bounds.should.equal("[)");
it("should use default bounds [] if unspecified", function () {
Range(1, 2).bounds.should.equal("[]");
});

it("should support empty ranges", function () {
Range().empty.should.be.true;
Range().isEmpty().should.be.true;
});
});

Expand Down Expand Up @@ -109,30 +97,4 @@ describe("Range", function () {
});
});
});

describe("toJSON", function () {
it("handles finite ranges", function () {
Range(1, 2, "[]").toJSON().should.deep.equal({
lower: 1,
upper: 2,
bounds: "[]"
});
});

it("handles infinite ranges", function () {
Range(null, null, "[]").toJSON().should.deep.equal({
lower: null,
upper: null,
bounds: "[]"
});
});

it("handles empty ranges", function () {
Range().toJSON().should.deep.equal({
lower: null,
upper: null,
bounds: null
});
});
});
});

0 comments on commit e3c2434

Please sign in to comment.