How to map all quotation marks to double quote?
The problem:
The exact search had the issue with the quote. Based on the different clients the quotation marks can be different. The most common quotation marks are as follows:
const allQuotes = ['"', "'", '«', '»', '<<', '>>', '‘', '’', '‚', '‛', '“', '”', '„', '‟', '‹', '›', '<', '>'];
When the user is trying the exact search with a quotation mark, the query string can be different. It can be difficult to support all types of quotation marks for search.
We can very easily map all the quotation marks to a standard double quote.
The following is a function that will map all quotation marks to a standard double quote.
const getQuoteAsciiList = () => { const allQuotes = ['"', "'", '«', '»', '<<', '>>', '‘', '’', '‚', '‛', '“', '”', '„', '‟', '‹', '›', '<', '>']; return allQuotes.map((q) => q.charCodeAt()); }; const sanitizeQuery = (userQuery) => { const qry = decodeURI(userQuery); // Smallest quote search string length 3 if (qry.length < 3) { return userQuery; } const firstAscii = qry.slice(0, 1).charCodeAt(); const lastAscii = qry.slice(-1).charCodeAt(); const asciiList = getQuoteAsciiList(); if (asciiList.indexOf(firstAscii) > -1 && asciiList.indexOf(lastAscii) > -1) { const trimQuoteQry = qry.slice(1, -1); return encodeURI(`"${trimQuoteQry}"`); } return userQuery; };
https://unicodelookup.com/#quotation/1
https://op.europa.eu/en/web/eu-vocabularies/formex/physical-specifications/character-encoding/quotation-marks