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+
}

src/regexp.ts

Lines changed: 6 additions & 22 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 { defaultInspect } from './parser/utils';
4+
import { type InspectFn, defaultInspect, getStylizeFunction } from './parser/utils';
55

66
function alphabetize(str: string): string {
77
return str.split('').sort().join('');
@@ -104,27 +104,11 @@ export class BSONRegExp extends BSONValue {
104104
throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
105105
}
106106

107-
inspect(depth?: number, options?: unknown): string {
108-
const stylize = getStylizeFunction(options);
109-
const pattern = stylize(`'${this.pattern}'`, 'regexp');
110-
const flags = stylize(`'${this.options}'`, 'regexp');
107+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
108+
const stylize = getStylizeFunction(options) ?? (v => v);
109+
inspect ??= defaultInspect;
110+
const pattern = stylize(inspect(this.pattern), 'regexp');
111+
const flags = stylize(inspect(this.options), 'regexp');
111112
return `new BSONRegExp(${pattern}, ${flags})`;
112113
}
113114
}
114-
115-
/** @internal */
116-
type StylizeFunction = (x: string, style: string) => string;
117-
/** @internal */
118-
function getStylizeFunction(options?: unknown): StylizeFunction {
119-
const stylizeExists =
120-
options != null &&
121-
typeof options === 'object' &&
122-
'stylize' in options &&
123-
typeof options.stylize === 'function';
124-
125-
if (stylizeExists) {
126-
return options.stylize as StylizeFunction;
127-
} else {
128-
return defaultInspect;
129-
}
130-
}

src/symbol.ts

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

44
/** @public */
55
export interface BSONSymbolExtended {
@@ -48,12 +48,8 @@ export class BSONSymbol extends BSONValue {
4848
return new BSONSymbol(doc.$symbol);
4949
}
5050

51-
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
52-
const addQuotes = !inspect;
51+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
5352
inspect ??= defaultInspect;
54-
if (addQuotes) {
55-
return `new BSONSymbol('${inspect(this.value, options)}')`;
56-
}
5753
return `new BSONSymbol(${inspect(this.value, options)})`;
5854
}
5955
}

src/timestamp.ts

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

66
/** @public */
77
export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect';
@@ -142,10 +142,10 @@ export class Timestamp extends LongWithoutOverridesClass {
142142
return new Timestamp({ t, i });
143143
}
144144

145-
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
145+
inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {
146146
inspect ??= defaultInspect;
147-
const t = inspect(this.getHighBits(), options);
148-
const i = inspect(this.getLowBits(), options);
147+
const t = inspect(this.high >>> 0, options);
148+
const i = inspect(this.low >>> 0, options);
149149
return `new Timestamp({ t: ${t}, i: ${i} })`;
150150
}
151151
}

test/colors.mjs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/** Used to generate screenshots for introducing color to BSON inspect function */
2+
3+
'use strict';
4+
5+
import {
6+
Binary,
7+
UUID,
8+
Code,
9+
DBRef,
10+
Decimal128,
11+
Double,
12+
Int32,
13+
Long,
14+
ObjectId,
15+
BSONRegExp,
16+
BSONSymbol,
17+
Timestamp,
18+
MaxKey,
19+
MinKey
20+
} from '../lib/bson.mjs';
21+
22+
console.log({
23+
binary: new Binary(Buffer.from('abcdef', 'utf8'), 0x06),
24+
uuid: new UUID(),
25+
code: new Code(function iLoveJavaScript() {
26+
do {
27+
console.log('hello!');
28+
} while (Math.random() > 0.5);
29+
}),
30+
c: new Code(
31+
function iLoveJavaScript() {
32+
do {
33+
console.log('hello!');
34+
} while (Math.random() > 0.5);
35+
},
36+
{ context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}}
37+
),
38+
c2: new Code (
39+
function iLoveJavaScript() { return `js`; },
40+
{ context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}}
41+
),
42+
dbref: new DBRef('collection', new ObjectId('00'.repeat(12))),
43+
dbref_db: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db'),
44+
dbref_db_fields: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db', { a: 1 }),
45+
decimal128: new Decimal128('1.353e34'),
46+
double: new Double(2.354),
47+
double2: new Double(2),
48+
double3: new Double(-0),
49+
int32: new Int32('4577'),
50+
long: new Long(-12442),
51+
objectid: new ObjectId('00'.repeat(12)),
52+
bsonregexp: new BSONRegExp('abc', 'imx'),
53+
bsonsymbol: new BSONSymbol('my symbol'),
54+
timestamp: new Timestamp({ i: 2345, t: 23453 }),
55+
maxkey: new MaxKey(),
56+
minkey: new MinKey()
57+
});
58+
59+
const oid = new ObjectId('00'.repeat(12));
60+
console.log(oid);

0 commit comments

Comments
 (0)