Skip to content

Commit

Permalink
fix: search for bindi chars alongwith the normal characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauravjeetsingh committed Apr 17, 2024
1 parent 6c61bb9 commit 43d7a49
Showing 1 changed file with 61 additions and 3 deletions.
64 changes: 61 additions & 3 deletions api/lib/searchOperators.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const lodash = require('lodash');
const anvaad = require('anvaad-js');

// defining this as an object was the only way I could access
// AsteriskMariadbTranslation, and AsteriskAsciiValue in the firstLetterStartToQuery function..
const constantsObj = {
Expand All @@ -8,6 +10,27 @@ const constantsObj = {
DecSearchOperators: [43, 45, 42, 34, 39],
};

// Pairing the bindi characters with their non-bind counterparts
const bindiCharsUni = {
: 'ਸ਼',
: 'ਖ਼',
: 'ਗ਼',
: 'ਜ਼',
: 'ਫ਼',
};

// Generating an object with the ASCII codes of the bindi characters
const bindiCharacters = Object.entries(bindiCharsUni).reduce((acc, [key, value]) => {
const char = anvaad.unicode(key, true);
const asciiCode = anvaad.ascii(char).replaceAll(',', '');

const charWithBindi = anvaad.unicode(value, true);
const asciiCodeWithBindi = anvaad.ascii(charWithBindi).replaceAll(',', '');

acc[asciiCode] = asciiCodeWithBindi;
return acc;
}, {});

const replaceAsterisksAndQuotes = str => {
let res = str;

Expand Down Expand Up @@ -69,6 +92,38 @@ const getQueryConditionsAndParams = (
};
};

const hasBindiCharacter = charCode => {
if (bindiCharacters[charCode]) {
return bindiCharacters[charCode];
}
return false;
};

const generateBindiQuery = (charCodeQuery, charCodeQueryWildcard, result) => {
let bindiCharQuery = charCodeQuery;
let bindiCharQueryWildcard = charCodeQueryWildcard;
const updatedResult = result;

charCodeQuery.split(',').forEach(charCode => {
const bindiCharCode = hasBindiCharacter(charCode);
if (bindiCharCode) {
bindiCharQuery = bindiCharQuery.replaceAll(charCode, bindiCharCode);
bindiCharQueryWildcard = bindiCharQueryWildcard.replaceAll(charCode, bindiCharCode);
}
});

if (charCodeQuery !== bindiCharQuery) {
updatedResult.condition = `${updatedResult.condition} OR ${updatedResult.condition}`;
updatedResult.parameters = [
...updatedResult.parameters,
bindiCharQuery,
bindiCharQueryWildcard,
];
}

return updatedResult;
};

module.exports = {
AsteriskAsciiValue: constantsObj.AsteriskAsciiValue,
AsteriskMariadbTranslation: constantsObj.AsteriskMariadbTranslation,
Expand Down Expand Up @@ -100,10 +155,12 @@ module.exports = {
parameters: [modifiedSearchQuery],
};
}
return {
condition: 'v.FirstLetterStr BETWEEN ? AND ?',
const result = {
columns: ' LEFT JOIN tokenized_firstletters t ON t.verseid = v.ID',
condition: 't.token BETWEEN ? AND ?',
parameters: [charCodeQuery, charCodeQueryWildcard],
};
return generateBindiQuery(charCodeQuery, charCodeQueryWildcard, result);
},
firstLetterAnywhereToQuery: (charCodeQuery, charCodeQueryWildcard) => {
if (constantsObj.SearchOperators.some(operator => charCodeQuery.includes(operator))) {
Expand Down Expand Up @@ -132,11 +189,12 @@ module.exports = {
parameters: [modifiedSearchQuery],
};
}
return {
const result = {
columns: ' LEFT JOIN tokenized_firstletters t ON t.verseid = v.ID',
condition: 't.token BETWEEN ? AND ?',
parameters: [charCodeQuery, charCodeQueryWildcard],
};
return generateBindiQuery(charCodeQuery, charCodeQueryWildcard, result);
},
fullWordRomanizedToQuery: searchQuery => {
if (constantsObj.SearchOperators.some(operator => searchQuery.includes(operator))) {
Expand Down

0 comments on commit 43d7a49

Please sign in to comment.