From 233edccd57ce8fa5eca2339bdb99a7dc7979ac9f Mon Sep 17 00:00:00 2001 From: Rhys Date: Wed, 14 Feb 2024 09:33:31 -0500 Subject: [PATCH] feat(query-parser): allow falsy indentation, default to 2 space in toJSString, deprecate stringify (#235) --- packages/query-parser/src/index.spec.ts | 62 +++++++++++++++++++++++++ packages/query-parser/src/stringify.ts | 13 ++++-- 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/packages/query-parser/src/index.spec.ts b/packages/query-parser/src/index.spec.ts index 8de6d22f..a296cb87 100644 --- a/packages/query-parser/src/index.spec.ts +++ b/packages/query-parser/src/index.spec.ts @@ -16,6 +16,7 @@ import { parseProject, parseSort, stringify, + toJSString, DEFAULT_LIMIT, DEFAULT_MAX_TIME_MS, DEFAULT_SKIP, @@ -327,6 +328,53 @@ describe('mongodb-query-parser', function () { }); }); + describe('toJSString', function () { + it('should default to two spaces', function () { + assert.equal( + toJSString({ a: { $exists: true } }), + `{ + a: { + $exists: true + } +}` + ); + }); + + it('should allow falsy indentation', function () { + assert.equal( + toJSString({ a: { $exists: true } }, 0), + '{a:{$exists:true}}' + ); + }); + + it('allows passing custom indent', function () { + assert.equal( + toJSString({ a: { $exists: true } }, 'pineapple'), + `{ +pineapplea: { +pineapplepineapple$exists: true +pineapple} +}` + ); + }); + + it('retains double spaces and new lines in strings', function () { + assert.equal( + toJSString( + { + a: { + name: `multi-line with s p a c + +e s`, + }, + }, + 0 + ), + "{a:{name:'multi-line with s p a c\\n \\ne s'}}" + ); + }); + }); + describe('stringify', function () { it('should work', function () { const res = parseFilter('{_id: ObjectId("58c33a794d08b991e3648fd2")}'); @@ -337,6 +385,20 @@ describe('mongodb-query-parser', function () { assert.equal(stringify({ a: { $exists: true } }), '{a: {$exists: true}}'); }); + // stringify is now deprecated as a result of this. + it('changes multi-space values', function () { + assert.equal( + stringify({ + a: { + name: `multi-line with s p a c + +e s`, + }, + }), + "{a: {name: 'multi-line with s p a c\\n \\ne s'}}" + ); + }); + context('when providing a long', function () { it('correctly converts to NumberLong', function () { const stringified = stringify({ test: bson.Long.fromNumber(5) }); diff --git a/packages/query-parser/src/stringify.ts b/packages/query-parser/src/stringify.ts index 7d5bb4cb..32d5023c 100644 --- a/packages/query-parser/src/stringify.ts +++ b/packages/query-parser/src/stringify.ts @@ -139,7 +139,7 @@ const BSON_TO_JS_STRING = { /** @public */ export function toJSString( obj: unknown, - ind?: Parameters[2] + ind: Parameters[2] = 2 ): string | undefined { return toJavascriptString( obj, @@ -151,13 +151,18 @@ export function toJSString( } return toJs(value); }, - ind || ' ' + ind ); } -/** @public */ +/** + * @public + * @deprecated + * This function is deprecated and not recommended as it replaces + * double spaces, newline values, and indents with only one space. + **/ export function stringify(obj: unknown): string | undefined { - return toJSString(obj) + return toJSString(obj, 1) ?.replace(/ ?\n ? ?/g, '') .replace(/ {2,}/g, ' '); }