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(query-parser): allow falsy indentation, default to 2 space in toJSString, deprecate stringify #235

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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, ' ');
}
Loading