From 2104890f31d48edaf33bb613281808abfbe7890d Mon Sep 17 00:00:00 2001 From: Sampo Silvennoinen Date: Sat, 16 Nov 2024 10:56:38 +0200 Subject: [PATCH] Add random entries to frontpage Closes #392 --- package.json | 2 +- .../{WordLink.test.js => WordLink.test.tsx} | 0 ...nk.test.js.snap => WordLink.test.tsx.snap} | 0 .../WordLink/{index.js => index.tsx} | 7 +- .../{WordList.test.js => WordList.test.tsx} | 0 ...st.test.js.snap => WordList.test.tsx.snap} | 0 src/components/WordList/index.js | 14 - src/components/WordList/index.tsx | 24 + src/lib/services/dictionary.ts | 26 +- src/pages/index.tsx | 13 +- .../pages/__snapshots__/index.test.tsx.snap | 1807 +++++++++++++++++ tests/unit/pages/index.test.tsx | 9 +- yarn.lock | 8 +- 13 files changed, 1883 insertions(+), 27 deletions(-) rename src/components/WordLink/{WordLink.test.js => WordLink.test.tsx} (100%) rename src/components/WordLink/__snapshots__/{WordLink.test.js.snap => WordLink.test.tsx.snap} (100%) rename src/components/WordLink/{index.js => index.tsx} (59%) rename src/components/WordList/{WordList.test.js => WordList.test.tsx} (100%) rename src/components/WordList/__snapshots__/{WordList.test.js.snap => WordList.test.tsx.snap} (100%) delete mode 100644 src/components/WordList/index.js create mode 100644 src/components/WordList/index.tsx diff --git a/package.json b/package.json index f269e23..6f637af 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "jest": "^29.7.0", "jest-environment-jsdom": "^29.7.0", "node-mocks-http": "^1.16.0", - "old-norse-alphabet-sort": "^1.1.12", + "old-norse-alphabet-sort": "^1.1.13", "react-test-renderer": "^18.3.1", "spyrjari": "^1.0.4", "typescript": "^5.6.2", diff --git a/src/components/WordLink/WordLink.test.js b/src/components/WordLink/WordLink.test.tsx similarity index 100% rename from src/components/WordLink/WordLink.test.js rename to src/components/WordLink/WordLink.test.tsx diff --git a/src/components/WordLink/__snapshots__/WordLink.test.js.snap b/src/components/WordLink/__snapshots__/WordLink.test.tsx.snap similarity index 100% rename from src/components/WordLink/__snapshots__/WordLink.test.js.snap rename to src/components/WordLink/__snapshots__/WordLink.test.tsx.snap diff --git a/src/components/WordLink/index.js b/src/components/WordLink/index.tsx similarity index 59% rename from src/components/WordLink/index.js rename to src/components/WordLink/index.tsx index 26086dd..e9bb7d0 100644 --- a/src/components/WordLink/index.js +++ b/src/components/WordLink/index.tsx @@ -1,7 +1,12 @@ import Link from 'next/link' +import type { DictionaryEntry } from 'lib/services/dictionary' import styles from './WordLink.module.scss' -export default function WordLink({ data }) { +interface WordLinkProps{ + data: DictionaryEntry, +} + +export default function WordLink({ data }: WordLinkProps) { const { slug, word } = data return ( diff --git a/src/components/WordList/WordList.test.js b/src/components/WordList/WordList.test.tsx similarity index 100% rename from src/components/WordList/WordList.test.js rename to src/components/WordList/WordList.test.tsx diff --git a/src/components/WordList/__snapshots__/WordList.test.js.snap b/src/components/WordList/__snapshots__/WordList.test.tsx.snap similarity index 100% rename from src/components/WordList/__snapshots__/WordList.test.js.snap rename to src/components/WordList/__snapshots__/WordList.test.tsx.snap diff --git a/src/components/WordList/index.js b/src/components/WordList/index.js deleted file mode 100644 index c3bc10e..0000000 --- a/src/components/WordList/index.js +++ /dev/null @@ -1,14 +0,0 @@ -import WordLink from 'components/WordLink' -import styles from './WordList.module.scss' - -export default function WordList({ words }) { - return ( - - ) -} diff --git a/src/components/WordList/index.tsx b/src/components/WordList/index.tsx new file mode 100644 index 0000000..0d7b6d2 --- /dev/null +++ b/src/components/WordList/index.tsx @@ -0,0 +1,24 @@ +import WordLink from 'components/WordLink' +import type { DictionaryEntry, DictionaryEntryDTO } from 'lib/services/dictionary' +import { hasProperty } from 'spyrjari' +import styles from './WordList.module.scss' + +interface WordListProps { + words: DictionaryEntryDTO[] | DictionaryEntry[], + showDefinition?: boolean +} + +export default function WordList({ words, showDefinition = false }: WordListProps) { + return ( + + ) +} diff --git a/src/lib/services/dictionary.ts b/src/lib/services/dictionary.ts index f6d4594..e2d9372 100644 --- a/src/lib/services/dictionary.ts +++ b/src/lib/services/dictionary.ts @@ -1,5 +1,6 @@ -import { getDictionary } from 'old-icelandic-zoega' -import { DictionaryEntry as RawDictionaryEntry } from 'cleasby-vigfusson-dictionary' +import { getDictionary, getNoMarkupDictionary } from 'old-icelandic-zoega' +import { oldNorseSort } from 'old-norse-alphabet-sort' +import type { DictionaryEntry as RawDictionaryEntry } from 'cleasby-vigfusson-dictionary' import { VALID_AS_FIRST } from 'old-norse-alphabet' import { slugifyWord, slugifyLetter } from '../utils/slugs' @@ -59,6 +60,17 @@ export const getAllWords = (): DictionaryEntry[] => { return formattedWords } +export const getAllWordsWithoutMarkup = (): DictionaryEntry[] => { + const words = getNoMarkupDictionary() + + /** + * Add URL safe slugs. + */ + const formattedWords = addSlugs(words) + + return formattedWords +} + export const getByLetter = (letter: string): DictionaryEntryDTO[] => { const words = getAllWords() const byLetter = words @@ -73,6 +85,16 @@ export const getWord = (slug: string): DictionaryEntry => ( getAllWords().filter((entry) => entry.slug === slug)[0] ) +export const getRandomEntries = (): DictionaryEntry[] => ( + // Return entries fit to be randomized "teasers" + // Therefore, content should be short, but not too short. + getAllWordsWithoutMarkup() + .sort(() => Math.random() - 0.5) + .filter((entry) => entry.definitions[0].length < 50 && entry.definitions[0].length > 15) + .slice(0, 36) + .sort((a, b) => oldNorseSort(a.word, b.word)) +) + export const getAlphabet = (): AlphabetLetter[] => { const letters = [...VALID_AS_FIRST.filter((letter) => letter !== 'ǫ' && letter !== 'ø'), 'ö'] diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 02c4854..2064227 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -1,14 +1,18 @@ // Services. import Link from 'next/link' -import { AlphabetLetter, getAlphabet } from 'lib/services/dictionary' +import { + type AlphabetLetter, type DictionaryEntry, getAlphabet, getRandomEntries, +} from 'lib/services/dictionary' // Components. import Layout from 'components/Layout' import ContentArea from 'components/ContentArea' import SampleText from 'components/SampleText' +import WordList from 'components/WordList' interface IndexProps { letters: AlphabetLetter[] + words: DictionaryEntry[], } interface IndexStaticProps { @@ -17,15 +21,17 @@ interface IndexStaticProps { export async function getStaticProps(): Promise { const letters = getAlphabet() + const randomEntries = getRandomEntries() return { props: { letters, + words: randomEntries, }, } } -export default function Index({ letters }: IndexProps) { +export default function Index({ letters, words }: IndexProps) { return ( @@ -51,6 +57,9 @@ export default function Index({ letters }: IndexProps) { + +

Random entries from the dictionary:

+
) } diff --git a/tests/unit/pages/__snapshots__/index.test.tsx.snap b/tests/unit/pages/__snapshots__/index.test.tsx.snap index 371c5df..8e7ccba 100644 --- a/tests/unit/pages/__snapshots__/index.test.tsx.snap +++ b/tests/unit/pages/__snapshots__/index.test.tsx.snap @@ -1111,6 +1111,1813 @@ exports[`Index page Matches snapshot 1`] = `


+

+ Random entries from the dictionary: +

+
{ test('Does not crash', () => { const div = document.createElement('div') const root = ReactDOM.createRoot(div) - root.render() + root.render() }) test('Matches snapshot', () => { - const tree = renderer.create().toJSON() + const tree = renderer.create( + , + ).toJSON() expect(tree).toMatchSnapshot() }) }) @@ -22,6 +24,7 @@ describe('Index page: data fetching', () => { const expected = { props: { letters: [], + words: [], }, } diff --git a/yarn.lock b/yarn.lock index 4ddb119..8eeebde 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5403,10 +5403,10 @@ old-icelandic-zoega@^1.2.5: resolved "https://registry.yarnpkg.com/old-icelandic-zoega/-/old-icelandic-zoega-1.2.5.tgz#9c26b8dd4a0d8e577802ecabcb22c33c7569c2ed" integrity sha512-OFzoVV96tsdy8u4luy/pt+N0pWPv4DG84RB7uPdIHknkQ+gE+HOB4L7Nr0nSstK3KklKPCceMLzZ2OKG6NFfyg== -old-norse-alphabet-sort@^1.1.12: - version "1.1.12" - resolved "https://registry.yarnpkg.com/old-norse-alphabet-sort/-/old-norse-alphabet-sort-1.1.12.tgz#0901bf235e69eee18985904bc9e80a8a1720e4d2" - integrity sha512-LYgdTyBrHGEv45Db+xl5vQQbK5rZA+fS/9kEQOkOB0lkoFP1M1cvjjitolfxvWBEeYfjq2/6o7lvZl8VSD8Hfw== +old-norse-alphabet-sort@^1.1.13: + version "1.1.13" + resolved "https://registry.yarnpkg.com/old-norse-alphabet-sort/-/old-norse-alphabet-sort-1.1.13.tgz#9c929f6b782eeae09594abde1eddabf77719c55f" + integrity sha512-D14S4PDOKRL7eVXuQqQkCav+7AUKns6XHygcK2NR3NWNdiDfrpUgdSycWRbdPwpy1Zvdr1K2O0w2+Epv4BIFcQ== dependencies: old-norse-alphabet "^1.1.2"