From d9f83906720b9263d38be2f87db9398fbdc6dac1 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Thu, 6 Jun 2019 15:41:30 -0600 Subject: [PATCH] Put homonymn number on public dictionary entries --- src/js/view/render.js | 6 ++++-- src/js/view/utilities.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/js/view/render.js b/src/js/view/render.js index 4e8da83..b34b8e1 100644 --- a/src/js/view/render.js +++ b/src/js/view/render.js @@ -1,6 +1,6 @@ import md from 'marked'; import { removeTags, slugify } from '../../helpers'; -import { getWordsStats } from './utilities'; +import { getWordsStats, getHomonymnNumber } from './utilities'; import { getMatchingSearchWords, highlightSearchTerm, getSearchFilters, getSearchTerm } from './search'; import { showSection } from './displayToggles'; import { setupSearchFilters, setupInfoModal } from './setupListeners'; @@ -155,13 +155,15 @@ export function renderWords() { details: parseReferences(removeTags(originalWord.details)), wordId: originalWord.wordId, }); + + const homonymnNumber = getHomonymnNumber(originalWord); const shareLink = window.location.pathname + (window.location.pathname.match(new RegExp(word.wordId + '$')) ? '' : '/' + word.wordId); wordsHTML += renderAd(displayIndex); wordsHTML += `
-

${word.name}

+

${word.name}${homonymnNumber > 0 ? ' ' + homonymnNumber.toString() + '' : ''}

${word.pronunciation} ${word.partOfSpeech} diff --git a/src/js/view/utilities.js b/src/js/view/utilities.js index 3753346..f9f09dd 100644 --- a/src/js/view/utilities.js +++ b/src/js/view/utilities.js @@ -100,3 +100,19 @@ export function getHomonymnIndexes(word) { }); return foundIndexes; } + +export function getHomonymnNumber(word) { + const homonyms = getHomonymnIndexes(word); + if (homonyms.length > 0) { + const index = window.currentDictionary.words.findIndex(w => w.wordId === word.wordId); + let number = 1; + + for (let i = 0; i < homonyms.length; i++) { + if (index < homonyms[i]) break; + number++; + } + + return number; + } + return 0; +}