From f33a31a7e6c1552cd12e0213e9f2580368f5b45c Mon Sep 17 00:00:00 2001 From: Charles Pick Date: Fri, 21 Nov 2014 23:24:46 +0000 Subject: [PATCH] add RID::equals() --- lib/recordid.js | 23 +++++++++++++++++++++++ test/core/recordid.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 test/core/recordid.js diff --git a/lib/recordid.js b/lib/recordid.js index 27012e0..3d2e449 100644 --- a/lib/recordid.js +++ b/lib/recordid.js @@ -74,6 +74,29 @@ RecordID.prototype.isValid = function () { return this.cluster == +this.cluster && this.position == +this.position; }; +/** + * Determine whether the record id is equal to another. + * + * @param {String|RID} rid The RID to compare with. + * @return {Boolean} If the RID matches, then true. + */ +RecordID.prototype.equals = function (rid) { + if (rid === this) { + return true; + } + else if (typeof rid === 'string') { + return this.toString() === rid; + } + else if (rid instanceof RecordID) { + return rid.cluster === this.cluster && rid.position === this.position; + } + else if ((rid = RecordID.parse(rid))) { + return rid.cluster === this.cluster && rid.position === this.position; + } + else { + return false; + } +}; /** * Parse a record id into a RecordID object. diff --git a/test/core/recordid.js b/test/core/recordid.js new file mode 100644 index 0000000..a64a76c --- /dev/null +++ b/test/core/recordid.js @@ -0,0 +1,28 @@ +describe('RecordID', function () { + var rid = LIB.RID('#1:23'); + + describe('RecordID::equals()', function () { + it('should equal an identical record', function () { + rid.equals(rid).should.be.true; + }); + it('should equal a string representation of the record', function () { + rid.equals("#1:23").should.be.true; + }); + it('should not equal a different record', function () { + rid.equals(LIB.RID("4:56")).should.be.false; + }); + it('should not equal a string representation a different record', function () { + rid.equals("4:56").should.be.false; + }); + it('should equal an identical record expressed as a POJO', function () { + rid.equals({cluster: 1, position: 23}).should.be.true; + }); + it('should not equal a different record expressed as a POJO', function () { + rid.equals({cluster: 4, position: 56}).should.be.false; + }); + it('should not equal nonsense', function () { + rid.equals(false).should.be.false; + rid.equals("blah").should.be.false; + }) + }); +}); \ No newline at end of file