Skip to content

Commit 8b5b6ad

Browse files
works
1 parent a93279a commit 8b5b6ad

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

src/objectid.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ export class ObjectId extends BSONValue {
3232
/** @internal */
3333
private static index = Math.floor(Math.random() * 0xffffff);
3434

35+
/**
36+
* Enables caching of hex string representations of ObjectIds.
37+
*/
3538
static cacheHexString: boolean = true;
3639

3740
/** ObjectId Bytes @internal */
@@ -44,15 +47,7 @@ export class ObjectId extends BSONValue {
4447
* not sufficient to determine if caching is enabled. `ObjectId.prototype.isCached()` can be used to
4548
* determine if the hex string has been cached yet for an ObjectId.
4649
*/
47-
#_hexString: string | null = null;
48-
49-
get #hexString(): string | null {
50-
return this.#_hexString;
51-
}
52-
53-
set #hexString(s: string) {
54-
this.#_hexString = s.toLowerCase();
55-
}
50+
#cachedHexString: string | null = null;
5651

5752
/** To generate a new ObjectId, use ObjectId() with no argument. */
5853
constructor();
@@ -121,7 +116,7 @@ export class ObjectId extends BSONValue {
121116
this.buffer = ByteUtils.fromHex(workingId);
122117
// If we are caching the hex string
123118
if (ObjectId.cacheHexString) {
124-
this.#hexString = workingId;
119+
this.#cachedHexString = workingId.toLowerCase();
125120
}
126121
} else {
127122
throw new BSONError(
@@ -144,7 +139,7 @@ export class ObjectId extends BSONValue {
144139
set id(value: Uint8Array) {
145140
this.buffer = value;
146141
if (ObjectId.cacheHexString) {
147-
this.#hexString = ByteUtils.toHex(value);
142+
this.#cachedHexString = ByteUtils.toHex(value);
148143
}
149144
}
150145

@@ -173,12 +168,12 @@ export class ObjectId extends BSONValue {
173168

174169
/** Returns the ObjectId id as a 24 lowercase character hex string representation */
175170
toHexString(): string {
176-
if (this.#hexString) return this.#hexString;
171+
if (this.#cachedHexString) return this.#cachedHexString.toLowerCase();
177172

178173
const hexString = ByteUtils.toHex(this.id);
179174

180175
if (ObjectId.cacheHexString) {
181-
this.#hexString = hexString;
176+
this.#cachedHexString = hexString;
182177
}
183178

184179
return hexString;
@@ -382,7 +377,7 @@ export class ObjectId extends BSONValue {
382377
* used for testing
383378
*/
384379
private isCached(): boolean {
385-
return ObjectId.cacheHexString && this.#hexString != null;
380+
return ObjectId.cacheHexString && this.#cachedHexString != null;
386381
}
387382

388383
/**

0 commit comments

Comments
 (0)