Skip to content

Commit

Permalink
feat(query-parser): allow falsy indentation, default to 2 space in to…
Browse files Browse the repository at this point in the history
…JSString, deprecate stringify (#235)
  • Loading branch information
Anemy authored Feb 14, 2024
1 parent 1b717dd commit 233edcc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
62 changes: 62 additions & 0 deletions packages/query-parser/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
parseProject,
parseSort,
stringify,
toJSString,
DEFAULT_LIMIT,
DEFAULT_MAX_TIME_MS,
DEFAULT_SKIP,
Expand Down Expand Up @@ -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")}');
Expand All @@ -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) });
Expand Down
13 changes: 9 additions & 4 deletions packages/query-parser/src/stringify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const BSON_TO_JS_STRING = {
/** @public */
export function toJSString(
obj: unknown,
ind?: Parameters<typeof JSON.stringify>[2]
ind: Parameters<typeof JSON.stringify>[2] = 2
): string | undefined {
return toJavascriptString(
obj,
Expand All @@ -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, ' ');
}

0 comments on commit 233edcc

Please sign in to comment.