From 3febaef8af0dd412d374520699627dd9295562f6 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Tue, 3 Oct 2023 16:47:42 -0400 Subject: [PATCH 01/16] test --- src/binary.ts | 21 ++++++++++--------- src/code.ts | 26 +++++++++++++++++------- src/db_ref.ts | 31 +++++++++++++++++++--------- src/decimal128.ts | 12 ++++++----- src/double.ts | 10 +++++---- src/int_32.ts | 10 +++++---- src/long.ts | 14 +++++++++---- src/objectid.ts | 10 +++++---- src/parser/utils.ts | 20 ++++++++++++++++++ src/regexp.ts | 12 +++++++---- src/symbol.ts | 14 +++++++------ src/timestamp.ts | 12 +++++++---- test/colors.mjs | 49 +++++++++++++++++++++++++++++++++++++++++++++ 13 files changed, 181 insertions(+), 60 deletions(-) create mode 100644 test/colors.mjs diff --git a/src/binary.ts b/src/binary.ts index 53c62a83..52fbad2a 100644 --- a/src/binary.ts +++ b/src/binary.ts @@ -1,4 +1,4 @@ -import { isAnyArrayBuffer, isUint8Array } from './parser/utils'; +import { getStylizeFunction, isAnyArrayBuffer, isUint8Array } from './parser/utils'; import type { EJSONOptions } from './extended_json'; import { BSONError } from './error'; import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants'; @@ -264,13 +264,15 @@ 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): string { + return this.inspect(depth, options); } - inspect(): string { + inspect(depth?: number, options?: unknown): string { + const stylize = getStylizeFunction(options); const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position)); - return `Binary.createFromBase64("${base64}", ${this.sub_type})`; + const base64Arg = stylize(`"${base64}"`, 'string'); + return `Binary.createFromBase64(${base64Arg}, ${this.sub_type})`; } } @@ -465,11 +467,12 @@ 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): string { + return this.inspect(depth, options); } - inspect(): string { - return `new UUID("${this.toHexString()}")`; + inspect(depth?: number, options?: unknown): string { + const stylize = getStylizeFunction(options); + return `new UUID(${stylize(`"${this.toHexString()}"`, 'string')})`; } } diff --git a/src/code.ts b/src/code.ts index 1fb46ab8..dee84e82 100644 --- a/src/code.ts +++ b/src/code.ts @@ -1,3 +1,4 @@ +import { getStylizeFunction } from './parser/utils'; import type { Document } from './bson'; import { BSONValue } from './bson_value'; @@ -56,14 +57,25 @@ 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?: (value: unknown, options: unknown) => string + ): 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?: (value: unknown, options: unknown) => string + ): string { + inspect ??= v => JSON.stringify(v); + let parametersString = inspect(this.code, options); + const multiLineFn = parametersString.includes('\n'); + if (this.scope != null) { + parametersString += `,${multiLineFn ? '\n' : ' '}${inspect(this.scope, options)}`; + } + return `new Code(${multiLineFn ? '\n' : ''}${parametersString}${multiLineFn ? '\n' : ''})`; } } diff --git a/src/db_ref.ts b/src/db_ref.ts index ebdcb61b..4ee34b1f 100644 --- a/src/db_ref.ts +++ b/src/db_ref.ts @@ -1,3 +1,4 @@ +import { getStylizeFunction, InspectParameterFn } from './parser/utils'; import type { Document } from './bson'; import { BSONValue } from './bson_value'; import type { EJSONOptions } from './extended_json'; @@ -112,16 +113,28 @@ 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 { + const stylize = getStylizeFunction(options); + inspect ??= v => JSON.stringify(v); + + const args = [stylize(`"${this.namespace}"`, 'string'), this.oid.inspect(depth, options)]; + + if (this.db) { + args.push(stylize(`"${this.db}"`, 'string')); + } + + if (Object.keys(this.fields).length > 0) { + args.push(inspect(this.fields, options)); + } + + return `new DBRef(${args.join(', ')})`; } } diff --git a/src/decimal128.ts b/src/decimal128.ts index b00c9d70..6f2ae599 100644 --- a/src/decimal128.ts +++ b/src/decimal128.ts @@ -1,7 +1,7 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; import { Long } from './long'; -import { isUint8Array } from './parser/utils'; +import { getStylizeFunction, isUint8Array } from './parser/utils'; import { ByteUtils } from './utils/byte_utils'; const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/; @@ -848,11 +848,13 @@ 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): string { + return this.inspect(depth, options); } - inspect(): string { - return `new Decimal128("${this.toString()}")`; + inspect(depth?: number, options?: unknown): string { + const stylize = getStylizeFunction(options); + const d128string = stylize(`"${this.toString()}"`, 'string'); + return `new Decimal128(${d128string})`; } } diff --git a/src/double.ts b/src/double.ts index 45815360..14a5caae 100644 --- a/src/double.ts +++ b/src/double.ts @@ -1,5 +1,6 @@ import { BSONValue } from './bson_value'; import type { EJSONOptions } from './extended_json'; +import { getStylizeFunction } from './parser/utils'; /** @public */ export interface DoubleExtended { @@ -72,12 +73,13 @@ 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): string { + return this.inspect(depth, options); } - inspect(): string { + inspect(depth?: number, options?: unknown): string { const eJSON = this.toExtendedJSON() as DoubleExtended; - return `new Double(${eJSON.$numberDouble})`; + const stylize = getStylizeFunction(options); + return `new Double(${stylize(eJSON.$numberDouble, 'number')})`; } } diff --git a/src/int_32.ts b/src/int_32.ts index c74f3c82..a0c9021d 100644 --- a/src/int_32.ts +++ b/src/int_32.ts @@ -1,5 +1,6 @@ import { BSONValue } from './bson_value'; import type { EJSONOptions } from './extended_json'; +import { getStylizeFunction } from './parser/utils'; /** @public */ export interface Int32Extended { @@ -60,11 +61,12 @@ 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): string { + return this.inspect(depth, options); } - inspect(): string { - return `new Int32(${this.valueOf()})`; + inspect(depth?: number, options?: unknown): string { + const stylize = getStylizeFunction(options); + return `new Int32(${stylize(this.value, 'number')})`; } } diff --git a/src/long.ts b/src/long.ts index 752c18fe..1715f90d 100644 --- a/src/long.ts +++ b/src/long.ts @@ -1,6 +1,7 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; import type { EJSONOptions } from './extended_json'; +import { getStylizeFunction } from './parser/utils'; import type { Timestamp } from './timestamp'; interface LongWASMHelpers { @@ -1057,11 +1058,16 @@ export class Long 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 Long("${this.toString()}"${this.unsigned ? ', true' : ''})`; + inspect(depth?: number, options?: unknown): string { + const stylize = getStylizeFunction(options); + return `new Int32(${stylize(54, 'number')})`; + /*return `new Long(${stylize(this.toString() + 'n', 'number')}, ${stylize( + this.unsigned, + 'boolean' + )})`; */ } } diff --git a/src/objectid.ts b/src/objectid.ts index 8e172525..9996c2cd 100644 --- a/src/objectid.ts +++ b/src/objectid.ts @@ -1,5 +1,6 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; +import { getStylizeFunction } from './parser/utils'; import { BSONDataView, ByteUtils } from './utils/byte_utils'; // Regular expression that checks for hex value @@ -296,11 +297,12 @@ 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): string { + return this.inspect(depth, options); } - inspect(): string { - return `new ObjectId("${this.toHexString()}")`; + inspect(depth?: number, options?: unknown): string { + const stylize = getStylizeFunction(options); + return `new ObjectId(${stylize(`"${this.toHexString()}"`, 'string')})`; } } diff --git a/src/parser/utils.ts b/src/parser/utils.ts index 1ed11701..beb28e39 100644 --- a/src/parser/utils.ts +++ b/src/parser/utils.ts @@ -27,3 +27,23 @@ export function isMap(d: unknown): d is Map { 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; +/** @internal */ +export type InspectParameterFn = (x: unknown, options: unknown) => string; +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 { + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + return v => `${v}`; + } +} \ No newline at end of file diff --git a/src/regexp.ts b/src/regexp.ts index 0e7b279d..d5d7a448 100644 --- a/src/regexp.ts +++ b/src/regexp.ts @@ -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(''); @@ -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(JSON.stringify(this.pattern), 'regexp'); + const flags = stylize(JSON.stringify(this.options), 'regexp'); + return `new BSONRegExp(${pattern}, ${flags})`; } } diff --git a/src/symbol.ts b/src/symbol.ts index 079cabea..7221e203 100644 --- a/src/symbol.ts +++ b/src/symbol.ts @@ -1,4 +1,5 @@ import { BSONValue } from './bson_value'; +import { getStylizeFunction } from './parser/utils'; /** @public */ export interface BSONSymbolExtended { @@ -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; } @@ -52,7 +49,12 @@ 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): string { + return this.inspect(depth, options); + } + + inspect(depth?: number, options?: unknown): string { + const stylize = getStylizeFunction(options); + return `new BSONSymbol(${stylize(`"${this.value}"`, 'string')})`; } } diff --git a/src/timestamp.ts b/src/timestamp.ts index 52d85a25..86b8ed4f 100644 --- a/src/timestamp.ts +++ b/src/timestamp.ts @@ -1,6 +1,7 @@ import { BSONError } from './error'; import type { Int32 } from './int_32'; import { Long } from './long'; +import { getStylizeFunction } from './parser/utils'; /** @public */ export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect'; @@ -142,11 +143,14 @@ export class Timestamp extends LongWithoutOverridesClass { } /** @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 Timestamp({ t: ${this.getHighBits()}, i: ${this.getLowBits()} })`; + inspect(depth?: number, options?: unknown): string { + const stylize = getStylizeFunction(options); + const t = stylize(`${this.getHighBits()}`, 'number'); + const i = stylize(`${this.getLowBits()}`, 'number'); + return `new Timestamp({ t: ${t}, i: ${i} })`; } } diff --git a/test/colors.mjs b/test/colors.mjs new file mode 100644 index 00000000..8bd0ec03 --- /dev/null +++ b/test/colors.mjs @@ -0,0 +1,49 @@ +'use strict'; + +import { + Binary, + UUID, + Code, + DBRef, + Decimal128, + Double, + Int32, + Long, + ObjectId, + BSONRegExp, + BSONSymbol, + Timestamp, + MaxKey, + MinKey +} from '../lib/bson.mjs'; + +console.log({ + binary: new Binary(Buffer.from('abcdef', 'utf8'), 0x06), + uuid: new UUID(), + code: new Code(function iLoveJavaScript() { + do { + console.log('hello!'); + } while (Math.random() > 0.5); + }), + code_w_scope: new Code( + function iLoveJavaScript() { + do { + console.log('hello!'); + } while (Math.random() > 0.5); + }, + { context: 'random looping!', reference: Long.fromString('2345') } + ), + dbref: new DBRef('collection', new ObjectId('00'.repeat(12))), + dbref_db: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db'), + dbref_db_fields: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db', { a: 1 }), + decimal128: new Decimal128('1.353e34'), + double: new Double(2.354), + int32: new Int32('4577'), + long: new Long(-12442), + objectid: new ObjectId('00'.repeat(12)), + bsonregexp: new BSONRegExp('abc', 'imx'), + bsonsymbol: new BSONSymbol('my symbol'), + timestamp: new Timestamp({ i: 2345, t: 23453 }), + maxkey: new MaxKey(), + minkey: new MinKey() +}); \ No newline at end of file From 34264a612cc3203fa7705edccae4ad927ccfa887 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Wed, 4 Oct 2023 15:25:59 -0400 Subject: [PATCH 02/16] all basic tests passing --- src/long.ts | 5 ++--- test/node/bson_type_classes.test.ts | 11 +++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/long.ts b/src/long.ts index 1715f90d..2e44c1bf 100644 --- a/src/long.ts +++ b/src/long.ts @@ -1064,10 +1064,9 @@ export class Long extends BSONValue { inspect(depth?: number, options?: unknown): string { const stylize = getStylizeFunction(options); - return `new Int32(${stylize(54, 'number')})`; - /*return `new Long(${stylize(this.toString() + 'n', 'number')}, ${stylize( + return `new Long("${stylize(this.toString(), 'number')}" ${stylize( this.unsigned, 'boolean' - )})`; */ + )})`; } } diff --git a/test/node/bson_type_classes.test.ts b/test/node/bson_type_classes.test.ts index 7b9b2105..726cfb04 100644 --- a/test/node/bson_type_classes.test.ts +++ b/test/node/bson_type_classes.test.ts @@ -1,4 +1,5 @@ import { expect } from 'chai'; +import { inspect } from 'node:util'; import { __isWeb__ } from '../register-bson'; import { Binary, @@ -60,10 +61,20 @@ describe('BSON Type classes common interfaces', () => { return this.currentTest?.skip(); } }); + for (const [name, creator] of BSONTypeClassCtors) { it(`${name} inherits from BSONValue`, () => { expect(creator()).to.be.instanceOf(BSONValue); }); + context.only(`${name}[Symbol.for('nodejs.util.inspect.custom')] ${(name === 'MinKey' || name === 'MaxKey') ? 'does not support' : 'supports'} color`, () => { + it(`returns string with ${(name === 'MinKey' || name === 'MaxKey') ? 'no ' : ''}ANSI colors`, () => { + if (name !== 'MinKey' && name !== 'MaxKey') { + const value = creator(); + const string = inspect(value, { colors: true }); + expect(string).include('\x1b'); + } + }); + }); } }); From 90f2f735c8146af4f9e6ca2390e754faae8501b3 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Fri, 6 Oct 2023 13:17:45 -0400 Subject: [PATCH 03/16] Lint fixes --- src/binary.ts | 38 ++++++++++++++++++++--------- src/code.ts | 15 +++++------- src/db_ref.ts | 9 +++---- src/decimal128.ts | 16 +++++++----- src/double.ts | 17 +++++++------ src/int_32.ts | 16 +++++++----- src/long.ts | 25 ++++++++++--------- src/objectid.ts | 16 +++++++----- src/parser/utils.ts | 24 +++++++++--------- src/regexp.ts | 4 +-- src/symbol.ts | 16 +++++++----- src/timestamp.ts | 18 ++++++++------ test/colors.mjs | 10 ++++++-- test/node/bson_type_classes.test.ts | 25 ++++++++++++------- 14 files changed, 150 insertions(+), 99 deletions(-) diff --git a/src/binary.ts b/src/binary.ts index 52fbad2a..98ddb5ae 100644 --- a/src/binary.ts +++ b/src/binary.ts @@ -1,4 +1,9 @@ -import { getStylizeFunction, 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'; @@ -264,15 +269,20 @@ export class Binary extends BSONValue { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string { - return this.inspect(depth, options); + [Symbol.for('nodejs.util.inspect.custom')]( + depth?: number, + options?: unknown, + inspect?: InspectParameterFn + ): string { + return this.inspect(depth, options, inspect); } - inspect(depth?: number, options?: unknown): string { - const stylize = getStylizeFunction(options); + inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + inspect ??= getBasicInspectParameterFn(); const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position)); - const base64Arg = stylize(`"${base64}"`, 'string'); - return `Binary.createFromBase64(${base64Arg}, ${this.sub_type})`; + const base64Arg = inspect(base64, options); + const subTypeArg = inspect(this.sub_type, options); + return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`; } } @@ -467,12 +477,16 @@ export class UUID extends Binary { * @returns return the 36 character hex string representation. * @internal */ - [Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string { - return this.inspect(depth, options); + [Symbol.for('nodejs.util.inspect.custom')]( + depth?: number, + options?: unknown, + inspect?: InspectParameterFn + ): string { + return this.inspect(depth, options, inspect); } - inspect(depth?: number, options?: unknown): string { - const stylize = getStylizeFunction(options); - return `new UUID(${stylize(`"${this.toHexString()}"`, 'string')})`; + inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + inspect ??= getBasicInspectParameterFn(); + return `new UUID(${inspect(this.toHexString(), options)})`; } } diff --git a/src/code.ts b/src/code.ts index dee84e82..950066b9 100644 --- a/src/code.ts +++ b/src/code.ts @@ -1,6 +1,6 @@ -import { getStylizeFunction } from './parser/utils'; import type { Document } from './bson'; import { BSONValue } from './bson_value'; +import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils'; /** @public */ export interface CodeExtended { @@ -60,22 +60,19 @@ export class Code extends BSONValue { [Symbol.for('nodejs.util.inspect.custom')]( depth?: number, options?: unknown, - inspect?: (value: unknown, options: unknown) => string + inspect?: InspectParameterFn ): string { return this.inspect(depth, options, inspect); } - inspect( - depth?: number, - options?: unknown, - inspect?: (value: unknown, options: unknown) => string - ): string { - inspect ??= v => JSON.stringify(v); + 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)}`; } - return `new Code(${multiLineFn ? '\n' : ''}${parametersString}${multiLineFn ? '\n' : ''})`; + const endingNewline = multiLineFn && this.scope === null; + return `new Code(${multiLineFn ? '\n' : ''}${parametersString}${endingNewline ? '\n' : ''})`; } } diff --git a/src/db_ref.ts b/src/db_ref.ts index 4ee34b1f..9ba8d515 100644 --- a/src/db_ref.ts +++ b/src/db_ref.ts @@ -1,8 +1,8 @@ -import { getStylizeFunction, InspectParameterFn } from './parser/utils'; 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 { @@ -122,13 +122,12 @@ export class DBRef extends BSONValue { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - const stylize = getStylizeFunction(options); - inspect ??= v => JSON.stringify(v); + inspect ??= getBasicInspectParameterFn(); - const args = [stylize(`"${this.namespace}"`, 'string'), this.oid.inspect(depth, options)]; + const args = [inspect(this.namespace, options), inspect(this.oid, options)]; if (this.db) { - args.push(stylize(`"${this.db}"`, 'string')); + args.push(inspect(this.db, options)); } if (Object.keys(this.fields).length > 0) { diff --git a/src/decimal128.ts b/src/decimal128.ts index 6f2ae599..5c5e8ca3 100644 --- a/src/decimal128.ts +++ b/src/decimal128.ts @@ -1,7 +1,7 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; import { Long } from './long'; -import { getStylizeFunction, 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+)?$/; @@ -848,13 +848,17 @@ export class Decimal128 extends BSONValue { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string { - return this.inspect(depth, options); + [Symbol.for('nodejs.util.inspect.custom')]( + depth?: number, + options?: unknown, + inspect?: InspectParameterFn + ): string { + return this.inspect(depth, options, inspect); } - inspect(depth?: number, options?: unknown): string { - const stylize = getStylizeFunction(options); - const d128string = stylize(`"${this.toString()}"`, 'string'); + inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + inspect ??= getBasicInspectParameterFn(); + const d128string = inspect(this.toString(), options); return `new Decimal128(${d128string})`; } } diff --git a/src/double.ts b/src/double.ts index 14a5caae..003231ae 100644 --- a/src/double.ts +++ b/src/double.ts @@ -1,6 +1,6 @@ import { BSONValue } from './bson_value'; import type { EJSONOptions } from './extended_json'; -import { getStylizeFunction } from './parser/utils'; +import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils'; /** @public */ export interface DoubleExtended { @@ -73,13 +73,16 @@ export class Double extends BSONValue { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string { - return this.inspect(depth, options); + [Symbol.for('nodejs.util.inspect.custom')]( + depth?: number, + options?: unknown, + inspect?: InspectParameterFn + ): string { + return this.inspect(depth, options, inspect); } - inspect(depth?: number, options?: unknown): string { - const eJSON = this.toExtendedJSON() as DoubleExtended; - const stylize = getStylizeFunction(options); - return `new Double(${stylize(eJSON.$numberDouble, 'number')})`; + inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + inspect ??= getBasicInspectParameterFn(); + return `new Double(${inspect(this.valueOf(), options)})`; } } diff --git a/src/int_32.ts b/src/int_32.ts index a0c9021d..83ee3d7c 100644 --- a/src/int_32.ts +++ b/src/int_32.ts @@ -1,6 +1,6 @@ import { BSONValue } from './bson_value'; import type { EJSONOptions } from './extended_json'; -import { getStylizeFunction } from './parser/utils'; +import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils'; /** @public */ export interface Int32Extended { @@ -61,12 +61,16 @@ export class Int32 extends BSONValue { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string { - return this.inspect(depth, options); + [Symbol.for('nodejs.util.inspect.custom')]( + depth?: number, + options?: unknown, + inspect?: InspectParameterFn + ): string { + return this.inspect(depth, options, inspect); } - inspect(depth?: number, options?: unknown): string { - const stylize = getStylizeFunction(options); - return `new Int32(${stylize(this.value, 'number')})`; + inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + inspect ??= getBasicInspectParameterFn(); + return `new Int32(${inspect(this.value, options)})`; } } diff --git a/src/long.ts b/src/long.ts index 2e44c1bf..b0048736 100644 --- a/src/long.ts +++ b/src/long.ts @@ -1,7 +1,7 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; import type { EJSONOptions } from './extended_json'; -import { getStylizeFunction } from './parser/utils'; +import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils'; import type { Timestamp } from './timestamp'; interface LongWASMHelpers { @@ -1058,15 +1058,18 @@ export class Long extends BSONValue { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string { - return this.inspect(depth, options); - } - - inspect(depth?: number, options?: unknown): string { - const stylize = getStylizeFunction(options); - return `new Long("${stylize(this.toString(), 'number')}" ${stylize( - this.unsigned, - 'boolean' - )})`; + [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 `${longVal} + ${unsignedVal}`; } } diff --git a/src/objectid.ts b/src/objectid.ts index 9996c2cd..67d0d5b4 100644 --- a/src/objectid.ts +++ b/src/objectid.ts @@ -1,6 +1,6 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; -import { getStylizeFunction } from './parser/utils'; +import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils'; import { BSONDataView, ByteUtils } from './utils/byte_utils'; // Regular expression that checks for hex value @@ -297,12 +297,16 @@ export class ObjectId extends BSONValue { * @returns return the 24 character hex string representation. * @internal */ - [Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string { - return this.inspect(depth, options); + [Symbol.for('nodejs.util.inspect.custom')]( + depth?: number, + options?: unknown, + inspect?: InspectParameterFn + ): string { + return this.inspect(depth, options, inspect); } - inspect(depth?: number, options?: unknown): string { - const stylize = getStylizeFunction(options); - return `new ObjectId(${stylize(`"${this.toHexString()}"`, 'string')})`; + inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + inspect ??= getBasicInspectParameterFn(); + return `new ObjectId(${inspect(this.toHexString(), options)})`; } } diff --git a/src/parser/utils.ts b/src/parser/utils.ts index beb28e39..f530f53e 100644 --- a/src/parser/utils.ts +++ b/src/parser/utils.ts @@ -32,18 +32,20 @@ export function isDate(d: unknown): d is Date { export type StylizeFunction = (x: unknown, style: string) => string; /** @internal */ 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 && + 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; + return options.stylize as (x: unknown, style: string) => string; } else { - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - return v => `${v}`; - } -} \ No newline at end of file + return getBasicInspectParameterFn(); + } +} diff --git a/src/regexp.ts b/src/regexp.ts index d5d7a448..4b93f0d9 100644 --- a/src/regexp.ts +++ b/src/regexp.ts @@ -111,8 +111,8 @@ export class BSONRegExp extends BSONValue { inspect(depth?: number, options?: unknown): string { const stylize = getStylizeFunction(options); - const pattern = stylize(JSON.stringify(this.pattern), 'regexp'); - const flags = stylize(JSON.stringify(this.options), 'regexp'); + const pattern = stylize(`'${this.pattern}'`, 'regexp'); + const flags = stylize(`${this.options}'`, 'regexp'); return `new BSONRegExp(${pattern}, ${flags})`; } } diff --git a/src/symbol.ts b/src/symbol.ts index 7221e203..b5d6f274 100644 --- a/src/symbol.ts +++ b/src/symbol.ts @@ -1,5 +1,5 @@ import { BSONValue } from './bson_value'; -import { getStylizeFunction } from './parser/utils'; +import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils'; /** @public */ export interface BSONSymbolExtended { @@ -49,12 +49,16 @@ export class BSONSymbol extends BSONValue { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string { - return this.inspect(depth, options); + [Symbol.for('nodejs.util.inspect.custom')]( + depth?: number, + options?: unknown, + inspect?: InspectParameterFn + ): string { + return this.inspect(depth, options, inspect); } - inspect(depth?: number, options?: unknown): string { - const stylize = getStylizeFunction(options); - return `new BSONSymbol(${stylize(`"${this.value}"`, 'string')})`; + inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + inspect ??= getBasicInspectParameterFn(); + return `new BSONSymbol(${inspect(this.value, options)})`; } } diff --git a/src/timestamp.ts b/src/timestamp.ts index 86b8ed4f..6221ae25 100644 --- a/src/timestamp.ts +++ b/src/timestamp.ts @@ -1,7 +1,7 @@ import { BSONError } from './error'; import type { Int32 } from './int_32'; import { Long } from './long'; -import { getStylizeFunction } from './parser/utils'; +import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils'; /** @public */ export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect'; @@ -143,14 +143,18 @@ export class Timestamp extends LongWithoutOverridesClass { } /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string { - return this.inspect(depth, options); + [Symbol.for('nodejs.util.inspect.custom')]( + depth?: number, + options?: unknown, + inspect?: InspectParameterFn + ): string { + return this.inspect(depth, options, inspect); } - inspect(depth?: number, options?: unknown): string { - const stylize = getStylizeFunction(options); - const t = stylize(`${this.getHighBits()}`, 'number'); - const i = stylize(`${this.getLowBits()}`, 'number'); + inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + inspect ??= getBasicInspectParameterFn(); + const t = inspect(this.getHighBits(), options); + const i = inspect(this.getLowBits(), options); return `new Timestamp({ t: ${t}, i: ${i} })`; } } diff --git a/test/colors.mjs b/test/colors.mjs index 8bd0ec03..1804b221 100644 --- a/test/colors.mjs +++ b/test/colors.mjs @@ -25,19 +25,25 @@ console.log({ console.log('hello!'); } while (Math.random() > 0.5); }), - code_w_scope: new Code( + c: new Code( function iLoveJavaScript() { do { console.log('hello!'); } while (Math.random() > 0.5); }, - { context: 'random looping!', reference: Long.fromString('2345') } + { context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}} + ), + c2: new Code ( + function iLoveJavaScript() { return `js`; }, + { context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}} ), dbref: new DBRef('collection', new ObjectId('00'.repeat(12))), dbref_db: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db'), dbref_db_fields: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db', { a: 1 }), decimal128: new Decimal128('1.353e34'), double: new Double(2.354), + double2: new Double(2), + double3: new Double(-0), int32: new Int32('4577'), long: new Long(-12442), objectid: new ObjectId('00'.repeat(12)), diff --git a/test/node/bson_type_classes.test.ts b/test/node/bson_type_classes.test.ts index 726cfb04..7b4aede5 100644 --- a/test/node/bson_type_classes.test.ts +++ b/test/node/bson_type_classes.test.ts @@ -66,15 +66,22 @@ describe('BSON Type classes common interfaces', () => { it(`${name} inherits from BSONValue`, () => { expect(creator()).to.be.instanceOf(BSONValue); }); - context.only(`${name}[Symbol.for('nodejs.util.inspect.custom')] ${(name === 'MinKey' || name === 'MaxKey') ? 'does not support' : 'supports'} color`, () => { - it(`returns string with ${(name === 'MinKey' || name === 'MaxKey') ? 'no ' : ''}ANSI colors`, () => { - if (name !== 'MinKey' && name !== 'MaxKey') { - const value = creator(); - const string = inspect(value, { colors: true }); - expect(string).include('\x1b'); - } - }); - }); + context( + `${name}[Symbol.for('nodejs.util.inspect.custom')] ${ + name === 'MinKey' || name === 'MaxKey' ? 'does not support' : 'supports' + } color`, + () => { + it(`returns string with ${ + name === 'MinKey' || name === 'MaxKey' ? 'no ' : '' + }ANSI colors`, () => { + if (name !== 'MinKey' && name !== 'MaxKey') { + const value = creator(); + const string = inspect(value, { colors: true }); + expect(string).include('\x1b'); + } + }); + } + ); } }); From cdde690a322c5c9dcc43d9832a36598d54135caa Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Fri, 6 Oct 2023 13:51:16 -0400 Subject: [PATCH 04/16] Long fix --- src/long.ts | 2 +- test/colors.mjs | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/long.ts b/src/long.ts index b0048736..69f6d396 100644 --- a/src/long.ts +++ b/src/long.ts @@ -1070,6 +1070,6 @@ export class Long extends BSONValue { inspect ??= getBasicInspectParameterFn(); const longVal = inspect(this.toString(), options); const unsignedVal = this.unsigned ? `', ' + ${inspect(this.unsigned, options)}` : ''; - return `${longVal} + ${unsignedVal}`; + return `new Long(${longVal}${unsignedVal})`; } } diff --git a/test/colors.mjs b/test/colors.mjs index 1804b221..243e00c2 100644 --- a/test/colors.mjs +++ b/test/colors.mjs @@ -52,4 +52,7 @@ console.log({ timestamp: new Timestamp({ i: 2345, t: 23453 }), maxkey: new MaxKey(), minkey: new MinKey() -}); \ No newline at end of file +}); + +const oid = new ObjectId('00'.repeat(12)); +console.log(oid); \ No newline at end of file From d04ff08c6c64606332bcb94094abb73e03816109 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Fri, 6 Oct 2023 15:54:03 -0400 Subject: [PATCH 05/16] Final lint fixes --- src/parser/utils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/parser/utils.ts b/src/parser/utils.ts index f530f53e..312a5853 100644 --- a/src/parser/utils.ts +++ b/src/parser/utils.ts @@ -30,7 +30,6 @@ export function isDate(d: unknown): d is Date { /** @internal */ export type StylizeFunction = (x: unknown, style: string) => string; -/** @internal */ export type InspectParameterFn = (x: unknown, options: unknown) => string; export function getBasicInspectParameterFn(): InspectParameterFn { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions From 5a13e69178676b8af71e08314da0e6574fd1e5b2 Mon Sep 17 00:00:00 2001 From: aditi-khare-mongoDB <106987683+aditi-khare-mongoDB@users.noreply.github.com> Date: Fri, 6 Oct 2023 16:11:22 -0400 Subject: [PATCH 06/16] Delete test/colors.mjs --- test/colors.mjs | 58 ------------------------------------------------- 1 file changed, 58 deletions(-) delete mode 100644 test/colors.mjs diff --git a/test/colors.mjs b/test/colors.mjs deleted file mode 100644 index 243e00c2..00000000 --- a/test/colors.mjs +++ /dev/null @@ -1,58 +0,0 @@ -'use strict'; - -import { - Binary, - UUID, - Code, - DBRef, - Decimal128, - Double, - Int32, - Long, - ObjectId, - BSONRegExp, - BSONSymbol, - Timestamp, - MaxKey, - MinKey -} from '../lib/bson.mjs'; - -console.log({ - binary: new Binary(Buffer.from('abcdef', 'utf8'), 0x06), - uuid: new UUID(), - code: new Code(function iLoveJavaScript() { - do { - console.log('hello!'); - } while (Math.random() > 0.5); - }), - c: new Code( - function iLoveJavaScript() { - do { - console.log('hello!'); - } while (Math.random() > 0.5); - }, - { context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}} - ), - c2: new Code ( - function iLoveJavaScript() { return `js`; }, - { context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}} - ), - dbref: new DBRef('collection', new ObjectId('00'.repeat(12))), - dbref_db: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db'), - dbref_db_fields: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db', { a: 1 }), - decimal128: new Decimal128('1.353e34'), - double: new Double(2.354), - double2: new Double(2), - double3: new Double(-0), - int32: new Int32('4577'), - long: new Long(-12442), - objectid: new ObjectId('00'.repeat(12)), - bsonregexp: new BSONRegExp('abc', 'imx'), - bsonsymbol: new BSONSymbol('my symbol'), - timestamp: new Timestamp({ i: 2345, t: 23453 }), - maxkey: new MaxKey(), - minkey: new MinKey() -}); - -const oid = new ObjectId('00'.repeat(12)); -console.log(oid); \ No newline at end of file From 43ceb12f0a9e630e37fb63eb10dc0077add73fad Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Mon, 9 Oct 2023 12:05:54 -0400 Subject: [PATCH 07/16] fixed failing evergreen tests --- src/binary.ts | 8 ++++++++ src/db_ref.ts | 11 +++++++++-- src/decimal128.ts | 4 ++++ src/long.ts | 2 +- src/objectid.ts | 4 ++++ src/regexp.ts | 2 +- src/symbol.ts | 4 ++++ test/node/binary.test.ts | 20 ++++++++++---------- test/node/bson_test.js | 14 +++++++------- test/node/code.test.ts | 4 ++-- test/node/object_id.test.ts | 2 +- test/node/symbol.test.ts | 4 ++-- test/node/uuid.test.ts | 2 +- 13 files changed, 54 insertions(+), 27 deletions(-) diff --git a/src/binary.ts b/src/binary.ts index 98ddb5ae..50bcc653 100644 --- a/src/binary.ts +++ b/src/binary.ts @@ -278,10 +278,14 @@ export class Binary extends BSONValue { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + const addQuotes = inspect ? false : true; inspect ??= getBasicInspectParameterFn(); const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position)); const base64Arg = inspect(base64, options); const subTypeArg = inspect(this.sub_type, options); + if (addQuotes) { + return `Binary.createFromBase64('${base64Arg}', ${subTypeArg})`; + } return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`; } } @@ -486,7 +490,11 @@ export class UUID extends Binary { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + const addQuotes = inspect ? false : true; inspect ??= getBasicInspectParameterFn(); + if (addQuotes) { + return `new UUID('${inspect(this.toHexString(), options)}')`; + } return `new UUID(${inspect(this.toHexString(), options)})`; } } diff --git a/src/db_ref.ts b/src/db_ref.ts index 9ba8d515..b225489e 100644 --- a/src/db_ref.ts +++ b/src/db_ref.ts @@ -122,12 +122,19 @@ export class DBRef extends BSONValue { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + const addQuotes = inspect ? false : true; inspect ??= getBasicInspectParameterFn(); - const args = [inspect(this.namespace, options), inspect(this.oid, options)]; + const namespaceArg = addQuotes + ? `'${inspect(this.namespace, options)}'` + : inspect(this.namespace, options); + const objectIDArg = addQuotes + ? `new ObjectId('${inspect(this.oid, options)}')` + : inspect(this.oid, options); + const args = [namespaceArg, objectIDArg]; if (this.db) { - args.push(inspect(this.db, options)); + args.push(addQuotes ? `'${inspect(this.db, options)}'` : inspect(this.db, options)); } if (Object.keys(this.fields).length > 0) { diff --git a/src/decimal128.ts b/src/decimal128.ts index 5c5e8ca3..cda28c1c 100644 --- a/src/decimal128.ts +++ b/src/decimal128.ts @@ -857,8 +857,12 @@ export class Decimal128 extends BSONValue { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + const addQuotes = inspect ? false : true; inspect ??= getBasicInspectParameterFn(); const d128string = inspect(this.toString(), options); + if (addQuotes) { + return `new Decimal128('${d128string}')`; + } return `new Decimal128(${d128string})`; } } diff --git a/src/long.ts b/src/long.ts index 69f6d396..70ae5944 100644 --- a/src/long.ts +++ b/src/long.ts @@ -1069,7 +1069,7 @@ export class Long extends BSONValue { inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { inspect ??= getBasicInspectParameterFn(); const longVal = inspect(this.toString(), options); - const unsignedVal = this.unsigned ? `', ' + ${inspect(this.unsigned, options)}` : ''; + const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : ''; return `new Long(${longVal}${unsignedVal})`; } } diff --git a/src/objectid.ts b/src/objectid.ts index 67d0d5b4..50883b8d 100644 --- a/src/objectid.ts +++ b/src/objectid.ts @@ -306,7 +306,11 @@ export class ObjectId extends BSONValue { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + const addQuotes = inspect ? false : true; inspect ??= getBasicInspectParameterFn(); + if (addQuotes) { + return `new ObjectId('${inspect(this.toHexString(), options)}')`; + } return `new ObjectId(${inspect(this.toHexString(), options)})`; } } diff --git a/src/regexp.ts b/src/regexp.ts index 4b93f0d9..5bc0670f 100644 --- a/src/regexp.ts +++ b/src/regexp.ts @@ -112,7 +112,7 @@ export class BSONRegExp extends BSONValue { inspect(depth?: number, options?: unknown): string { const stylize = getStylizeFunction(options); const pattern = stylize(`'${this.pattern}'`, 'regexp'); - const flags = stylize(`${this.options}'`, 'regexp'); + const flags = stylize(`'${this.options}'`, 'regexp'); return `new BSONRegExp(${pattern}, ${flags})`; } } diff --git a/src/symbol.ts b/src/symbol.ts index b5d6f274..0f87c763 100644 --- a/src/symbol.ts +++ b/src/symbol.ts @@ -58,7 +58,11 @@ export class BSONSymbol extends BSONValue { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + const addQuotes = inspect ? false : true; inspect ??= getBasicInspectParameterFn(); + if (addQuotes) { + return `new BSONSymbol('${inspect(this.value, options)}')`; + } return `new BSONSymbol(${inspect(this.value, options)})`; } } diff --git a/test/node/binary.test.ts b/test/node/binary.test.ts index 5fdaa107..09a4f972 100644 --- a/test/node/binary.test.ts +++ b/test/node/binary.test.ts @@ -81,27 +81,27 @@ describe('class Binary', () => { }); context('inspect()', () => { - it('when value is default returns "Binary.createFromBase64("", 0)"', () => { - expect(new Binary().inspect()).to.equal('Binary.createFromBase64("", 0)'); + it(`when value is default returns "Binary.createFromBase64('', 0)"`, () => { + expect(new Binary().inspect()).to.equal(`Binary.createFromBase64('', 0)`); }); - it('when value is empty returns "Binary.createFromBase64("", 0)"', () => { - expect(new Binary(new Uint8Array(0)).inspect()).to.equal('Binary.createFromBase64("", 0)'); + it(`when value is empty returns "Binary.createFromBase64('', 0)"`, () => { + expect(new Binary(new Uint8Array(0)).inspect()).to.equal(`Binary.createFromBase64('', 0)`); }); - it('when value is default with a subtype returns "Binary.createFromBase64("", 35)"', () => { - expect(new Binary(null, 0x23).inspect()).to.equal('Binary.createFromBase64("", 35)'); + it(`when value is default with a subtype returns "Binary.createFromBase64('', 35)"`, () => { + expect(new Binary(undefined, 0x23).inspect()).to.equal(`Binary.createFromBase64('', 35)`); }); - it('when value is empty with a subtype returns "Binary.createFromBase64("", 35)"', () => { + it(`when value is empty with a subtype returns "Binary.createFromBase64('', 35)"`, () => { expect(new Binary(new Uint8Array(0), 0x23).inspect()).to.equal( - 'Binary.createFromBase64("", 35)' + `Binary.createFromBase64('', 35)` ); }); - it('when value has utf8 "abcdef" encoded returns "Binary.createFromBase64("YWJjZGVm", 0)"', () => { + it(`when value has utf8 "abcdef" encoded returns "Binary.createFromBase64('YWJjZGVm', 0)"`, () => { expect(new Binary(Buffer.from('abcdef', 'utf8')).inspect()).to.equal( - 'Binary.createFromBase64("YWJjZGVm", 0)' + `Binary.createFromBase64('YWJjZGVm', 0)` ); }); diff --git a/test/node/bson_test.js b/test/node/bson_test.js index b0110dd3..f4e3219a 100644 --- a/test/node/bson_test.js +++ b/test/node/bson_test.js @@ -1827,7 +1827,7 @@ describe('BSON', function () { */ it('Binary', function () { const binary = new Binary(Buffer.from('0123456789abcdef0123456789abcdef', 'hex'), 4); - expect(inspect(binary)).to.equal('Binary.createFromBase64("ASNFZ4mrze8BI0VniavN7w==", 4)'); + expect(inspect(binary)).to.equal(`Binary.createFromBase64('ASNFZ4mrze8BI0VniavN7w==', 4)`); }); /** @@ -1835,7 +1835,7 @@ describe('BSON', function () { */ it('BSONSymbol', function () { const symbol = new BSONSymbol('sym'); - expect(inspect(symbol)).to.equal('new BSONSymbol("sym")'); + expect(inspect(symbol)).to.equal(`new BSONSymbol('sym')`); }); /** @@ -1843,7 +1843,7 @@ describe('BSON', function () { */ it('Code', function () { const code = new Code('this.a > i', { i: 1 }); - expect(inspect(code)).to.equal('new Code("this.a > i", {"i":1})'); + expect(inspect(code)).to.equal(`new Code('this.a > i', { i: 1 })`); }); /** @@ -1853,7 +1853,7 @@ describe('BSON', function () { const oid = new ObjectId('deadbeefdeadbeefdeadbeef'); const dbref = new DBRef('namespace', oid, 'integration_tests_'); expect(inspect(dbref)).to.equal( - 'new DBRef("namespace", new ObjectId("deadbeefdeadbeefdeadbeef"), "integration_tests_")' + `new DBRef('namespace', new ObjectId('deadbeefdeadbeefdeadbeef'), 'integration_tests_')` ); }); @@ -1862,7 +1862,7 @@ describe('BSON', function () { */ it('Decimal128', function () { const dec = Decimal128.fromString('1.42'); - expect(inspect(dec)).to.equal('new Decimal128("1.42")'); + expect(inspect(dec)).to.equal(`new Decimal128('1.42')`); }); /** @@ -1886,10 +1886,10 @@ describe('BSON', function () { */ it('Long', function () { const long = Long.fromString('42'); - expect(inspect(long)).to.equal('new Long("42")'); + expect(inspect(long)).to.equal(`new Long('42')`); const unsignedLong = Long.fromString('42', true); - expect(inspect(unsignedLong)).to.equal('new Long("42", true)'); + expect(inspect(unsignedLong)).to.equal(`new Long('42', true)`); }); /** diff --git a/test/node/code.test.ts b/test/node/code.test.ts index 4e28541b..6b6cd3e5 100644 --- a/test/node/code.test.ts +++ b/test/node/code.test.ts @@ -10,9 +10,9 @@ describe('class Code', () => { }); it('prints re-evaluatable output for Code that contains quotes', () => { - const codeStringInput = new BSON.Code('function a(){ return "asdf"; }'); + const codeStringInput = new BSON.Code(`function a(){ return 'asdf'; }`); expect(inspect(codeStringInput)).to.equal( - String.raw`new Code("function a(){ return \"asdf\"; }")` + String.raw`new Code("function a(){ return 'asdf'; }")` ); }); diff --git a/test/node/object_id.test.ts b/test/node/object_id.test.ts index 62d0dd5a..d49f3bb5 100644 --- a/test/node/object_id.test.ts +++ b/test/node/object_id.test.ts @@ -315,7 +315,7 @@ describe('ObjectId', function () { it('should correctly allow for node.js inspect to work with ObjectId', function (done) { const a = 'AAAAAAAAAAAAAAAAAAAAAAAA'; const b = new ObjectId(a); - expect(util.inspect(b)).to.equal('new ObjectId("aaaaaaaaaaaaaaaaaaaaaaaa")'); + expect(util.inspect(b)).to.equal(`new ObjectId('aaaaaaaaaaaaaaaaaaaaaaaa')`); done(); }); diff --git a/test/node/symbol.test.ts b/test/node/symbol.test.ts index f26ab3f3..d6325b80 100644 --- a/test/node/symbol.test.ts +++ b/test/node/symbol.test.ts @@ -44,7 +44,7 @@ describe('class BSONSymbol', () => { }); it('prints re-evaluatable output for BSONSymbol that contains quotes', () => { - const input = new BSONSymbol('asdf"ghjk'); - expect(inspect(input)).to.equal(String.raw`new BSONSymbol("asdf\"ghjk")`); + const input = new BSONSymbol(`asdf'ghjk`); + expect(inspect(input)).to.equal(String.raw`new BSONSymbol("asdf'ghjk")`); }); }); diff --git a/test/node/uuid.test.ts b/test/node/uuid.test.ts index 77d6411b..dddf0d9b 100644 --- a/test/node/uuid.test.ts +++ b/test/node/uuid.test.ts @@ -144,7 +144,7 @@ describe('UUID', () => { it('should correctly allow for node.js inspect to work with UUID', () => { const uuid = new UUID(UPPERCASE_DASH_SEPARATED_UUID_STRING); - expect(inspect(uuid)).to.equal(`new UUID("${LOWERCASE_DASH_SEPARATED_UUID_STRING}")`); + expect(inspect(uuid)).to.equal(`new UUID('${LOWERCASE_DASH_SEPARATED_UUID_STRING}')`); }); describe('serialize', () => { From e538cd437cd675a97f6614b2a3db3e768fad3704 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Mon, 9 Oct 2023 12:06:27 -0400 Subject: [PATCH 08/16] fixed failing evergreen tests --- test/colors.mjs | 58 ------------------------------------------------- 1 file changed, 58 deletions(-) delete mode 100644 test/colors.mjs diff --git a/test/colors.mjs b/test/colors.mjs deleted file mode 100644 index 243e00c2..00000000 --- a/test/colors.mjs +++ /dev/null @@ -1,58 +0,0 @@ -'use strict'; - -import { - Binary, - UUID, - Code, - DBRef, - Decimal128, - Double, - Int32, - Long, - ObjectId, - BSONRegExp, - BSONSymbol, - Timestamp, - MaxKey, - MinKey -} from '../lib/bson.mjs'; - -console.log({ - binary: new Binary(Buffer.from('abcdef', 'utf8'), 0x06), - uuid: new UUID(), - code: new Code(function iLoveJavaScript() { - do { - console.log('hello!'); - } while (Math.random() > 0.5); - }), - c: new Code( - function iLoveJavaScript() { - do { - console.log('hello!'); - } while (Math.random() > 0.5); - }, - { context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}} - ), - c2: new Code ( - function iLoveJavaScript() { return `js`; }, - { context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}} - ), - dbref: new DBRef('collection', new ObjectId('00'.repeat(12))), - dbref_db: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db'), - dbref_db_fields: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db', { a: 1 }), - decimal128: new Decimal128('1.353e34'), - double: new Double(2.354), - double2: new Double(2), - double3: new Double(-0), - int32: new Int32('4577'), - long: new Long(-12442), - objectid: new ObjectId('00'.repeat(12)), - bsonregexp: new BSONRegExp('abc', 'imx'), - bsonsymbol: new BSONSymbol('my symbol'), - timestamp: new Timestamp({ i: 2345, t: 23453 }), - maxkey: new MaxKey(), - minkey: new MinKey() -}); - -const oid = new ObjectId('00'.repeat(12)); -console.log(oid); \ No newline at end of file From a14ae9226c4ec2624597b07a0b634ea6a08bd039 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Mon, 9 Oct 2023 16:33:24 -0400 Subject: [PATCH 09/16] PR Requested Changes 0 --- src/binary.ts | 25 ++++--------------------- src/bson_value.ts | 11 ++++++++++- src/code.ts | 13 ++----------- src/db_ref.ts | 13 ++----------- src/decimal128.ts | 13 ++----------- src/double.ts | 13 ++----------- src/int_32.ts | 13 ++----------- src/long.ts | 13 ++----------- src/max_key.ts | 5 ----- src/min_key.ts | 5 ----- src/objectid.ts | 13 ++----------- src/parser/utils.ts | 13 ++++++------- src/regexp.ts | 4 ++-- src/symbol.ts | 4 ++-- src/timestamp.ts | 13 ++----------- test/types/bson.test-d.ts | 3 ++- 16 files changed, 42 insertions(+), 132 deletions(-) diff --git a/src/binary.ts b/src/binary.ts index 50bcc653..d128e4a0 100644 --- a/src/binary.ts +++ b/src/binary.ts @@ -1,6 +1,6 @@ import { type InspectParameterFn, - getBasicInspectParameterFn, + basicInspectParameterFn, isAnyArrayBuffer, isUint8Array } from './parser/utils'; @@ -268,18 +268,9 @@ export class Binary extends BSONValue { return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type); } - /** @internal */ - [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 { const addQuotes = inspect ? false : true; - inspect ??= getBasicInspectParameterFn(); + inspect ??= basicInspectParameterFn; const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position)); const base64Arg = inspect(base64, options); const subTypeArg = inspect(this.sub_type, options); @@ -479,19 +470,11 @@ export class UUID extends Binary { * Converts to a string representation of this Id. * * @returns return the 36 character hex string representation. - * @internal + * */ - [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 { const addQuotes = inspect ? false : true; - inspect ??= getBasicInspectParameterFn(); + inspect ??= basicInspectParameterFn; if (addQuotes) { return `new UUID('${inspect(this.toHexString(), options)}')`; } diff --git a/src/bson_value.ts b/src/bson_value.ts index b36266e0..c23875b7 100644 --- a/src/bson_value.ts +++ b/src/bson_value.ts @@ -1,4 +1,5 @@ import { BSON_MAJOR_VERSION } from './constants'; +import { type InspectParameterFn } from './parser/utils'; /** @public */ export abstract class BSONValue { @@ -10,8 +11,16 @@ export abstract class BSONValue { return BSON_MAJOR_VERSION; } + [Symbol.for('nodejs.util.inspect.custom')]( + depth?: number, + options?: unknown, + inspect?: InspectParameterFn + ): string { + return this.inspect(depth, options, inspect); + } + /** @public */ - public abstract inspect(): string; + public abstract inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string; /** @internal */ abstract toExtendedJSON(): unknown; diff --git a/src/code.ts b/src/code.ts index 950066b9..d3e2476b 100644 --- a/src/code.ts +++ b/src/code.ts @@ -1,6 +1,6 @@ import type { Document } from './bson'; import { BSONValue } from './bson_value'; -import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils'; +import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils'; /** @public */ export interface CodeExtended { @@ -56,17 +56,8 @@ export class Code extends BSONValue { return new Code(doc.$code, doc.$scope); } - /** @internal */ - [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(); + inspect ??= basicInspectParameterFn; let parametersString = inspect(this.code, options); const multiLineFn = parametersString.includes('\n'); if (this.scope != null) { diff --git a/src/db_ref.ts b/src/db_ref.ts index b225489e..93030bfa 100644 --- a/src/db_ref.ts +++ b/src/db_ref.ts @@ -2,7 +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'; +import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils'; /** @public */ export interface DBRefLike { @@ -112,18 +112,9 @@ export class DBRef extends BSONValue { return new DBRef(doc.$ref, doc.$id, doc.$db, copy); } - /** @internal */ - [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 { const addQuotes = inspect ? false : true; - inspect ??= getBasicInspectParameterFn(); + inspect ??= basicInspectParameterFn; const namespaceArg = addQuotes ? `'${inspect(this.namespace, options)}'` diff --git a/src/decimal128.ts b/src/decimal128.ts index cda28c1c..ce985a4c 100644 --- a/src/decimal128.ts +++ b/src/decimal128.ts @@ -1,7 +1,7 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; import { Long } from './long'; -import { type InspectParameterFn, getBasicInspectParameterFn, isUint8Array } from './parser/utils'; +import { type InspectParameterFn, basicInspectParameterFn, isUint8Array } from './parser/utils'; import { ByteUtils } from './utils/byte_utils'; const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/; @@ -847,18 +847,9 @@ export class Decimal128 extends BSONValue { return Decimal128.fromString(doc.$numberDecimal); } - /** @internal */ - [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 { const addQuotes = inspect ? false : true; - inspect ??= getBasicInspectParameterFn(); + inspect ??= basicInspectParameterFn; const d128string = inspect(this.toString(), options); if (addQuotes) { return `new Decimal128('${d128string}')`; diff --git a/src/double.ts b/src/double.ts index 003231ae..9b391d02 100644 --- a/src/double.ts +++ b/src/double.ts @@ -1,6 +1,6 @@ import { BSONValue } from './bson_value'; import type { EJSONOptions } from './extended_json'; -import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils'; +import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils'; /** @public */ export interface DoubleExtended { @@ -72,17 +72,8 @@ export class Double extends BSONValue { return options && options.relaxed ? doubleValue : new Double(doubleValue); } - /** @internal */ - [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(); + inspect ??= basicInspectParameterFn; return `new Double(${inspect(this.valueOf(), options)})`; } } diff --git a/src/int_32.ts b/src/int_32.ts index 83ee3d7c..a36f0205 100644 --- a/src/int_32.ts +++ b/src/int_32.ts @@ -1,6 +1,6 @@ import { BSONValue } from './bson_value'; import type { EJSONOptions } from './extended_json'; -import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils'; +import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils'; /** @public */ export interface Int32Extended { @@ -60,17 +60,8 @@ export class Int32 extends BSONValue { return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt); } - /** @internal */ - [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(); + inspect ??= basicInspectParameterFn; return `new Int32(${inspect(this.value, options)})`; } } diff --git a/src/long.ts b/src/long.ts index 70ae5944..ef2789a4 100644 --- a/src/long.ts +++ b/src/long.ts @@ -1,7 +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 InspectParameterFn, basicInspectParameterFn } from './parser/utils'; import type { Timestamp } from './timestamp'; interface LongWASMHelpers { @@ -1057,17 +1057,8 @@ export class Long extends BSONValue { return longResult; } - /** @internal */ - [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(); + inspect ??= basicInspectParameterFn; const longVal = inspect(this.toString(), options); const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : ''; return `new Long(${longVal}${unsignedVal})`; diff --git a/src/max_key.ts b/src/max_key.ts index 2894f58b..903f1d16 100644 --- a/src/max_key.ts +++ b/src/max_key.ts @@ -25,11 +25,6 @@ export class MaxKey extends BSONValue { return new MaxKey(); } - /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](): string { - return this.inspect(); - } - inspect(): string { return 'new MaxKey()'; } diff --git a/src/min_key.ts b/src/min_key.ts index f72cf26b..244e645a 100644 --- a/src/min_key.ts +++ b/src/min_key.ts @@ -25,11 +25,6 @@ export class MinKey extends BSONValue { return new MinKey(); } - /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](): string { - return this.inspect(); - } - inspect(): string { return 'new MinKey()'; } diff --git a/src/objectid.ts b/src/objectid.ts index 50883b8d..8085fd7a 100644 --- a/src/objectid.ts +++ b/src/objectid.ts @@ -1,6 +1,6 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; -import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils'; +import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils'; import { BSONDataView, ByteUtils } from './utils/byte_utils'; // Regular expression that checks for hex value @@ -295,19 +295,10 @@ export class ObjectId extends BSONValue { * Converts to a string representation of this Id. * * @returns return the 24 character hex string representation. - * @internal */ - [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 { const addQuotes = inspect ? false : true; - inspect ??= getBasicInspectParameterFn(); + inspect ??= basicInspectParameterFn; if (addQuotes) { return `new ObjectId('${inspect(this.toHexString(), options)}')`; } diff --git a/src/parser/utils.ts b/src/parser/utils.ts index 312a5853..95fe81d1 100644 --- a/src/parser/utils.ts +++ b/src/parser/utils.ts @@ -29,12 +29,11 @@ export function isDate(d: unknown): d is Date { } /** @internal */ -export type StylizeFunction = (x: unknown, style: string) => string; +export type StylizeFunction = (x: string, 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}`; -} +// eslint-disable-next-line @typescript-eslint/restrict-template-expressions +export const basicInspectParameterFn: InspectParameterFn = v => `${v}`; + export function getStylizeFunction(options?: unknown): StylizeFunction { const stylizeExists = options != null && @@ -43,8 +42,8 @@ export function getStylizeFunction(options?: unknown): StylizeFunction { typeof options.stylize === 'function'; if (stylizeExists) { - return options.stylize as (x: unknown, style: string) => string; + return options.stylize as StylizeFunction; } else { - return getBasicInspectParameterFn(); + return basicInspectParameterFn; } } diff --git a/src/regexp.ts b/src/regexp.ts index 5bc0670f..d3bd03da 100644 --- a/src/regexp.ts +++ b/src/regexp.ts @@ -104,8 +104,8 @@ export class BSONRegExp extends BSONValue { throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`); } - /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string { + /** @internal */ + [Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string { return this.inspect(depth, options); } diff --git a/src/symbol.ts b/src/symbol.ts index 0f87c763..ab8c814a 100644 --- a/src/symbol.ts +++ b/src/symbol.ts @@ -1,5 +1,5 @@ import { BSONValue } from './bson_value'; -import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils'; +import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils'; /** @public */ export interface BSONSymbolExtended { @@ -59,7 +59,7 @@ export class BSONSymbol extends BSONValue { inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { const addQuotes = inspect ? false : true; - inspect ??= getBasicInspectParameterFn(); + inspect ??= basicInspectParameterFn; if (addQuotes) { return `new BSONSymbol('${inspect(this.value, options)}')`; } diff --git a/src/timestamp.ts b/src/timestamp.ts index 6221ae25..dd2735dd 100644 --- a/src/timestamp.ts +++ b/src/timestamp.ts @@ -1,7 +1,7 @@ import { BSONError } from './error'; import type { Int32 } from './int_32'; import { Long } from './long'; -import { type InspectParameterFn, getBasicInspectParameterFn } from './parser/utils'; +import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils'; /** @public */ export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect'; @@ -142,17 +142,8 @@ export class Timestamp extends LongWithoutOverridesClass { return new Timestamp({ t, i }); } - /** @internal */ - [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(); + inspect ??= basicInspectParameterFn; const t = inspect(this.getHighBits(), options); const i = inspect(this.getLowBits(), options); return `new Timestamp({ t: ${t}, i: ${i} })`; diff --git a/test/types/bson.test-d.ts b/test/types/bson.test-d.ts index afa89b91..3baf8b9d 100644 --- a/test/types/bson.test-d.ts +++ b/test/types/bson.test-d.ts @@ -19,6 +19,7 @@ import { Decimal128Extended, BSONValue } from '../../bson'; // import from generated bson.d.ts +import type { InspectParameterFn } from '../../src/parser/utils'; expectType<() => UUID>(Binary.prototype.toUUID); expectType<() => Binary>(UUID.prototype.toBinary); @@ -75,4 +76,4 @@ expectType<'Binary'>(UUID.prototype._bsontype) // Common BSONValue interface declare const bsonValue: BSONValue; expectType(bsonValue._bsontype); -expectType<() => string>(bsonValue.inspect); +expectType<(depth?: number | undefined, options?: unknown, inspect?: InspectParameterFn | undefined) => string>(bsonValue.inspect); From 59eda94969472f3aee6d932a8d1a8aca4916eb8e Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Tue, 10 Oct 2023 16:19:57 -0400 Subject: [PATCH 10/16] PR requested changes 1 --- src/binary.ts | 4 +-- src/db_ref.ts | 23 ++++++---------- src/decimal128.ts | 2 +- src/objectid.ts | 2 +- src/regexp.ts | 5 ---- src/symbol.ts | 2 +- test/colors.mjs | 58 +++++++++++++++++++++++++++++++++++++++ test/node/bson_test.js | 61 +++++++++++++++++++++++++++++++++++------- 8 files changed, 123 insertions(+), 34 deletions(-) create mode 100644 test/colors.mjs diff --git a/src/binary.ts b/src/binary.ts index d128e4a0..8b1ca7e1 100644 --- a/src/binary.ts +++ b/src/binary.ts @@ -269,7 +269,7 @@ export class Binary extends BSONValue { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - const addQuotes = inspect ? false : true; + const addQuotes = !inspect; inspect ??= basicInspectParameterFn; const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position)); const base64Arg = inspect(base64, options); @@ -473,7 +473,7 @@ export class UUID extends Binary { * */ inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - const addQuotes = inspect ? false : true; + const addQuotes = !inspect; inspect ??= basicInspectParameterFn; if (addQuotes) { return `new UUID('${inspect(this.toHexString(), options)}')`; diff --git a/src/db_ref.ts b/src/db_ref.ts index 93030bfa..629ba977 100644 --- a/src/db_ref.ts +++ b/src/db_ref.ts @@ -113,24 +113,17 @@ export class DBRef extends BSONValue { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - const addQuotes = inspect ? false : true; + const addQuotes = !inspect; inspect ??= basicInspectParameterFn; - const namespaceArg = addQuotes - ? `'${inspect(this.namespace, options)}'` - : inspect(this.namespace, options); - const objectIDArg = addQuotes - ? `new ObjectId('${inspect(this.oid, options)}')` - : inspect(this.oid, options); - const args = [namespaceArg, objectIDArg]; + const args = [ + inspect(this.namespace, options), + inspect(this.oid, options), + ...(this.db ? [inspect(this.db, options)] : []), + ...(Object.keys(this.fields).length > 0 ? [inspect(this.fields, options)] : []) + ].map(arg => addQuotes ? `'${arg}'` : arg); - if (this.db) { - args.push(addQuotes ? `'${inspect(this.db, options)}'` : inspect(this.db, options)); - } - - if (Object.keys(this.fields).length > 0) { - args.push(inspect(this.fields, options)); - } + args[1] = addQuotes ? `new ObjectId(${args[1]})` : args[1]; return `new DBRef(${args.join(', ')})`; } diff --git a/src/decimal128.ts b/src/decimal128.ts index ce985a4c..7d98d8ab 100644 --- a/src/decimal128.ts +++ b/src/decimal128.ts @@ -848,7 +848,7 @@ export class Decimal128 extends BSONValue { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - const addQuotes = inspect ? false : true; + const addQuotes = !inspect; inspect ??= basicInspectParameterFn; const d128string = inspect(this.toString(), options); if (addQuotes) { diff --git a/src/objectid.ts b/src/objectid.ts index 8085fd7a..ca46036b 100644 --- a/src/objectid.ts +++ b/src/objectid.ts @@ -297,7 +297,7 @@ export class ObjectId extends BSONValue { * @returns return the 24 character hex string representation. */ inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - const addQuotes = inspect ? false : true; + const addQuotes = !inspect; inspect ??= basicInspectParameterFn; if (addQuotes) { return `new ObjectId('${inspect(this.toHexString(), options)}')`; diff --git a/src/regexp.ts b/src/regexp.ts index d3bd03da..013ca559 100644 --- a/src/regexp.ts +++ b/src/regexp.ts @@ -104,11 +104,6 @@ export class BSONRegExp extends BSONValue { throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`); } - /** @internal */ - [Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string { - return this.inspect(depth, options); - } - inspect(depth?: number, options?: unknown): string { const stylize = getStylizeFunction(options); const pattern = stylize(`'${this.pattern}'`, 'regexp'); diff --git a/src/symbol.ts b/src/symbol.ts index ab8c814a..b55d6b54 100644 --- a/src/symbol.ts +++ b/src/symbol.ts @@ -58,7 +58,7 @@ export class BSONSymbol extends BSONValue { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - const addQuotes = inspect ? false : true; + const addQuotes = !inspect; inspect ??= basicInspectParameterFn; if (addQuotes) { return `new BSONSymbol('${inspect(this.value, options)}')`; diff --git a/test/colors.mjs b/test/colors.mjs new file mode 100644 index 00000000..243e00c2 --- /dev/null +++ b/test/colors.mjs @@ -0,0 +1,58 @@ +'use strict'; + +import { + Binary, + UUID, + Code, + DBRef, + Decimal128, + Double, + Int32, + Long, + ObjectId, + BSONRegExp, + BSONSymbol, + Timestamp, + MaxKey, + MinKey +} from '../lib/bson.mjs'; + +console.log({ + binary: new Binary(Buffer.from('abcdef', 'utf8'), 0x06), + uuid: new UUID(), + code: new Code(function iLoveJavaScript() { + do { + console.log('hello!'); + } while (Math.random() > 0.5); + }), + c: new Code( + function iLoveJavaScript() { + do { + console.log('hello!'); + } while (Math.random() > 0.5); + }, + { context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}} + ), + c2: new Code ( + function iLoveJavaScript() { return `js`; }, + { context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}} + ), + dbref: new DBRef('collection', new ObjectId('00'.repeat(12))), + dbref_db: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db'), + dbref_db_fields: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db', { a: 1 }), + decimal128: new Decimal128('1.353e34'), + double: new Double(2.354), + double2: new Double(2), + double3: new Double(-0), + int32: new Int32('4577'), + long: new Long(-12442), + objectid: new ObjectId('00'.repeat(12)), + bsonregexp: new BSONRegExp('abc', 'imx'), + bsonsymbol: new BSONSymbol('my symbol'), + timestamp: new Timestamp({ i: 2345, t: 23453 }), + maxkey: new MaxKey(), + minkey: new MinKey() +}); + +const oid = new ObjectId('00'.repeat(12)); +console.log(oid); \ No newline at end of file diff --git a/test/node/bson_test.js b/test/node/bson_test.js index f4e3219a..8796cce6 100644 --- a/test/node/bson_test.js +++ b/test/node/bson_test.js @@ -1841,20 +1841,63 @@ describe('BSON', function () { /** * @ignore */ - it('Code', function () { - const code = new Code('this.a > i', { i: 1 }); - expect(inspect(code)).to.equal(`new Code('this.a > i', { i: 1 })`); + context('Code', function () { + it('when non-nested fields', function () { + const code = new Code('this.a > i', { i: 1 }); + expect(inspect(code)).to.equal(`new Code('this.a > i', { i: 1 })`); + }); + it('when non-nested fields', function () { + const code = new Code('this.a > i', { a: 1, b: { nest: 'mine' } }); + expect(inspect(code)).to.equal(`new Code('this.a > i', { a: 1, b: { nest: 'mine' } })`); + }); + it('when multiline code', function () { + const code = new Code( + function iLoveJavaScript() { + do { + console.log('hello!'); + } while (Math.random() > 0.5); + }, + { context: 'random looping!', reference: Long.fromString('2345'), my_map: { a: 1 } } + ); + expect(inspect(code)).to.equal( + /* eslint-disable */ +`new Code( +'function iLoveJavaScript() {\\n' + + ' do {\\n' + + " console.log('hello!');\\n" + + ' } while (Math.random() > 0.5);\\n' + + ' }', +{ + context: 'random looping!', + reference: new Long('2345'), + my_map: { a: 1 } +})` + /* eslint-enable */ + ); + }); }); /** * @ignore */ - it('DBRef', function () { - const oid = new ObjectId('deadbeefdeadbeefdeadbeef'); - const dbref = new DBRef('namespace', oid, 'integration_tests_'); - expect(inspect(dbref)).to.equal( - `new DBRef('namespace', new ObjectId('deadbeefdeadbeefdeadbeef'), 'integration_tests_')` - ); + context('DBRef', function () { + it('when non-nested fields', function () { + const oid = new ObjectId('deadbeefdeadbeefdeadbeef'); + const dbref = new DBRef('namespace', oid, 'integration_tests_'); + expect(inspect(dbref)).to.equal( + `new DBRef('namespace', new ObjectId('deadbeefdeadbeefdeadbeef'), 'integration_tests_')` + ); + }); + it('when nested fields', function () { + const oid = new ObjectId('deadbeefdeadbeefdeadbeef'); + const dbref = new DBRef('namespace', oid, 'integration_tests_', { + a: 1, + b: { nest: 'mine' } + }); + expect(inspect(dbref)).to.equal( + `new DBRef('namespace', new ObjectId('deadbeefdeadbeefdeadbeef'), 'integration_tests_', { a: 1, b: { nest: 'mine' } })` + ); + }); }); /** From 83b81366c545648b4aa0cb57a50f5662b91838dc Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Tue, 10 Oct 2023 16:23:27 -0400 Subject: [PATCH 11/16] lint fix --- src/db_ref.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/db_ref.ts b/src/db_ref.ts index 629ba977..e3937afc 100644 --- a/src/db_ref.ts +++ b/src/db_ref.ts @@ -121,9 +121,9 @@ export class DBRef extends BSONValue { inspect(this.oid, options), ...(this.db ? [inspect(this.db, options)] : []), ...(Object.keys(this.fields).length > 0 ? [inspect(this.fields, options)] : []) - ].map(arg => addQuotes ? `'${arg}'` : arg); + ].map(arg => (addQuotes ? `'${arg}'` : arg)); - args[1] = addQuotes ? `new ObjectId(${args[1]})` : args[1]; + args[1] = addQuotes ? `new ObjectId(${args[1]})` : args[1]; return `new DBRef(${args.join(', ')})`; } From cf5d1328ec8173e3d40d0e7b21d8a981030e1291 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Wed, 11 Oct 2023 13:11:32 -0400 Subject: [PATCH 12/16] test name changes --- test/colors.mjs | 58 ------------------------------------------ test/node/bson_test.js | 10 ++++---- 2 files changed, 5 insertions(+), 63 deletions(-) delete mode 100644 test/colors.mjs diff --git a/test/colors.mjs b/test/colors.mjs deleted file mode 100644 index 243e00c2..00000000 --- a/test/colors.mjs +++ /dev/null @@ -1,58 +0,0 @@ -'use strict'; - -import { - Binary, - UUID, - Code, - DBRef, - Decimal128, - Double, - Int32, - Long, - ObjectId, - BSONRegExp, - BSONSymbol, - Timestamp, - MaxKey, - MinKey -} from '../lib/bson.mjs'; - -console.log({ - binary: new Binary(Buffer.from('abcdef', 'utf8'), 0x06), - uuid: new UUID(), - code: new Code(function iLoveJavaScript() { - do { - console.log('hello!'); - } while (Math.random() > 0.5); - }), - c: new Code( - function iLoveJavaScript() { - do { - console.log('hello!'); - } while (Math.random() > 0.5); - }, - { context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}} - ), - c2: new Code ( - function iLoveJavaScript() { return `js`; }, - { context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}} - ), - dbref: new DBRef('collection', new ObjectId('00'.repeat(12))), - dbref_db: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db'), - dbref_db_fields: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db', { a: 1 }), - decimal128: new Decimal128('1.353e34'), - double: new Double(2.354), - double2: new Double(2), - double3: new Double(-0), - int32: new Int32('4577'), - long: new Long(-12442), - objectid: new ObjectId('00'.repeat(12)), - bsonregexp: new BSONRegExp('abc', 'imx'), - bsonsymbol: new BSONSymbol('my symbol'), - timestamp: new Timestamp({ i: 2345, t: 23453 }), - maxkey: new MaxKey(), - minkey: new MinKey() -}); - -const oid = new ObjectId('00'.repeat(12)); -console.log(oid); \ No newline at end of file diff --git a/test/node/bson_test.js b/test/node/bson_test.js index 8796cce6..2f495da7 100644 --- a/test/node/bson_test.js +++ b/test/node/bson_test.js @@ -1842,15 +1842,15 @@ describe('BSON', function () { * @ignore */ context('Code', function () { - it('when non-nested fields', function () { + it('when it contains non-nested fields', function () { const code = new Code('this.a > i', { i: 1 }); expect(inspect(code)).to.equal(`new Code('this.a > i', { i: 1 })`); }); - it('when non-nested fields', function () { + it('when it contains nested fields', function () { const code = new Code('this.a > i', { a: 1, b: { nest: 'mine' } }); expect(inspect(code)).to.equal(`new Code('this.a > i', { a: 1, b: { nest: 'mine' } })`); }); - it('when multiline code', function () { + it('when it contains multiline code', function () { const code = new Code( function iLoveJavaScript() { do { @@ -1881,14 +1881,14 @@ describe('BSON', function () { * @ignore */ context('DBRef', function () { - it('when non-nested fields', function () { + it('when it contains non-nested fields', function () { const oid = new ObjectId('deadbeefdeadbeefdeadbeef'); const dbref = new DBRef('namespace', oid, 'integration_tests_'); expect(inspect(dbref)).to.equal( `new DBRef('namespace', new ObjectId('deadbeefdeadbeefdeadbeef'), 'integration_tests_')` ); }); - it('when nested fields', function () { + it('when it contains nested fields', function () { const oid = new ObjectId('deadbeefdeadbeefdeadbeef'); const dbref = new DBRef('namespace', oid, 'integration_tests_', { a: 1, From ca773fd66629f81c2d1501208df51baea0469683 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Wed, 11 Oct 2023 14:15:07 -0400 Subject: [PATCH 13/16] PR requested changes pt 2 --- src/binary.ts | 6 +++--- src/code.ts | 4 ++-- src/db_ref.ts | 4 ++-- src/decimal128.ts | 4 ++-- src/double.ts | 4 ++-- src/int_32.ts | 4 ++-- src/long.ts | 4 ++-- src/objectid.ts | 4 ++-- src/parser/utils.ts | 18 +----------------- src/regexp.ts | 19 ++++++++++++++++++- src/symbol.ts | 4 ++-- src/timestamp.ts | 4 ++-- 12 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/binary.ts b/src/binary.ts index 8b1ca7e1..1125e58a 100644 --- a/src/binary.ts +++ b/src/binary.ts @@ -1,6 +1,6 @@ import { type InspectParameterFn, - basicInspectParameterFn, + defaultInspect, isAnyArrayBuffer, isUint8Array } from './parser/utils'; @@ -270,7 +270,7 @@ export class Binary extends BSONValue { inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { const addQuotes = !inspect; - inspect ??= basicInspectParameterFn; + inspect ??= defaultInspect; const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position)); const base64Arg = inspect(base64, options); const subTypeArg = inspect(this.sub_type, options); @@ -474,7 +474,7 @@ export class UUID extends Binary { */ inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { const addQuotes = !inspect; - inspect ??= basicInspectParameterFn; + inspect ??= defaultInspect; if (addQuotes) { return `new UUID('${inspect(this.toHexString(), options)}')`; } diff --git a/src/code.ts b/src/code.ts index d3e2476b..1392a1ae 100644 --- a/src/code.ts +++ b/src/code.ts @@ -1,6 +1,6 @@ import type { Document } from './bson'; import { BSONValue } from './bson_value'; -import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils'; +import { type InspectParameterFn, defaultInspect } from './parser/utils'; /** @public */ export interface CodeExtended { @@ -57,7 +57,7 @@ export class Code extends BSONValue { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - inspect ??= basicInspectParameterFn; + inspect ??= defaultInspect; let parametersString = inspect(this.code, options); const multiLineFn = parametersString.includes('\n'); if (this.scope != null) { diff --git a/src/db_ref.ts b/src/db_ref.ts index e3937afc..8c87a7d6 100644 --- a/src/db_ref.ts +++ b/src/db_ref.ts @@ -2,7 +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, basicInspectParameterFn } from './parser/utils'; +import { type InspectParameterFn, defaultInspect } from './parser/utils'; /** @public */ export interface DBRefLike { @@ -114,7 +114,7 @@ export class DBRef extends BSONValue { inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { const addQuotes = !inspect; - inspect ??= basicInspectParameterFn; + inspect ??= defaultInspect; const args = [ inspect(this.namespace, options), diff --git a/src/decimal128.ts b/src/decimal128.ts index 7d98d8ab..5fdd3e02 100644 --- a/src/decimal128.ts +++ b/src/decimal128.ts @@ -1,7 +1,7 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; import { Long } from './long'; -import { type InspectParameterFn, basicInspectParameterFn, isUint8Array } from './parser/utils'; +import { type InspectParameterFn, defaultInspect, isUint8Array } from './parser/utils'; import { ByteUtils } from './utils/byte_utils'; const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/; @@ -849,7 +849,7 @@ export class Decimal128 extends BSONValue { inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { const addQuotes = !inspect; - inspect ??= basicInspectParameterFn; + inspect ??= defaultInspect; const d128string = inspect(this.toString(), options); if (addQuotes) { return `new Decimal128('${d128string}')`; diff --git a/src/double.ts b/src/double.ts index 9b391d02..839ce459 100644 --- a/src/double.ts +++ b/src/double.ts @@ -1,6 +1,6 @@ import { BSONValue } from './bson_value'; import type { EJSONOptions } from './extended_json'; -import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils'; +import { type InspectParameterFn, defaultInspect } from './parser/utils'; /** @public */ export interface DoubleExtended { @@ -73,7 +73,7 @@ export class Double extends BSONValue { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - inspect ??= basicInspectParameterFn; + inspect ??= defaultInspect; return `new Double(${inspect(this.valueOf(), options)})`; } } diff --git a/src/int_32.ts b/src/int_32.ts index a36f0205..9d89baa0 100644 --- a/src/int_32.ts +++ b/src/int_32.ts @@ -1,6 +1,6 @@ import { BSONValue } from './bson_value'; import type { EJSONOptions } from './extended_json'; -import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils'; +import { type InspectParameterFn, defaultInspect } from './parser/utils'; /** @public */ export interface Int32Extended { @@ -61,7 +61,7 @@ export class Int32 extends BSONValue { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - inspect ??= basicInspectParameterFn; + inspect ??= defaultInspect; return `new Int32(${inspect(this.value, options)})`; } } diff --git a/src/long.ts b/src/long.ts index ef2789a4..433e5223 100644 --- a/src/long.ts +++ b/src/long.ts @@ -1,7 +1,7 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; import type { EJSONOptions } from './extended_json'; -import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils'; +import { type InspectParameterFn, defaultInspect } from './parser/utils'; import type { Timestamp } from './timestamp'; interface LongWASMHelpers { @@ -1058,7 +1058,7 @@ export class Long extends BSONValue { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - inspect ??= basicInspectParameterFn; + inspect ??= defaultInspect; const longVal = inspect(this.toString(), options); const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : ''; return `new Long(${longVal}${unsignedVal})`; diff --git a/src/objectid.ts b/src/objectid.ts index ca46036b..bcc0c36b 100644 --- a/src/objectid.ts +++ b/src/objectid.ts @@ -1,6 +1,6 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; -import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils'; +import { type InspectParameterFn, defaultInspect } from './parser/utils'; import { BSONDataView, ByteUtils } from './utils/byte_utils'; // Regular expression that checks for hex value @@ -298,7 +298,7 @@ export class ObjectId extends BSONValue { */ inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { const addQuotes = !inspect; - inspect ??= basicInspectParameterFn; + inspect ??= defaultInspect; if (addQuotes) { return `new ObjectId('${inspect(this.toHexString(), options)}')`; } diff --git a/src/parser/utils.ts b/src/parser/utils.ts index 95fe81d1..d638b75c 100644 --- a/src/parser/utils.ts +++ b/src/parser/utils.ts @@ -28,22 +28,6 @@ export function isDate(d: unknown): d is Date { return Object.prototype.toString.call(d) === '[object Date]'; } -/** @internal */ -export type StylizeFunction = (x: string, style: string) => string; export type InspectParameterFn = (x: unknown, options: unknown) => string; // eslint-disable-next-line @typescript-eslint/restrict-template-expressions -export const basicInspectParameterFn: InspectParameterFn = 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 StylizeFunction; - } else { - return basicInspectParameterFn; - } -} +export const defaultInspect: InspectParameterFn = v => `${v}`; diff --git a/src/regexp.ts b/src/regexp.ts index 013ca559..60a39d6e 100644 --- a/src/regexp.ts +++ b/src/regexp.ts @@ -1,7 +1,7 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; import type { EJSONOptions } from './extended_json'; -import { getStylizeFunction } from './parser/utils'; +import { defaultInspect } from './parser/utils'; function alphabetize(str: string): string { return str.split('').sort().join(''); @@ -111,3 +111,20 @@ export class BSONRegExp extends BSONValue { return `new BSONRegExp(${pattern}, ${flags})`; } } + +/** @internal */ +type StylizeFunction = (x: string, style: string) => string; +/** @internal */ +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 StylizeFunction; + } else { + return defaultInspect; + } +} diff --git a/src/symbol.ts b/src/symbol.ts index b55d6b54..cec0b816 100644 --- a/src/symbol.ts +++ b/src/symbol.ts @@ -1,5 +1,5 @@ import { BSONValue } from './bson_value'; -import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils'; +import { type InspectParameterFn, defaultInspect } from './parser/utils'; /** @public */ export interface BSONSymbolExtended { @@ -59,7 +59,7 @@ export class BSONSymbol extends BSONValue { inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { const addQuotes = !inspect; - inspect ??= basicInspectParameterFn; + inspect ??= defaultInspect; if (addQuotes) { return `new BSONSymbol('${inspect(this.value, options)}')`; } diff --git a/src/timestamp.ts b/src/timestamp.ts index dd2735dd..5d8c8a8c 100644 --- a/src/timestamp.ts +++ b/src/timestamp.ts @@ -1,7 +1,7 @@ import { BSONError } from './error'; import type { Int32 } from './int_32'; import { Long } from './long'; -import { type InspectParameterFn, basicInspectParameterFn } from './parser/utils'; +import { type InspectParameterFn, defaultInspect } from './parser/utils'; /** @public */ export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect'; @@ -143,7 +143,7 @@ export class Timestamp extends LongWithoutOverridesClass { } inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - inspect ??= basicInspectParameterFn; + inspect ??= defaultInspect; const t = inspect(this.getHighBits(), options); const i = inspect(this.getLowBits(), options); return `new Timestamp({ t: ${t}, i: ${i} })`; From c0036715cac5142826351b0dda4ddb629ef8ffc3 Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Wed, 11 Oct 2023 15:59:11 -0400 Subject: [PATCH 14/16] Removed extraneous symbol.for --- src/symbol.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/symbol.ts b/src/symbol.ts index cec0b816..a6d778a6 100644 --- a/src/symbol.ts +++ b/src/symbol.ts @@ -48,15 +48,6 @@ export class BSONSymbol extends BSONValue { return new BSONSymbol(doc.$symbol); } - /** @internal */ - [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 { const addQuotes = !inspect; inspect ??= defaultInspect; From fb33d30e92302171335d7e4e6d2178a954afe52c Mon Sep 17 00:00:00 2001 From: Aditi Khare Date: Fri, 13 Oct 2023 12:16:16 -0400 Subject: [PATCH 15/16] PR requested changes 3 --- src/binary.ts | 19 ++----------- src/bson_value.ts | 12 +++++--- src/code.ts | 4 +-- src/db_ref.ts | 9 +++--- src/decimal128.ts | 8 ++---- src/double.ts | 6 ++-- src/int_32.ts | 4 +-- src/long.ts | 4 +-- src/objectid.ts | 8 ++---- src/parser/utils.ts | 29 +++++++++++++++++-- src/regexp.ts | 28 ++++-------------- src/symbol.ts | 8 ++---- src/timestamp.ts | 8 +++--- test/colors.mjs | 60 +++++++++++++++++++++++++++++++++++++++ test/node/binary.test.ts | 15 ++++++---- test/types/bson.test-d.ts | 4 +-- 16 files changed, 137 insertions(+), 89 deletions(-) create mode 100644 test/colors.mjs diff --git a/src/binary.ts b/src/binary.ts index 1125e58a..b08067af 100644 --- a/src/binary.ts +++ b/src/binary.ts @@ -1,9 +1,4 @@ -import { - type InspectParameterFn, - defaultInspect, - isAnyArrayBuffer, - isUint8Array -} from './parser/utils'; +import { type InspectFn, defaultInspect, isAnyArrayBuffer, isUint8Array } from './parser/utils'; import type { EJSONOptions } from './extended_json'; import { BSONError } from './error'; import { BSON_BINARY_SUBTYPE_UUID_NEW } from './constants'; @@ -268,15 +263,11 @@ export class Binary extends BSONValue { return type === BSON_BINARY_SUBTYPE_UUID_NEW ? new UUID(data) : new Binary(data, type); } - inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - const addQuotes = !inspect; + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { inspect ??= defaultInspect; const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position)); const base64Arg = inspect(base64, options); const subTypeArg = inspect(this.sub_type, options); - if (addQuotes) { - return `Binary.createFromBase64('${base64Arg}', ${subTypeArg})`; - } return `Binary.createFromBase64(${base64Arg}, ${subTypeArg})`; } } @@ -472,12 +463,8 @@ export class UUID extends Binary { * @returns return the 36 character hex string representation. * */ - inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - const addQuotes = !inspect; + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { inspect ??= defaultInspect; - if (addQuotes) { - return `new UUID('${inspect(this.toHexString(), options)}')`; - } return `new UUID(${inspect(this.toHexString(), options)})`; } } diff --git a/src/bson_value.ts b/src/bson_value.ts index c23875b7..069764d8 100644 --- a/src/bson_value.ts +++ b/src/bson_value.ts @@ -1,5 +1,5 @@ import { BSON_MAJOR_VERSION } from './constants'; -import { type InspectParameterFn } from './parser/utils'; +import { type InspectFn } from './parser/utils'; /** @public */ export abstract class BSONValue { @@ -14,13 +14,17 @@ export abstract class BSONValue { [Symbol.for('nodejs.util.inspect.custom')]( depth?: number, options?: unknown, - inspect?: InspectParameterFn + inspect?: InspectFn ): string { return this.inspect(depth, options, inspect); } - /** @public */ - public abstract inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string; + /** + * @public + * Prints a human-readable string of BSON value information + * If invoked manually without node.js.inspect function, this will default to a modified JSON.stringify + */ + public abstract inspect(depth?: number, options?: unknown, inspect?: InspectFn): string; /** @internal */ abstract toExtendedJSON(): unknown; diff --git a/src/code.ts b/src/code.ts index 1392a1ae..98b1ede9 100644 --- a/src/code.ts +++ b/src/code.ts @@ -1,6 +1,6 @@ import type { Document } from './bson'; import { BSONValue } from './bson_value'; -import { type InspectParameterFn, defaultInspect } from './parser/utils'; +import { type InspectFn, defaultInspect } from './parser/utils'; /** @public */ export interface CodeExtended { @@ -56,7 +56,7 @@ export class Code extends BSONValue { return new Code(doc.$code, doc.$scope); } - inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { inspect ??= defaultInspect; let parametersString = inspect(this.code, options); const multiLineFn = parametersString.includes('\n'); diff --git a/src/db_ref.ts b/src/db_ref.ts index 8c87a7d6..94131473 100644 --- a/src/db_ref.ts +++ b/src/db_ref.ts @@ -2,7 +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, defaultInspect } from './parser/utils'; +import { type InspectFn, defaultInspect } from './parser/utils'; /** @public */ export interface DBRefLike { @@ -112,8 +112,7 @@ export class DBRef extends BSONValue { return new DBRef(doc.$ref, doc.$id, doc.$db, copy); } - inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - const addQuotes = !inspect; + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { inspect ??= defaultInspect; const args = [ @@ -121,9 +120,9 @@ export class DBRef extends BSONValue { inspect(this.oid, options), ...(this.db ? [inspect(this.db, options)] : []), ...(Object.keys(this.fields).length > 0 ? [inspect(this.fields, options)] : []) - ].map(arg => (addQuotes ? `'${arg}'` : arg)); + ]; - args[1] = addQuotes ? `new ObjectId(${args[1]})` : args[1]; + args[1] = inspect === defaultInspect ? `new ObjectId(${args[1]})` : args[1]; return `new DBRef(${args.join(', ')})`; } diff --git a/src/decimal128.ts b/src/decimal128.ts index 5fdd3e02..dc96f9cd 100644 --- a/src/decimal128.ts +++ b/src/decimal128.ts @@ -1,7 +1,7 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; import { Long } from './long'; -import { type InspectParameterFn, defaultInspect, isUint8Array } from './parser/utils'; +import { type InspectFn, defaultInspect, isUint8Array } from './parser/utils'; import { ByteUtils } from './utils/byte_utils'; const PARSE_STRING_REGEXP = /^(\+|-)?(\d+|(\d*\.\d*))?(E|e)?([-+])?(\d+)?$/; @@ -847,13 +847,9 @@ export class Decimal128 extends BSONValue { return Decimal128.fromString(doc.$numberDecimal); } - inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - const addQuotes = !inspect; + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { inspect ??= defaultInspect; const d128string = inspect(this.toString(), options); - if (addQuotes) { - return `new Decimal128('${d128string}')`; - } return `new Decimal128(${d128string})`; } } diff --git a/src/double.ts b/src/double.ts index 839ce459..6dcec2e9 100644 --- a/src/double.ts +++ b/src/double.ts @@ -1,6 +1,6 @@ import { BSONValue } from './bson_value'; import type { EJSONOptions } from './extended_json'; -import { type InspectParameterFn, defaultInspect } from './parser/utils'; +import { type InspectFn, defaultInspect } from './parser/utils'; /** @public */ export interface DoubleExtended { @@ -72,8 +72,8 @@ export class Double extends BSONValue { return options && options.relaxed ? doubleValue : new Double(doubleValue); } - inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { inspect ??= defaultInspect; - return `new Double(${inspect(this.valueOf(), options)})`; + return `new Double(${inspect(this.value, options)})`; } } diff --git a/src/int_32.ts b/src/int_32.ts index 9d89baa0..31b13c15 100644 --- a/src/int_32.ts +++ b/src/int_32.ts @@ -1,6 +1,6 @@ import { BSONValue } from './bson_value'; import type { EJSONOptions } from './extended_json'; -import { type InspectParameterFn, defaultInspect } from './parser/utils'; +import { type InspectFn, defaultInspect } from './parser/utils'; /** @public */ export interface Int32Extended { @@ -60,7 +60,7 @@ export class Int32 extends BSONValue { return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt); } - inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { inspect ??= defaultInspect; return `new Int32(${inspect(this.value, options)})`; } diff --git a/src/long.ts b/src/long.ts index 433e5223..f05f71e6 100644 --- a/src/long.ts +++ b/src/long.ts @@ -1,7 +1,7 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; import type { EJSONOptions } from './extended_json'; -import { type InspectParameterFn, defaultInspect } from './parser/utils'; +import { type InspectFn, defaultInspect } from './parser/utils'; import type { Timestamp } from './timestamp'; interface LongWASMHelpers { @@ -1057,7 +1057,7 @@ export class Long extends BSONValue { return longResult; } - inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { inspect ??= defaultInspect; const longVal = inspect(this.toString(), options); const unsignedVal = this.unsigned ? `, ${inspect(this.unsigned, options)}` : ''; diff --git a/src/objectid.ts b/src/objectid.ts index bcc0c36b..372b08d8 100644 --- a/src/objectid.ts +++ b/src/objectid.ts @@ -1,6 +1,6 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; -import { type InspectParameterFn, defaultInspect } from './parser/utils'; +import { type InspectFn, defaultInspect } from './parser/utils'; import { BSONDataView, ByteUtils } from './utils/byte_utils'; // Regular expression that checks for hex value @@ -296,12 +296,8 @@ export class ObjectId extends BSONValue { * * @returns return the 24 character hex string representation. */ - inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - const addQuotes = !inspect; + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { inspect ??= defaultInspect; - if (addQuotes) { - return `new ObjectId('${inspect(this.toHexString(), options)}')`; - } return `new ObjectId(${inspect(this.toHexString(), options)})`; } } diff --git a/src/parser/utils.ts b/src/parser/utils.ts index d638b75c..0b27249e 100644 --- a/src/parser/utils.ts +++ b/src/parser/utils.ts @@ -28,6 +28,29 @@ export function isDate(d: unknown): d is Date { return Object.prototype.toString.call(d) === '[object Date]'; } -export type InspectParameterFn = (x: unknown, options: unknown) => string; -// eslint-disable-next-line @typescript-eslint/restrict-template-expressions -export const defaultInspect: InspectParameterFn = v => `${v}`; +export type InspectFn = (x: unknown, options?: unknown) => string; +export function defaultInspect(x: unknown, _options?: unknown): string { + return JSON.stringify(x, (k: string, v: unknown) => { + if (typeof v === 'bigint') { + return { $numberLong: `${v}` }; + } else if (isMap(v)) { + return Object.fromEntries(v); + } + return v; + }); +} + +/** @internal */ +type StylizeFunction = (x: string, style: string) => string; +/** @internal */ +export function getStylizeFunction(options?: unknown): StylizeFunction | undefined { + const stylizeExists = + options != null && + typeof options === 'object' && + 'stylize' in options && + typeof options.stylize === 'function'; + + if (stylizeExists) { + return options.stylize as StylizeFunction; + } +} diff --git a/src/regexp.ts b/src/regexp.ts index 60a39d6e..e401a290 100644 --- a/src/regexp.ts +++ b/src/regexp.ts @@ -1,7 +1,7 @@ import { BSONValue } from './bson_value'; import { BSONError } from './error'; import type { EJSONOptions } from './extended_json'; -import { defaultInspect } from './parser/utils'; +import { type InspectFn, defaultInspect, getStylizeFunction } from './parser/utils'; function alphabetize(str: string): string { return str.split('').sort().join(''); @@ -104,27 +104,11 @@ export class BSONRegExp extends BSONValue { throw new BSONError(`Unexpected BSONRegExp EJSON object form: ${JSON.stringify(doc)}`); } - inspect(depth?: number, options?: unknown): string { - const stylize = getStylizeFunction(options); - const pattern = stylize(`'${this.pattern}'`, 'regexp'); - const flags = stylize(`'${this.options}'`, 'regexp'); + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { + const stylize = getStylizeFunction(options) ?? (v => v); + inspect ??= defaultInspect; + const pattern = stylize(inspect(this.pattern), 'regexp'); + const flags = stylize(inspect(this.options), 'regexp'); return `new BSONRegExp(${pattern}, ${flags})`; } } - -/** @internal */ -type StylizeFunction = (x: string, style: string) => string; -/** @internal */ -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 StylizeFunction; - } else { - return defaultInspect; - } -} diff --git a/src/symbol.ts b/src/symbol.ts index a6d778a6..6835ab95 100644 --- a/src/symbol.ts +++ b/src/symbol.ts @@ -1,5 +1,5 @@ import { BSONValue } from './bson_value'; -import { type InspectParameterFn, defaultInspect } from './parser/utils'; +import { type InspectFn, defaultInspect } from './parser/utils'; /** @public */ export interface BSONSymbolExtended { @@ -48,12 +48,8 @@ export class BSONSymbol extends BSONValue { return new BSONSymbol(doc.$symbol); } - inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { - const addQuotes = !inspect; + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { inspect ??= defaultInspect; - if (addQuotes) { - return `new BSONSymbol('${inspect(this.value, options)}')`; - } return `new BSONSymbol(${inspect(this.value, options)})`; } } diff --git a/src/timestamp.ts b/src/timestamp.ts index 5d8c8a8c..9d1e205c 100644 --- a/src/timestamp.ts +++ b/src/timestamp.ts @@ -1,7 +1,7 @@ import { BSONError } from './error'; import type { Int32 } from './int_32'; import { Long } from './long'; -import { type InspectParameterFn, defaultInspect } from './parser/utils'; +import { type InspectFn, defaultInspect } from './parser/utils'; /** @public */ export type TimestampOverrides = '_bsontype' | 'toExtendedJSON' | 'fromExtendedJSON' | 'inspect'; @@ -142,10 +142,10 @@ export class Timestamp extends LongWithoutOverridesClass { return new Timestamp({ t, i }); } - inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string { + inspect(depth?: number, options?: unknown, inspect?: InspectFn): string { inspect ??= defaultInspect; - const t = inspect(this.getHighBits(), options); - const i = inspect(this.getLowBits(), options); + const t = inspect(this.high >>> 0, options); + const i = inspect(this.low >>> 0, options); return `new Timestamp({ t: ${t}, i: ${i} })`; } } diff --git a/test/colors.mjs b/test/colors.mjs new file mode 100644 index 00000000..46ec1639 --- /dev/null +++ b/test/colors.mjs @@ -0,0 +1,60 @@ +/** Used to generate screenshots for introducing color to BSON inspect function */ + +'use strict'; + +import { + Binary, + UUID, + Code, + DBRef, + Decimal128, + Double, + Int32, + Long, + ObjectId, + BSONRegExp, + BSONSymbol, + Timestamp, + MaxKey, + MinKey +} from '../lib/bson.mjs'; + +console.log({ + binary: new Binary(Buffer.from('abcdef', 'utf8'), 0x06), + uuid: new UUID(), + code: new Code(function iLoveJavaScript() { + do { + console.log('hello!'); + } while (Math.random() > 0.5); + }), + c: new Code( + function iLoveJavaScript() { + do { + console.log('hello!'); + } while (Math.random() > 0.5); + }, + { context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}} + ), + c2: new Code ( + function iLoveJavaScript() { return `js`; }, + { context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}} + ), + dbref: new DBRef('collection', new ObjectId('00'.repeat(12))), + dbref_db: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db'), + dbref_db_fields: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db', { a: 1 }), + decimal128: new Decimal128('1.353e34'), + double: new Double(2.354), + double2: new Double(2), + double3: new Double(-0), + int32: new Int32('4577'), + long: new Long(-12442), + objectid: new ObjectId('00'.repeat(12)), + bsonregexp: new BSONRegExp('abc', 'imx'), + bsonsymbol: new BSONSymbol('my symbol'), + timestamp: new Timestamp({ i: 2345, t: 23453 }), + maxkey: new MaxKey(), + minkey: new MinKey() +}); + +const oid = new ObjectId('00'.repeat(12)); +console.log(oid); \ No newline at end of file diff --git a/test/node/binary.test.ts b/test/node/binary.test.ts index 09a4f972..1e0b2bd6 100644 --- a/test/node/binary.test.ts +++ b/test/node/binary.test.ts @@ -1,6 +1,7 @@ import { expect } from 'chai'; import * as vm from 'node:vm'; import { Binary, BSON } from '../register-bson'; +import * as util from 'node:util'; describe('class Binary', () => { context('constructor()', () => { @@ -82,25 +83,27 @@ describe('class Binary', () => { context('inspect()', () => { it(`when value is default returns "Binary.createFromBase64('', 0)"`, () => { - expect(new Binary().inspect()).to.equal(`Binary.createFromBase64('', 0)`); + expect(util.inspect(new Binary())).to.equal(`Binary.createFromBase64('', 0)`); }); it(`when value is empty returns "Binary.createFromBase64('', 0)"`, () => { - expect(new Binary(new Uint8Array(0)).inspect()).to.equal(`Binary.createFromBase64('', 0)`); + expect(util.inspect(new Binary(new Uint8Array(0)))).to.equal( + `Binary.createFromBase64('', 0)` + ); }); it(`when value is default with a subtype returns "Binary.createFromBase64('', 35)"`, () => { - expect(new Binary(undefined, 0x23).inspect()).to.equal(`Binary.createFromBase64('', 35)`); + expect(util.inspect(new Binary(null, 0x23))).to.equal(`Binary.createFromBase64('', 35)`); }); it(`when value is empty with a subtype returns "Binary.createFromBase64('', 35)"`, () => { - expect(new Binary(new Uint8Array(0), 0x23).inspect()).to.equal( + expect(util.inspect(new Binary(new Uint8Array(0), 0x23))).to.equal( `Binary.createFromBase64('', 35)` ); }); - it(`when value has utf8 "abcdef" encoded returns "Binary.createFromBase64('YWJjZGVm', 0)"`, () => { - expect(new Binary(Buffer.from('abcdef', 'utf8')).inspect()).to.equal( + it(`when value has utf8 "abcdef" encoded returns "Binary.createFromBase64("YWJjZGVm", 0)"`, () => { + expect(util.inspect(new Binary(Buffer.from('abcdef', 'utf8')))).to.equal( `Binary.createFromBase64('YWJjZGVm', 0)` ); }); diff --git a/test/types/bson.test-d.ts b/test/types/bson.test-d.ts index 3baf8b9d..6addcc5e 100644 --- a/test/types/bson.test-d.ts +++ b/test/types/bson.test-d.ts @@ -19,7 +19,7 @@ import { Decimal128Extended, BSONValue } from '../../bson'; // import from generated bson.d.ts -import type { InspectParameterFn } from '../../src/parser/utils'; +import type { InspectFn } from '../../src/parser/utils'; expectType<() => UUID>(Binary.prototype.toUUID); expectType<() => Binary>(UUID.prototype.toBinary); @@ -76,4 +76,4 @@ expectType<'Binary'>(UUID.prototype._bsontype) // Common BSONValue interface declare const bsonValue: BSONValue; expectType(bsonValue._bsontype); -expectType<(depth?: number | undefined, options?: unknown, inspect?: InspectParameterFn | undefined) => string>(bsonValue.inspect); +expectType<(depth?: number | undefined, options?: unknown, inspect?: InspectFn | undefined) => string>(bsonValue.inspect); From 4a4926cbbad3354e8e740679e081b819484a41ea Mon Sep 17 00:00:00 2001 From: aditi-khare-mongoDB <106987683+aditi-khare-mongoDB@users.noreply.github.com> Date: Fri, 13 Oct 2023 17:28:48 -0400 Subject: [PATCH 16/16] Delete test/colors.mjs --- test/colors.mjs | 60 ------------------------------------------------- 1 file changed, 60 deletions(-) delete mode 100644 test/colors.mjs diff --git a/test/colors.mjs b/test/colors.mjs deleted file mode 100644 index 46ec1639..00000000 --- a/test/colors.mjs +++ /dev/null @@ -1,60 +0,0 @@ -/** Used to generate screenshots for introducing color to BSON inspect function */ - -'use strict'; - -import { - Binary, - UUID, - Code, - DBRef, - Decimal128, - Double, - Int32, - Long, - ObjectId, - BSONRegExp, - BSONSymbol, - Timestamp, - MaxKey, - MinKey -} from '../lib/bson.mjs'; - -console.log({ - binary: new Binary(Buffer.from('abcdef', 'utf8'), 0x06), - uuid: new UUID(), - code: new Code(function iLoveJavaScript() { - do { - console.log('hello!'); - } while (Math.random() > 0.5); - }), - c: new Code( - function iLoveJavaScript() { - do { - console.log('hello!'); - } while (Math.random() > 0.5); - }, - { context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}} - ), - c2: new Code ( - function iLoveJavaScript() { return `js`; }, - { context: 'random looping!', reference: Long.fromString('2345'), my_map: {a:1}} - ), - dbref: new DBRef('collection', new ObjectId('00'.repeat(12))), - dbref_db: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db'), - dbref_db_fields: new DBRef('collection', new ObjectId('00'.repeat(12)), 'db', { a: 1 }), - decimal128: new Decimal128('1.353e34'), - double: new Double(2.354), - double2: new Double(2), - double3: new Double(-0), - int32: new Int32('4577'), - long: new Long(-12442), - objectid: new ObjectId('00'.repeat(12)), - bsonregexp: new BSONRegExp('abc', 'imx'), - bsonsymbol: new BSONSymbol('my symbol'), - timestamp: new Timestamp({ i: 2345, t: 23453 }), - maxkey: new MaxKey(), - minkey: new MinKey() -}); - -const oid = new ObjectId('00'.repeat(12)); -console.log(oid); \ No newline at end of file