Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,38 @@ describe('lenientlyFixQuery [Utils]', function () {
expect(query).to.equal('\\{ query: 1 ${}}');
});
});

describe('when an ObjectId is pasted alone', function () {
it('returns a fix action with _id: ObjectId("<id>")', function () {
const query = lenientlyFixQuery('{578cfb38d5021e616087f53f}');
expect(query).to.equal(
'\\{ _id: ObjectId("578cfb38d5021e616087f53f") ${}}'
);
});
});

describe('when a valid ObjectId query is pasted', function () {
it('returns a no-op with the existing query', function () {
const query = lenientlyFixQuery(
'{ _id: ObjectId("507f1f77bcf86cd799439011") }'
);

expect(query).to.equal(false);
});
});

describe('when an invalid ObjectId is pasted', function () {
it('returns a no-op with the existing query', function () {
const queryWith25Chars = lenientlyFixQuery('{578cfb38d5021e616087f53f1}');
expect(queryWith25Chars).to.equal(false);

const queryWithObjectIdNoBraces = lenientlyFixQuery(
'578cfb38d5021e616087f53f1'
);
expect(queryWithObjectIdNoBraces).to.equal(false);

const pineappleQuery = lenientlyFixQuery('{pineapple}');
expect(pineappleQuery).to.equal(false);
});
});
});
76 changes: 50 additions & 26 deletions packages/compass-query-bar/src/query/leniently-fix-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,67 @@ function _isValidQuery(query: string): boolean {
}
}

// Add _id: ObjectId("<id>") when a user pastes only an object id.
function _fixObjectIdInQuery(query: string): string | undefined {
const objectIdRegex = /^{([0-9a-fA-F]{24})}$/;

const match = query.match(objectIdRegex);
if (match) {
return `{ _id: ObjectId("${match[1]}") }`;
}
}

function _fixBraceEscapingInQuery(query: string): string | undefined {
const isValid = _isValidQuery(query);

if (isValid) {
return;
}

if (query.startsWith('{') && query.endsWith('}')) {
const queryWithoutWrappingBraces = query.substring(1, query.length - 1);
const isInnerQueryValid = _isValidQuery(queryWithoutWrappingBraces);
if (isInnerQueryValid) {
return queryWithoutWrappingBraces;
}
} else {
const wrappedQuery = `{${query}}`;
if (_isValidQuery(wrappedQuery)) {
return wrappedQuery;
}
}
}

export function lenientlyFixQuery(query: string): string | false {
query = query.trim();
let modified = false;

if (query === '') {
return '\\{${}}';
}

const isValid = _isValidQuery(query);
let modified = false;

if (!isValid) {
if (query.startsWith('{') && query.endsWith('}')) {
const queryWithoutWrappingBraces = query.substring(1, query.length - 1);
const isInnerQueryValid = _isValidQuery(queryWithoutWrappingBraces);
if (isInnerQueryValid) {
modified = true;
query = queryWithoutWrappingBraces;
}
} else {
const wrappedQuery = `{${query}}`;
if (_isValidQuery(wrappedQuery)) {
modified = true;
query = wrappedQuery;
}
}
const fixedObjectId = _fixObjectIdInQuery(query);
if (fixedObjectId) {
modified = true;
query = fixedObjectId;
}

if (modified) {
query = query.replaceAll('{', '\\{');
const caretPosition = query.lastIndexOf('}');
const fixedBraceEscaping = _fixBraceEscapingInQuery(query);
if (fixedBraceEscaping) {
modified = true;
query = fixedBraceEscaping;
}

query =
query.substring(0, caretPosition) +
'${}' +
query.substring(caretPosition);
return query;
if (!modified) {
return false;
}

return false;
// Add template formatting to put the cursor position before the last closing brace.
query = query.replaceAll('{', '\\{');
const caretPosition = query.lastIndexOf('}');

query =
query.substring(0, caretPosition) + '${}' + query.substring(caretPosition);
return query;
}
Loading