Skip to content

Commit a14ae92

Browse files
PR Requested Changes 0
1 parent b307fdf commit a14ae92

File tree

16 files changed

+42
-132
lines changed

16 files changed

+42
-132
lines changed

src/binary.ts

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
type InspectParameterFn,
3-
getBasicInspectParameterFn,
3+
basicInspectParameterFn,
44
isAnyArrayBuffer,
55
isUint8Array
66
} from './parser/utils';
@@ -268,18 +268,9 @@ export class Binary extends BSONValue {
268268
return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type);
269269
}
270270

271-
/** @internal */
272-
[Symbol.for('nodejs.util.inspect.custom')](
273-
depth?: number,
274-
options?: unknown,
275-
inspect?: InspectParameterFn
276-
): string {
277-
return this.inspect(depth, options, inspect);
278-
}
279-
280271
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
281272
const addQuotes = inspect ? false : true;
282-
inspect ??= getBasicInspectParameterFn();
273+
inspect ??= basicInspectParameterFn;
283274
const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
284275
const base64Arg = inspect(base64, options);
285276
const subTypeArg = inspect(this.sub_type, options);
@@ -479,19 +470,11 @@ export class UUID extends Binary {
479470
* Converts to a string representation of this Id.
480471
*
481472
* @returns return the 36 character hex string representation.
482-
* @internal
473+
*
483474
*/
484-
[Symbol.for('nodejs.util.inspect.custom')](
485-
depth?: number,
486-
options?: unknown,
487-
inspect?: InspectParameterFn
488-
): string {
489-
return this.inspect(depth, options, inspect);
490-
}
491-
492475
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
493476
const addQuotes = inspect ? false : true;
494-
inspect ??= getBasicInspectParameterFn();
477+
inspect ??= basicInspectParameterFn;
495478
if (addQuotes) {
496479
return `new UUID('${inspect(this.toHexString(), options)}')`;
497480
}

src/bson_value.ts

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

34
/** @public */
45
export abstract class BSONValue {
@@ -10,8 +11,16 @@ export abstract class BSONValue {
1011
return BSON_MAJOR_VERSION;
1112
}
1213

14+
[Symbol.for('nodejs.util.inspect.custom')](
15+
depth?: number,
16+
options?: unknown,
17+
inspect?: InspectParameterFn
18+
): string {
19+
return this.inspect(depth, options, inspect);
20+
}
21+
1322
/** @public */
14-
public abstract inspect(): string;
23+
public abstract inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string;
1524

1625
/** @internal */
1726
abstract toExtendedJSON(): unknown;

src/code.ts

Lines changed: 2 additions & 11 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, getBasicInspectParameterFn } from './parser/utils';
3+
import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils';
44

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

59-
/** @internal */
60-
[Symbol.for('nodejs.util.inspect.custom')](
61-
depth?: number,
62-
options?: unknown,
63-
inspect?: InspectParameterFn
64-
): string {
65-
return this.inspect(depth, options, inspect);
66-
}
67-
6859
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
69-
inspect ??= getBasicInspectParameterFn();
60+
inspect ??= basicInspectParameterFn;
7061
let parametersString = inspect(this.code, options);
7162
const multiLineFn = parametersString.includes('\n');
7263
if (this.scope != null) {

src/db_ref.ts

Lines changed: 2 additions & 11 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, getBasicInspectParameterFn } from './parser/utils';
5+
import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils';
66

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

115-
/** @internal */
116-
[Symbol.for('nodejs.util.inspect.custom')](
117-
depth?: number,
118-
options?: unknown,
119-
inspect?: InspectParameterFn
120-
): string {
121-
return this.inspect(depth, options, inspect);
122-
}
123-
124115
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
125116
const addQuotes = inspect ? false : true;
126-
inspect ??= getBasicInspectParameterFn();
117+
inspect ??= basicInspectParameterFn;
127118

128119
const namespaceArg = addQuotes
129120
? `'${inspect(this.namespace, options)}'`

src/decimal128.ts

Lines changed: 2 additions & 11 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, getBasicInspectParameterFn, isUint8Array } from './parser/utils';
4+
import { type InspectParameterFn, basicInspectParameterFn, isUint8Array } from './parser/utils';
55
import { ByteUtils } from './utils/byte_utils';
66

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

850-
/** @internal */
851-
[Symbol.for('nodejs.util.inspect.custom')](
852-
depth?: number,
853-
options?: unknown,
854-
inspect?: InspectParameterFn
855-
): string {
856-
return this.inspect(depth, options, inspect);
857-
}
858-
859850
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
860851
const addQuotes = inspect ? false : true;
861-
inspect ??= getBasicInspectParameterFn();
852+
inspect ??= basicInspectParameterFn;
862853
const d128string = inspect(this.toString(), options);
863854
if (addQuotes) {
864855
return `new Decimal128('${d128string}')`;

src/double.ts

Lines changed: 2 additions & 11 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, getBasicInspectParameterFn } from './parser/utils';
3+
import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils';
44

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

75-
/** @internal */
76-
[Symbol.for('nodejs.util.inspect.custom')](
77-
depth?: number,
78-
options?: unknown,
79-
inspect?: InspectParameterFn
80-
): string {
81-
return this.inspect(depth, options, inspect);
82-
}
83-
8475
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
85-
inspect ??= getBasicInspectParameterFn();
76+
inspect ??= basicInspectParameterFn;
8677
return `new Double(${inspect(this.valueOf(), options)})`;
8778
}
8879
}

src/int_32.ts

Lines changed: 2 additions & 11 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, getBasicInspectParameterFn } from './parser/utils';
3+
import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils';
44

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

63-
/** @internal */
64-
[Symbol.for('nodejs.util.inspect.custom')](
65-
depth?: number,
66-
options?: unknown,
67-
inspect?: InspectParameterFn
68-
): string {
69-
return this.inspect(depth, options, inspect);
70-
}
71-
7263
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
73-
inspect ??= getBasicInspectParameterFn();
64+
inspect ??= basicInspectParameterFn;
7465
return `new Int32(${inspect(this.value, options)})`;
7566
}
7667
}

src/long.ts

Lines changed: 2 additions & 11 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, getBasicInspectParameterFn } from './parser/utils';
4+
import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils';
55
import type { Timestamp } from './timestamp';
66

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

1060-
/** @internal */
1061-
[Symbol.for('nodejs.util.inspect.custom')](
1062-
depth?: number,
1063-
options?: unknown,
1064-
inspect?: InspectParameterFn
1065-
): string {
1066-
return this.inspect(depth, options, inspect);
1067-
}
1068-
10691060
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
1070-
inspect ??= getBasicInspectParameterFn();
1061+
inspect ??= basicInspectParameterFn;
10711062
const longVal = inspect(this.toString(), options);
10721063
const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : '';
10731064
return `new Long(${longVal}${unsignedVal})`;

src/max_key.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ export class MaxKey extends BSONValue {
2525
return new MaxKey();
2626
}
2727

28-
/** @internal */
29-
[Symbol.for('nodejs.util.inspect.custom')](): string {
30-
return this.inspect();
31-
}
32-
3328
inspect(): string {
3429
return 'new MaxKey()';
3530
}

src/min_key.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ export class MinKey extends BSONValue {
2525
return new MinKey();
2626
}
2727

28-
/** @internal */
29-
[Symbol.for('nodejs.util.inspect.custom')](): string {
30-
return this.inspect();
31-
}
32-
3328
inspect(): string {
3429
return 'new MinKey()';
3530
}

src/objectid.ts

Lines changed: 2 additions & 11 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, getBasicInspectParameterFn } from './parser/utils';
3+
import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils';
44
import { BSONDataView, ByteUtils } from './utils/byte_utils';
55

66
// Regular expression that checks for hex value
@@ -295,19 +295,10 @@ export class ObjectId extends BSONValue {
295295
* Converts to a string representation of this Id.
296296
*
297297
* @returns return the 24 character hex string representation.
298-
* @internal
299298
*/
300-
[Symbol.for('nodejs.util.inspect.custom')](
301-
depth?: number,
302-
options?: unknown,
303-
inspect?: InspectParameterFn
304-
): string {
305-
return this.inspect(depth, options, inspect);
306-
}
307-
308299
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
309300
const addQuotes = inspect ? false : true;
310-
inspect ??= getBasicInspectParameterFn();
301+
inspect ??= basicInspectParameterFn;
311302
if (addQuotes) {
312303
return `new ObjectId('${inspect(this.toHexString(), options)}')`;
313304
}

src/parser/utils.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ export function isDate(d: unknown): d is Date {
2929
}
3030

3131
/** @internal */
32-
export type StylizeFunction = (x: unknown, style: string) => string;
32+
export type StylizeFunction = (x: string, style: string) => string;
3333
export type InspectParameterFn = (x: unknown, options: unknown) => string;
34-
export function getBasicInspectParameterFn(): InspectParameterFn {
35-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
36-
return v => `${v}`;
37-
}
34+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
35+
export const basicInspectParameterFn: InspectParameterFn = v => `${v}`;
36+
3837
export function getStylizeFunction(options?: unknown): StylizeFunction {
3938
const stylizeExists =
4039
options != null &&
@@ -43,8 +42,8 @@ export function getStylizeFunction(options?: unknown): StylizeFunction {
4342
typeof options.stylize === 'function';
4443

4544
if (stylizeExists) {
46-
return options.stylize as (x: unknown, style: string) => string;
45+
return options.stylize as StylizeFunction;
4746
} else {
48-
return getBasicInspectParameterFn();
47+
return basicInspectParameterFn;
4948
}
5049
}

src/regexp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ export class BSONRegExp extends BSONValue {
104104
throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`);
105105
}
106106

107-
/** @internal */
108-
[Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string {
107+
/** @internal */
108+
[Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string {
109109
return this.inspect(depth, options);
110110
}
111111

src/symbol.ts

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

44
/** @public */
55
export interface BSONSymbolExtended {
@@ -59,7 +59,7 @@ export class BSONSymbol extends BSONValue {
5959

6060
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
6161
const addQuotes = inspect ? false : true;
62-
inspect ??= getBasicInspectParameterFn();
62+
inspect ??= basicInspectParameterFn;
6363
if (addQuotes) {
6464
return `new BSONSymbol('${inspect(this.value, options)}')`;
6565
}

src/timestamp.ts

Lines changed: 2 additions & 11 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, getBasicInspectParameterFn } from './parser/utils';
4+
import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils';
55

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

145-
/** @internal */
146-
[Symbol.for('nodejs.util.inspect.custom')](
147-
depth?: number,
148-
options?: unknown,
149-
inspect?: InspectParameterFn
150-
): string {
151-
return this.inspect(depth, options, inspect);
152-
}
153-
154145
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
155-
inspect ??= getBasicInspectParameterFn();
146+
inspect ??= basicInspectParameterFn;
156147
const t = inspect(this.getHighBits(), options);
157148
const i = inspect(this.getLowBits(), options);
158149
return `new Timestamp({ t: ${t}, i: ${i} })`;

test/types/bson.test-d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
Decimal128Extended,
2020
BSONValue
2121
} from '../../bson'; // import from generated bson.d.ts
22+
import type { InspectParameterFn } from '../../src/parser/utils';
2223

2324
expectType<() => UUID>(Binary.prototype.toUUID);
2425
expectType<() => Binary>(UUID.prototype.toBinary);
@@ -75,4 +76,4 @@ expectType<'Binary'>(UUID.prototype._bsontype)
7576
// Common BSONValue interface
7677
declare const bsonValue: BSONValue;
7778
expectType<string>(bsonValue._bsontype);
78-
expectType<() => string>(bsonValue.inspect);
79+
expectType<(depth?: number | undefined, options?: unknown, inspect?: InspectParameterFn | undefined) => string>(bsonValue.inspect);

0 commit comments

Comments
 (0)