Skip to content

Commit 616665f

Browse files
authored
fix: make inspection result of BSON types evaluable (#416)
Make sure that evaluating the inspection result of a BSON type returns a result that can be evaluated to give an object equivalent to the original. NODE-2947
1 parent b707f65 commit 616665f

File tree

14 files changed

+30
-24
lines changed

14 files changed

+30
-24
lines changed

src/binary.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ export class Binary {
263263

264264
inspect(): string {
265265
const asBuffer = this.value(true);
266-
return `Binary("${asBuffer.toString('hex')}", ${this.sub_type})`;
266+
return `new Binary(Buffer.from("${asBuffer.toString('hex')}", "hex"), ${this.sub_type})`;
267267
}
268268
}
269269

src/code.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export class Code {
5050

5151
inspect(): string {
5252
const codeJson = this.toJSON();
53-
return `Code("${codeJson.code}"${codeJson.scope ? `, ${JSON.stringify(codeJson.scope)}` : ''})`;
53+
return `new Code("${codeJson.code}"${
54+
codeJson.scope ? `, ${JSON.stringify(codeJson.scope)}` : ''
55+
})`;
5456
}
5557
}
5658

src/db_ref.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ export class DBRef {
108108
// NOTE: if OID is an ObjectId class it will just print the oid string.
109109
const oid =
110110
this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
111-
return `DBRef("${this.namespace}", "${oid}"${this.db ? `, "${this.db}"` : ''})`;
111+
return `new DBRef("${this.namespace}", new ObjectId("${oid}")${
112+
this.db ? `, "${this.db}"` : ''
113+
})`;
112114
}
113115
}
114116

src/decimal128.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ export class Decimal128 {
794794
}
795795

796796
inspect(): string {
797-
return `Decimal128("${this.toString()}")`;
797+
return `Decimal128.fromString("${this.toString()}")`;
798798
}
799799
}
800800

src/double.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class Double {
7878

7979
inspect(): string {
8080
const eJSON = this.toExtendedJSON() as DoubleExtended;
81-
return `Double(${eJSON.$numberDouble})`;
81+
return `new Double(${eJSON.$numberDouble})`;
8282
}
8383
}
8484

src/int_32.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class Int32 {
5757
}
5858

5959
inspect(): string {
60-
return `Int32(${this.valueOf()})`;
60+
return `new Int32(${this.valueOf()})`;
6161
}
6262
}
6363

src/long.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ export class Long {
992992
}
993993

994994
inspect(): string {
995-
return `Long("${this.toString()}")`;
995+
return `new Long("${this.toString()}")`;
996996
}
997997
}
998998

src/max_key.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class MaxKey {
2626
}
2727

2828
inspect(): string {
29-
return 'MaxKey()';
29+
return 'new MaxKey()';
3030
}
3131
}
3232

src/min_key.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class MinKey {
2626
}
2727

2828
inspect(): string {
29-
return 'MinKey()';
29+
return 'new MinKey()';
3030
}
3131
}
3232

src/objectid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ export class ObjectId {
350350
}
351351

352352
inspect(): string {
353-
return `ObjectId("${this.toHexString()}")`;
353+
return `new ObjectId("${this.toHexString()}")`;
354354
}
355355
}
356356

0 commit comments

Comments
 (0)