From cab603c29d518b0778f39bd8d6b2b33e3f544be7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petar=20Stoki=C4=87?= <5ar100kic@gmail.com> Date: Mon, 25 Dec 2023 17:21:17 +0100 Subject: [PATCH] fix str returning undefined --- objectid.js | 3 +++ test/test.js | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/objectid.js b/objectid.js index 3e9f8b8..5f6d49d 100644 --- a/objectid.js +++ b/objectid.js @@ -313,3 +313,6 @@ var inspect = (Symbol && Symbol.for && Symbol.for('nodejs.util.inspect.custom')) ObjectID.prototype[inspect] = function() { return "ObjectID("+this+")" }; ObjectID.prototype.toJSON = ObjectID.prototype.toHexString; ObjectID.prototype.toString = ObjectID.prototype.toHexString; +Object.defineProperty(ObjectID.prototype, "str", { + get: ObjectID.prototype.toHexString +}); diff --git a/test/test.js b/test/test.js index 3ec6720..64ed322 100644 --- a/test/test.js +++ b/test/test.js @@ -123,6 +123,12 @@ describe("ObjectIDs", function() { o.toString().should.eql("54495ad94c934721ede76d90"); }); + it("should convert to a hex string for ObjectID.str", function() { + var hexString = "54495ad94c934721ede76d90"; + var o = new ObjectID(hexString); + o.str.should.eql("54495ad94c934721ede76d90"); + }); + it("should throw and error if constructing with an invalid string", function() { (function(){ var o = new ObjectID("tttttttttttttttttttttttt");