Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/recordid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions test/core/recordid.js
Original file line number Diff line number Diff line change
@@ -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;
})
});
});