Skip to content

Commit

Permalink
feat(algolia): refactor heading resolver
Browse files Browse the repository at this point in the history
* take levels 0 and 1
* add the least significant level of the rest, if possible and not similar to 1 or 0

closes #219
  • Loading branch information
almostSouji committed Oct 12, 2024
1 parent aca41c8 commit 4c7bc9f
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/functions/autocomplete/algoliaAutoComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,22 @@ function headingIsSimilar(one: string, other: string) {
export function resolveHitToNamestring(hit: AlgoliaHit) {
const { hierarchy } = hit;

const [lvl0, lvl1, lvl2, lvl3] = [hierarchy.lvl0, hierarchy.lvl1, hierarchy.lvl2, hierarchy.lvl3].map((heading) =>
removeDtypesPrefix(heading),
);
const [lvl0, lvl1, ...restLevels] = Object.values(hierarchy).map((heading) => removeDtypesPrefix(heading));

let value = headingIsSimilar(lvl0, lvl1) ? lvl1 : `${lvl0}${lvl1 ? `: ${lvl1}` : ''}`;
const headingParts = [];

if (lvl2.length) {
value += ` - ${lvl2}`;
if (headingIsSimilar(lvl0, lvl1)) {
headingParts.push(lvl1);
} else {
headingParts.push(`${lvl0}:`, lvl1);
}

if (lvl3.length) {
value += ` > ${lvl3}`;
const mostSpecific = restLevels.filter(Boolean).at(-1);
if (mostSpecific?.length && !headingIsSimilar(lvl0, mostSpecific) && !headingIsSimilar(lvl1, mostSpecific)) {
headingParts.push(`- ${mostSpecific}`);
}

return decode(value)!;
return decode(headingParts.join(' '))!;
}

function autoCompleteMap(elements: AlgoliaHit[]) {
Expand Down

0 comments on commit 4c7bc9f

Please sign in to comment.