Skip to content

Commit fb33d30

Browse files
PR requested changes 3
1 parent c003671 commit fb33d30

File tree

16 files changed

+137
-89
lines changed

16 files changed

+137
-89
lines changed

src/binary.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
type InspectParameterFn,
3-
defaultInspect,
4-
isAnyArrayBuffer,
5-
isUint8Array
6-
} from './parser/utils';
1+
import { type InspectFn, defaultInspect, isAnyArrayBuffer, isUint8Array } from './parser/utils';
72
import type { EJSONOptions } from './extended_json';
83
import { BSONError } from './error';
94
import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants';
@@ -268,15 +263,11 @@ export class Binary extends BSONValue {
268263
return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
269264
}
270265

271-
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
272-
const addQuotes = !inspect;
266+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
273267
inspect ??= defaultInspect;
274268
const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
275269
const base64Arg = inspect(base64, options);
276270
const subTypeArg = inspect(this.sub_type, options);
277-
if (addQuotes) {
278-
return `Binary.createFromBase64('${base64Arg}', ${subTypeArg})`;
279-
}
280271
return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`;
281272
}
282273
}
@@ -472,12 +463,8 @@ export class UUID extends Binary {
472463
* @returns return the 36 character hex string representation.
473464
*
474465
*/
475-
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
476-
const addQuotes = !inspect;
466+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
477467
inspect ??= defaultInspect;
478-
if (addQuotes) {
479-
return `new UUID('${inspect(this.toHexString(), options)}')`;
480-
}
481468
return `new UUID(${inspect(this.toHexString(), options)})`;
482469
}
483470
}

src/bson_value.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BSON_MAJOR_VERSION } from './constants';
2-
import { type InspectParameterFn } from './parser/utils';
2+
import { type InspectFn } from './parser/utils';
33

44
/** @public */
55
export abstract class BSONValue {
@@ -14,13 +14,17 @@ export abstract class BSONValue {
1414
[Symbol.for('nodejs.util.inspect.custom')](
1515
depth?: number,
1616
options?: unknown,
17-
inspect?: InspectParameterFn
17+
inspect?: InspectFn
1818
): string {
1919
return this.inspect(depth, options, inspect);
2020
}
2121

22-
/** @public */
23-
public abstract inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string;
22+
/**
23+
* @public
24+
* Prints a human-readable string of BSON value information
25+
* If invoked manually without node.js.inspect function, this will default to a modified JSON.stringify
26+
*/
27+
public abstract inspect(depth?: number, options?: unknown, inspect?: InspectFn): string;
2428

2529
/** @internal */
2630
abstract toExtendedJSON(): unknown;

src/code.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Document } from './bson';
22
import { BSONValue } from './bson_value';
3-
import { type InspectParameterFn, defaultInspect } from './parser/utils';
3+
import { type InspectFn, defaultInspect } from './parser/utils';
44

55
/** @public */
66
export interface CodeExtended {
@@ -56,7 +56,7 @@ export class Code extends BSONValue {
5656
return new Code(doc.$code, doc.$scope);
5757
}
5858

59-
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
59+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
6060
inspect ??= defaultInspect;
6161
let parametersString = inspect(this.code, options);
6262
const multiLineFn = parametersString.includes('\n');

src/db_ref.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { Document } from './bson';
22
import { BSONValue } from './bson_value';
33
import type { EJSONOptions } from './extended_json';
44
import type { ObjectId } from './objectid';
5-
import { type InspectParameterFn, defaultInspect } from './parser/utils';
5+
import { type InspectFn, defaultInspect } from './parser/utils';
66

77
/** @public */
88
export interface DBRefLike {
@@ -112,18 +112,17 @@ export class DBRef extends BSONValue {
112112
return new DBRef(doc.$ref, doc.$id, doc.$db, copy);
113113
}
114114

115-
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
116-
const addQuotes = !inspect;
115+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
117116
inspect ??= defaultInspect;
118117

119118
const args = [
120119
inspect(this.namespace, options),
121120
inspect(this.oid, options),
122121
...(this.db ? [inspect(this.db, options)] : []),
123122
...(Object.keys(this.fields).length > 0 ? [inspect(this.fields, options)] : [])
124-
].map(arg => (addQuotes ? `'${arg}'` : arg));
123+
];
125124

126-
args[1] = addQuotes ? `new ObjectId(${args[1]})` : args[1];
125+
args[1] = inspect === defaultInspect ? `new ObjectId(${args[1]})` : args[1];
127126

128127
return `new DBRef(${args.join(', ')})`;
129128
}

src/decimal128.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BSONValue } from './bson_value';
22
import { BSONError } from './error';
33
import { Long } from './long';
4-
import { type InspectParameterFn, defaultInspect, isUint8Array } from './parser/utils';
4+
import { type InspectFn, defaultInspect, isUint8Array } from './parser/utils';
55
import { ByteUtils } from './utils/byte_utils';
66

77
const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
@@ -847,13 +847,9 @@ export class Decimal128 extends BSONValue {
847847
return Decimal128.fromString(doc.$numberDecimal);
848848
}
849849

850-
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
851-
const addQuotes = !inspect;
850+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
852851
inspect ??= defaultInspect;
853852
const d128string = inspect(this.toString(), options);
854-
if (addQuotes) {
855-
return `new Decimal128('${d128string}')`;
856-
}
857853
return `new Decimal128(${d128string})`;
858854
}
859855
}

src/double.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BSONValue } from './bson_value';
22
import type { EJSONOptions } from './extended_json';
3-
import { type InspectParameterFn, defaultInspect } from './parser/utils';
3+
import { type InspectFn, defaultInspect } from './parser/utils';
44

55
/** @public */
66
export interface DoubleExtended {
@@ -72,8 +72,8 @@ export class Double extends BSONValue {
7272
return options && options.relaxed ? doubleValue : new Double(doubleValue);
7373
}
7474

75-
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
75+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
7676
inspect ??= defaultInspect;
77-
return `new Double(${inspect(this.valueOf(), options)})`;
77+
return `new Double(${inspect(this.value, options)})`;
7878
}
7979
}

src/int_32.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BSONValue } from './bson_value';
22
import type { EJSONOptions } from './extended_json';
3-
import { type InspectParameterFn, defaultInspect } from './parser/utils';
3+
import { type InspectFn, defaultInspect } from './parser/utils';
44

55
/** @public */
66
export interface Int32Extended {
@@ -60,7 +60,7 @@ export class Int32 extends BSONValue {
6060
return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);
6161
}
6262

63-
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
63+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
6464
inspect ??= defaultInspect;
6565
return `new Int32(${inspect(this.value, options)})`;
6666
}

src/long.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BSONValue } from './bson_value';
22
import { BSONError } from './error';
33
import type { EJSONOptions } from './extended_json';
4-
import { type InspectParameterFn, defaultInspect } from './parser/utils';
4+
import { type InspectFn, defaultInspect } from './parser/utils';
55
import type { Timestamp } from './timestamp';
66

77
interface LongWASMHelpers {
@@ -1057,7 +1057,7 @@ export class Long extends BSONValue {
10571057
return longResult;
10581058
}
10591059

1060-
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
1060+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
10611061
inspect ??= defaultInspect;
10621062
const longVal = inspect(this.toString(), options);
10631063
const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : '';

src/objectid.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BSONValue } from './bson_value';
22
import { BSONError } from './error';
3-
import { type InspectParameterFn, defaultInspect } from './parser/utils';
3+
import { type InspectFn, defaultInspect } from './parser/utils';
44
import { BSONDataView, ByteUtils } from './utils/byte_utils';
55

66
// Regular expression that checks for hex value
@@ -296,12 +296,8 @@ export class ObjectId extends BSONValue {
296296
*
297297
* @returns return the 24 character hex string representation.
298298
*/
299-
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
300-
const addQuotes = !inspect;
299+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
301300
inspect ??= defaultInspect;
302-
if (addQuotes) {
303-
return `new ObjectId('${inspect(this.toHexString(), options)}')`;
304-
}
305301
return `new ObjectId(${inspect(this.toHexString(), options)})`;
306302
}
307303
}

src/parser/utils.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,29 @@ export function isDate(d: unknown): d is Date {
2828
return Object.prototype.toString.call(d) === '[object Date]';
2929
}
3030

31-
export type InspectParameterFn = (x: unknown, options: unknown) => string;
32-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
33-
export const defaultInspect: InspectParameterFn = v => `${v}`;
31+
export type InspectFn = (x: unknown, options?: unknown) => string;
32+
export function defaultInspect(x: unknown, _options?: unknown): string {
33+
return JSON.stringify(x, (k: string, v: unknown) => {
34+
if (typeof v === 'bigint') {
35+
return { $numberLong: `${v}` };
36+
} else if (isMap(v)) {
37+
return Object.fromEntries(v);
38+
}
39+
return v;
40+
});
41+
}
42+
43+
/** @internal */
44+
type StylizeFunction = (x: string, style: string) => string;
45+
/** @internal */
46+
export function getStylizeFunction(options?: unknown): StylizeFunction | undefined {
47+
const stylizeExists =
48+
options != null &&
49+
typeof options === 'object' &&
50+
'stylize' in options &&
51+
typeof options.stylize === 'function';
52+
53+
if (stylizeExists) {
54+
return options.stylize as StylizeFunction;
55+
}
56+
}

0 commit comments

Comments
 (0)