diff --git a/packages/compass-query-bar/src/query/leniently-fix-query.spec.ts b/packages/compass-query-bar/src/query/leniently-fix-query.spec.ts index bbee092ee77..0d4e2d59d7c 100644 --- a/packages/compass-query-bar/src/query/leniently-fix-query.spec.ts +++ b/packages/compass-query-bar/src/query/leniently-fix-query.spec.ts @@ -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("")', 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); + }); + }); }); diff --git a/packages/compass-query-bar/src/query/leniently-fix-query.ts b/packages/compass-query-bar/src/query/leniently-fix-query.ts index 50f6abdd228..3b98423b74e 100644 --- a/packages/compass-query-bar/src/query/leniently-fix-query.ts +++ b/packages/compass-query-bar/src/query/leniently-fix-query.ts @@ -8,43 +8,67 @@ function _isValidQuery(query: string): boolean { } } +// Add _id: ObjectId("") 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; }