Skip to content

Commit 59eda94

Browse files
PR requested changes 1
1 parent a14ae92 commit 59eda94

File tree

8 files changed

+123
-34
lines changed

8 files changed

+123
-34
lines changed

src/binary.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ export class Binary extends BSONValue {
269269
}
270270

271271
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
272-
const addQuotes = inspect ? false : true;
272+
const addQuotes = !inspect;
273273
inspect ??= basicInspectParameterFn;
274274
const base64 = ByteUtils.toBase64(this.buffer.subarray(0, this.position));
275275
const base64Arg = inspect(base64, options);
@@ -473,7 +473,7 @@ export class UUID extends Binary {
473473
*
474474
*/
475475
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
476-
const addQuotes = inspect ? false : true;
476+
const addQuotes = !inspect;
477477
inspect ??= basicInspectParameterFn;
478478
if (addQuotes) {
479479
return `new UUID('${inspect(this.toHexString(), options)}')`;

src/db_ref.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,17 @@ export class DBRef extends BSONValue {
113113
}
114114

115115
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
116-
const addQuotes = inspect ? false : true;
116+
const addQuotes = !inspect;
117117
inspect ??= basicInspectParameterFn;
118118

119-
const namespaceArg = addQuotes
120-
? `'${inspect(this.namespace, options)}'`
121-
: inspect(this.namespace, options);
122-
const objectIDArg = addQuotes
123-
? `new ObjectId('${inspect(this.oid, options)}')`
124-
: inspect(this.oid, options);
125-
const args = [namespaceArg, objectIDArg];
119+
const args = [
120+
inspect(this.namespace, options),
121+
inspect(this.oid, options),
122+
...(this.db ? [inspect(this.db, options)] : []),
123+
...(Object.keys(this.fields).length > 0 ? [inspect(this.fields, options)] : [])
124+
].map(arg => addQuotes ? `'${arg}'` : arg);
126125

127-
if (this.db) {
128-
args.push(addQuotes ? `'${inspect(this.db, options)}'` : inspect(this.db, options));
129-
}
130-
131-
if (Object.keys(this.fields).length > 0) {
132-
args.push(inspect(this.fields, options));
133-
}
126+
args[1] = addQuotes ? `new ObjectId(${args[1]})` : args[1];
134127

135128
return `new DBRef(${args.join(', ')})`;
136129
}

src/decimal128.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ export class Decimal128 extends BSONValue {
848848
}
849849

850850
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
851-
const addQuotes = inspect ? false : true;
851+
const addQuotes = !inspect;
852852
inspect ??= basicInspectParameterFn;
853853
const d128string = inspect(this.toString(), options);
854854
if (addQuotes) {

src/objectid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ export class ObjectId extends BSONValue {
297297
* @returns return the 24 character hex string representation.
298298
*/
299299
inspect(depth?: number, options?: unknown, inspect?: InspectParameterFn): string {
300-
const addQuotes = inspect ? false : true;
300+
const addQuotes = !inspect;
301301
inspect ??= basicInspectParameterFn;
302302
if (addQuotes) {
303303
return `new ObjectId('${inspect(this.toHexString(), options)}')`;

src/regexp.ts

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

107-
/** @internal */
108-
[Symbol.for('nodejs.util.inspect.custom')](depth?: number, options?: unknown): string {
109-
return this.inspect(depth, options);
110-
}
111-
112107
inspect(depth?: number, options?: unknown): string {
113108
const stylize = getStylizeFunction(options);
114109
const pattern = stylize(`'${this.pattern}'`, 'regexp');

src/symbol.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class BSONSymbol extends BSONValue {
5858
}
5959

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

test/colors.mjs

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

test/node/bson_test.js

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,20 +1841,63 @@ describe('BSON', function () {
18411841
/**
18421842
* @ignore
18431843
*/
1844-
it('Code', function () {
1845-
const code = new Code('this.a > i', { i: 1 });
1846-
expect(inspect(code)).to.equal(`new Code('this.a > i', { i: 1 })`);
1844+
context('Code', function () {
1845+
it('when non-nested fields', function () {
1846+
const code = new Code('this.a > i', { i: 1 });
1847+
expect(inspect(code)).to.equal(`new Code('this.a > i', { i: 1 })`);
1848+
});
1849+
it('when non-nested fields', function () {
1850+
const code = new Code('this.a > i', { a: 1, b: { nest: 'mine' } });
1851+
expect(inspect(code)).to.equal(`new Code('this.a > i', { a: 1, b: { nest: 'mine' } })`);
1852+
});
1853+
it('when multiline code', function () {
1854+
const code = new Code(
1855+
function iLoveJavaScript() {
1856+
do {
1857+
console.log('hello!');
1858+
} while (Math.random() > 0.5);
1859+
},
1860+
{ context: 'random looping!', reference: Long.fromString('2345'), my_map: { a: 1 } }
1861+
);
1862+
expect(inspect(code)).to.equal(
1863+
/* eslint-disable */
1864+
`new Code(
1865+
'function iLoveJavaScript() {\\n' +
1866+
' do {\\n' +
1867+
" console.log('hello!');\\n" +
1868+
' } while (Math.random() > 0.5);\\n' +
1869+
' }',
1870+
{
1871+
context: 'random looping!',
1872+
reference: new Long('2345'),
1873+
my_map: { a: 1 }
1874+
})`
1875+
/* eslint-enable */
1876+
);
1877+
});
18471878
});
18481879

18491880
/**
18501881
* @ignore
18511882
*/
1852-
it('DBRef', function () {
1853-
const oid = new ObjectId('deadbeefdeadbeefdeadbeef');
1854-
const dbref = new DBRef('namespace', oid, 'integration_tests_');
1855-
expect(inspect(dbref)).to.equal(
1856-
`new DBRef('namespace', new ObjectId('deadbeefdeadbeefdeadbeef'), 'integration_tests_')`
1857-
);
1883+
context('DBRef', function () {
1884+
it('when non-nested fields', function () {
1885+
const oid = new ObjectId('deadbeefdeadbeefdeadbeef');
1886+
const dbref = new DBRef('namespace', oid, 'integration_tests_');
1887+
expect(inspect(dbref)).to.equal(
1888+
`new DBRef('namespace', new ObjectId('deadbeefdeadbeefdeadbeef'), 'integration_tests_')`
1889+
);
1890+
});
1891+
it('when nested fields', function () {
1892+
const oid = new ObjectId('deadbeefdeadbeefdeadbeef');
1893+
const dbref = new DBRef('namespace', oid, 'integration_tests_', {
1894+
a: 1,
1895+
b: { nest: 'mine' }
1896+
});
1897+
expect(inspect(dbref)).to.equal(
1898+
`new DBRef('namespace', new ObjectId('deadbeefdeadbeefdeadbeef'), 'integration_tests_', { a: 1, b: { nest: 'mine' } })`
1899+
);
1900+
});
18581901
});
18591902

18601903
/**

0 commit comments

Comments
 (0)