Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-5040): add color to BSON inspect #635

Merged
merged 17 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)}')`;
Expand Down
23 changes: 8 additions & 15 deletions src/db_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')})`;
}
Expand Down
2 changes: 1 addition & 1 deletion src/decimal128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/objectid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}')`;
Expand Down
5 changes: 0 additions & 5 deletions src/regexp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion src/symbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}')`;
Expand Down
58 changes: 58 additions & 0 deletions test/colors.mjs
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'use strict';
W-A-James marked this conversation as resolved.
Show resolved Hide resolved

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);
61 changes: 52 additions & 9 deletions test/node/bson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
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 () {
nbbeeken marked this conversation as resolved.
Show resolved Hide resolved
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 () {
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
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 () {
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
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 () {
W-A-James marked this conversation as resolved.
Show resolved Hide resolved
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' } })`
);
});
});

/**
Expand Down