Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions src/binary.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { isAnyArrayBuffer, isUint8Array } from './parser/utils';
import {
type InspectParameterFn,
getBasicInspectParameterFn,
isAnyArrayBuffer,
isUint8Array
} from './parser/utils';
import type { EJSONOptions } from './extended_json';
import { BSONError } from './error';
import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants';
Expand Down Expand Up @@ -264,13 +269,20 @@ export class Binary extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(): string {
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
inspect ??= getBasicInspectParameterFn();
const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
return `Binary.createFromBase64("${base64}", ${this.sub_type})`;
const base64Arg = inspect(base64, options);
const subTypeArg = inspect(this.sub_type, options);
return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`;
}
}

Expand Down Expand Up @@ -465,11 +477,16 @@ export class UUID extends Binary {
* @returns return the 36 character hex string representation.
* @internal
*/
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(): string {
return `new UUID("${this.toHexString()}")`;
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
inspect ??= getBasicInspectParameterFn();
return `new UUID(${inspect(this.toHexString(), options)})`;
}
}
23 changes: 16 additions & 7 deletions src/code.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Document } from './bson';
import { BSONValue } from './bson_value';
import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils';

/** @public */
export interface CodeExtended {
Expand Down Expand Up @@ -56,14 +57,22 @@ export class Code extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(): string {
const codeJson = this.toJSON();
return `new Code(${JSON.stringify(String(codeJson.code))}${
codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : ''
})`;
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
inspect ??= getBasicInspectParameterFn();
let parametersString = inspect(this.code, options);
const multiLineFn = parametersString.includes('\n');
if (this.scope != null) {
parametersString += `,${multiLineFn ? '\n' : ' '}${inspect(this.scope, options)}`;
}
const endingNewline = multiLineFn && this.scope === null;
return `new Code(${multiLineFn ? '\n' : ''}${parametersString}${endingNewline ? '\n' : ''})`;
}
}
30 changes: 21 additions & 9 deletions src/db_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Document } from './bson';
import { BSONValue } from './bson_value';
import type { EJSONOptions } from './extended_json';
import type { ObjectId } from './objectid';
import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils';

/** @public */
export interface DBRefLike {
Expand Down Expand Up @@ -112,16 +113,27 @@ export class DBRef extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(): string {
// NOTE: if OID is an ObjectId class it will just print the oid string.
const oid =
this.oid === undefined || this.oid.toString === undefined ? this.oid : this.oid.toString();
return `new DBRef("${this.namespace}", new ObjectId("${String(oid)}")${
this.db ? `, "${this.db}"` : ''
})`;
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
inspect ??= getBasicInspectParameterFn();

const args = [inspect(this.namespace, options), inspect(this.oid, options)];

if (this.db) {
args.push(inspect(this.db, options));
}

if (Object.keys(this.fields).length > 0) {
args.push(inspect(this.fields, options));
}

return `new DBRef(${args.join(', ')})`;
}
}
16 changes: 11 additions & 5 deletions src/decimal128.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BSONValue } from './bson_value';
import { BSONError } from './error';
import { Long } from './long';
import { isUint8Array } from './parser/utils';
import { type InspectParameterFn, getBasicInspectParameterFn, isUint8Array } from './parser/utils';
import { ByteUtils } from './utils/byte_utils';

const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/;
Expand Down Expand Up @@ -848,11 +848,17 @@ export class Decimal128 extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(): string {
return `new Decimal128("${this.toString()}")`;
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
inspect ??= getBasicInspectParameterFn();
const d128string = inspect(this.toString(), options);
return `new Decimal128(${d128string})`;
}
}
15 changes: 10 additions & 5 deletions src/double.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BSONValue } from './bson_value';
import type { EJSONOptions } from './extended_json';
import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils';

/** @public */
export interface DoubleExtended {
Expand Down Expand Up @@ -72,12 +73,16 @@ export class Double extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(): string {
const eJSON = this.toExtendedJSON() as DoubleExtended;
return `new Double(${eJSON.$numberDouble})`;
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
inspect ??= getBasicInspectParameterFn();
return `new Double(${inspect(this.valueOf(), options)})`;
}
}
14 changes: 10 additions & 4 deletions src/int_32.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BSONValue } from './bson_value';
import type { EJSONOptions } from './extended_json';
import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils';

/** @public */
export interface Int32Extended {
Expand Down Expand Up @@ -60,11 +61,16 @@ export class Int32 extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(): string {
return `new Int32(${this.valueOf()})`;
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
inspect ??= getBasicInspectParameterFn();
return `new Int32(${inspect(this.value, options)})`;
}
}
20 changes: 14 additions & 6 deletions src/long.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BSONValue } from './bson_value';
import { BSONError } from './error';
import type { EJSONOptions } from './extended_json';
import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils';
import type { Timestamp } from './timestamp';

interface LongWASMHelpers {
Expand Down Expand Up @@ -1057,11 +1058,18 @@ export class Long extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
}

inspect(): string {
return `new Long("${this.toString()}"${this.unsigned ? ', true' : ''})`;
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
inspect ??= getBasicInspectParameterFn();
const longVal = inspect(this.toString(), options);
const unsignedVal = this.unsigned ? `', ' + ${inspect(this.unsigned, options)}` : '';
return `new Long(${longVal}${unsignedVal})`;
}
}
14 changes: 10 additions & 4 deletions src/objectid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BSONValue } from './bson_value';
import { BSONError } from './error';
import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils';
import { BSONDataView, ByteUtils } from './utils/byte_utils';

// Regular expression that checks for hex value
Expand Down Expand Up @@ -296,11 +297,16 @@ export class ObjectId extends BSONValue {
* @returns return the 24 character hex string representation.
* @internal
*/
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(): string {
return `new ObjectId("${this.toHexString()}")`;
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
inspect ??= getBasicInspectParameterFn();
return `new ObjectId(${inspect(this.toHexString(), options)})`;
}
}
21 changes: 21 additions & 0 deletions src/parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,24 @@ export function isMap(d: unknown): d is Map<unknown, unknown> {
export function isDate(d: unknown): d is Date {
return Object.prototype.toString.call(d) === '[object Date]';
}

/** @internal */
export type StylizeFunction = (x: unknown, style: string) => string;
export type InspectParameterFn = (x: unknown, options: unknown) => string;
export function getBasicInspectParameterFn(): InspectParameterFn {
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
return v => `${v}`;
}
export function getStylizeFunction(options?: unknown): StylizeFunction {
const stylizeExists =
options != null &&
typeof options === 'object' &&
'stylize' in options &&
typeof options.stylize === 'function';

if (stylizeExists) {
return options.stylize as (x: unknown, style: string) => string;
} else {
return getBasicInspectParameterFn();
}
}
12 changes: 8 additions & 4 deletions src/regexp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BSONValue } from './bson_value';
import { BSONError } from './error';
import type { EJSONOptions } from './extended_json';
import { getStylizeFunction } from './parser/utils';

function alphabetize(str: string): string {
return str.split('').sort().join('');
Expand Down Expand Up @@ -104,11 +105,14 @@ export class BSONRegExp extends BSONValue {
}

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

inspect(): string {
return `new BSONRegExp(${JSON.stringify(this.pattern)}, ${JSON.stringify(this.options)})`;
inspect(depth?: number, options?: unknown): string {
const stylize = getStylizeFunction(options);
const pattern = stylize(`'${this.pattern}'`, 'regexp');
const flags = stylize(`${this.options}'`, 'regexp');
return `new BSONRegExp(${pattern}, ${flags})`;
}
}
18 changes: 12 additions & 6 deletions src/symbol.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BSONValue } from './bson_value';
import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils';

/** @public */
export interface BSONSymbolExtended {
Expand Down Expand Up @@ -33,10 +34,6 @@ export class BSONSymbol extends BSONValue {
return this.value;
}

inspect(): string {
return `new BSONSymbol(${JSON.stringify(this.value)})`;
}

toJSON(): string {
return this.value;
}
Expand All @@ -52,7 +49,16 @@ export class BSONSymbol extends BSONValue {
}

/** @internal */
[Symbol.for('nodejs.util.inspect.custom')](): string {
return this.inspect();
[Symbol.for('nodejs.util.inspect.custom')](
depth?: number,
options?: unknown,
inspect?: InspectParameterFn
): string {
return this.inspect(depth, options, inspect);
}

inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
inspect ??= getBasicInspectParameterFn();
return `new BSONSymbol(${inspect(this.value, options)})`;
}
}
Loading