diff --git a/src/code.ts b/src/code.ts index 48889f56..1fb46ab8 100644 --- a/src/code.ts +++ b/src/code.ts @@ -62,7 +62,7 @@ export class Code extends BSONValue { inspect(): string { const codeJson = this.toJSON(); - return `new Code("${String(codeJson.code)}"${ + return `new Code(${JSON.stringify(String(codeJson.code))}${ codeJson.scope != null ? `, ${JSON.stringify(codeJson.scope)}` : '' })`; } diff --git a/src/symbol.ts b/src/symbol.ts index 081a52ad..079cabea 100644 --- a/src/symbol.ts +++ b/src/symbol.ts @@ -34,7 +34,7 @@ export class BSONSymbol extends BSONValue { } inspect(): string { - return `new BSONSymbol("${this.value}")`; + return `new BSONSymbol(${JSON.stringify(this.value)})`; } toJSON(): string { diff --git a/test/node/code.test.ts b/test/node/code.test.ts index e8afba9f..4e28541b 100644 --- a/test/node/code.test.ts +++ b/test/node/code.test.ts @@ -1,5 +1,6 @@ import { expect } from 'chai'; import * as BSON from '../register-bson'; +import { inspect } from 'util'; describe('class Code', () => { it('defines a nodejs inspect method', () => { @@ -8,6 +9,13 @@ describe('class Code', () => { .that.is.a('function'); }); + it('prints re-evaluatable output for Code that contains quotes', () => { + const codeStringInput = new BSON.Code('function a(){ return "asdf"; }'); + expect(inspect(codeStringInput)).to.equal( + String.raw`new Code("function a(){ return \"asdf\"; }")` + ); + }); + describe('new Code()', () => { it('defines a code property that is a string', () => { const codeStringInput = new BSON.Code('function a(){}'); diff --git a/test/node/symbol.test.ts b/test/node/symbol.test.ts index c63ad859..f26ab3f3 100644 --- a/test/node/symbol.test.ts +++ b/test/node/symbol.test.ts @@ -1,6 +1,7 @@ import { expect } from 'chai'; import { BSONSymbol, BSON } from '../register-bson'; import { bufferFromHexArray } from './tools/utils'; +import { inspect } from 'util'; describe('class BSONSymbol', () => { it('get _bsontype returns BSONSymbol', () => { @@ -41,4 +42,9 @@ describe('class BSONSymbol', () => { const result = BSON.deserialize(bytes, { promoteValues: false }); expect(result).to.have.nested.property('sym._bsontype', '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")`); + }); });