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 (
-
- { words.map((word) => (
- -
-
-
- )) }
-
- )
-}
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 (
+
+ { words.map((word) => (
+ -
+
+ {showDefinition && hasProperty(word, 'definitions') && }
+
+ )) }
+
+ )
+}
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`] = `
+ -
+
+ a
+
+
not;",
+ }
+ }
+ />
+
+ -
+
+ abbadis
+
+
abbess.",
+ }
+ }
+ />
+
+ -
+
+ abbast
+
+
to be angry, to quarrel (~ við e-n, upp á e-n).",
+ }
+ }
+ />
+
+ -
+
+ abbindi
+
+
constipation.",
+ }
+ }
+ />
+
+ -
+
+ af
+
+
+
+ -
+
+ afar
+
+
very, exceedingly (~ auðigr, ~ breiðr, ~ vel, ~ illa).",
+ }
+ }
+ />
+
+ -
+
+ afarkostalaust
+
+
on fair terms.",
+ }
+ }
+ />
+
+ -
+
+ afarkostr
+
+
hard terms.",
+ }
+ }
+ />
+
+ -
+
+ afarligr
+
+
immense, huge.",
+ }
+ }
+ />
+
+ -
+
+ afarmenni
+
+
an outstanding man.",
+ }
+ }
+ />
+
+ -
+
+ afarorð
+
+
overbearing word.",
+ }
+ }
+ />
+
+ -
+
+ afaryrði
+
+
proud speech.",
+ }
+ }
+ />
+
+ -
+
+ afarúðigr
+
+
overbearing.",
+ }
+ }
+ />
+
+ -
+
+ afauðit
+
+
one has bad luck (fails) in a thing.",
+ }
+ }
+ />
+
+ -
+
+ afbindi
+
+
constipation.",
+ }
+ }
+ />
+
+ -
+
+ afblómga
+
+
to deprive of flowers; fig., ~ frægð e-s, to detract from one’s fame.",
+ }
+ }
+ />
+
+ -
+
+ afboð
+
+
threats, high words.",
+ }
+ }
+ />
+
+ -
+
+ afbragð
+
+
paragon (~ annarra manna); in genitive as a prefix to nouns, surpassing, excellent (~s vænleikr, ~s maðr).",
+ }
+ }
+ />
+
+ -
+
+ afbragðligr
+
+
surpassing.",
+ }
+ }
+ />
+
+ -
+
+ afbrigð
+
+
+
+ -
+
+ afbrigðartré
+
+
tree of transgression.",
+ }
+ }
+ />
+
+ -
+
+ afbrigði
+
+
+
+ -
+
+ afbrot
+
+
offence, transgressi on.",
+ }
+ }
+ />
+
+ -
+
+ afbrugðning
+
+
deviation.",
+ }
+ }
+ />
+
+ -
+
+ afbrúðigr
+
+
jealous.",
+ }
+ }
+ />
+
+ -
+
+ afbrýða
+
+
to be jealous.",
+ }
+ }
+ />
+
+ -
+
+ afbrýði
+
+
jealousy.",
+ }
+ }
+ />
+
+ -
+
+ afburðar
+
+
very, exceedingly (~ digr, ~ vænn, ~ sterkliga, ~ vel).",
+ }
+ }
+ />
+
+ -
+
+ afburðarmaðr
+
+
man of mark.",
+ }
+ }
+ />
+
+ -
+
+ afburðarsamr
+
+
given to distinguish oneself.",
+ }
+ }
+ />
+
+ -
+
+ afburðarskip
+
+
first-rate ship.",
+ }
+ }
+ />
+
+ -
+
+ afburðr
+
+
superiority; kvað honum eigi annat vænna til afburðar, to get the better of it; ok vilda ek, at hón yrði eigi með minnum afburðum, less glorious.",
+ }
+ }
+ />
+
+ -
+
+ afdalr
+
+
a remote, an out-of-the-way, valley.",
+ }
+ }
+ />
+
+ -
+
+ afdeilingr
+
+
part, portion, share.",
+ }
+ }
+ />
+
+ -
+
+ afdrif
+
+
destiny, fate (líkligr til stórra ~a).",
+ }
+ }
+ />
+
+ -
+
+ afdrykkja
+
+
+
+ -
+
+ afdráttr
+
+
+
+ -
+
+ afdœma
+
+
+
+ -
+
+ afeggja
+
+
to dissuade.",
+ }
+ }
+ />
+
+ -
+
+ afeigna
+
+
to dispossess (~ e-m e-t).",
+ }
+ }
+ />
+
+ -
+
+ afeista
+
+
to castrate.",
+ }
+ }
+ />
+
+ -
+
+ aferfa
+
+
to disinherit.",
+ }
+ }
+ />
+
+ -
+
+ afeyringr
+
+
one whose ears have been cut off.",
+ }
+ }
+ />
+
+ -
+
+ affall
+
+
diminution, abatement; selja e-t með afföllum, to sell at a discount.",
+ }
+ }
+ />
+
+ -
+
+ affara-
+
+
departure; hence affaradagr, affarakveld.",
+ }
+ }
+ />
+
+ -
+
+ affaradagr
+
+
the last day of a feast; ~ jóla, Twelth Night.",
+ }
+ }
+ />
+
+ -
+
+ affarakveld
+
+
the last evening of a feast.",
+ }
+ }
+ />
+
+ -
+
+ afferma
+
+
to unload (~ skip).",
+ }
+ }
+ />
+
+ -
+
+ affeðrast
+
+
to degenerate.",
+ }
+ }
+ />
+
+ -
+
+ affletta
+
+
to strip (~ e-n e-u).",
+ }
+ }
+ />
+
+ -
+
+ afflutningr
+
+
+
+ -
+
+ afflytja
+
+
+
+ -
+
+ affœra
+
+
to misrepresent.",
+ }
+ }
+ />
+
+ -
+
+ afgamall
+
+
very old, decrepit.",
+ }
+ }
+ />
+
+ -
+
+ afganga
+
+
+
+ -
+
+ afgangr
+
+
+
+ -
+
+ afgelja
+
+
chattering.",
+ }
+ }
+ />
+
+ -
+
+ afgipt
+
+
indulgence, absolution.",
+ }
+ }
+ />
+
+ -
+
+ afgiptarbréf
+
+
letter of indulgence.",
+ }
+ }
+ />
+
+ -
+
+ afgjarn
+
+
eager to be off (~t er öfundarfé).",
+ }
+ }
+ />
+
+ -
+
+ afgjöf
+
+
tribute.",
+ }
+ }
+ />
+
+ -
+
+ afglapa
+
+
to disturb an assembly or public meeting.",
+ }
+ }
+ />
+
+ -
+
+ afglapan
+
+
disturbance (þings ~).",
+ }
+ }
+ />
+
+ -
+
+ afglapi
+
+
fool, simpleton.",
+ }
+ }
+ />
+
+ -
+
+ afgreizla
+
+
payment, contribution.",
+ }
+ }
+ />
+
+ -
+
+ afgöngudagr
+
+
+
+ -
+
+ afgøra
+
+
to do amiss, do wrong (ek hefi engan hlut afgørt við þik).",
+ }
+ }
+ />
+
+ -
+
+ afgørð
+
+
transgression, offence.",
+ }
+ }
+ />
+
+ -
+
+ afhallr
+
+
having a downward slope.",
+ }
+ }
+ />
+
+ -
+
+ afhaugr
+
+
side-mound.",
+ }
+ }
+ />
+
+ -
+
+ afhefð
+
+
withholding, anothet’s property.",
+ }
+ }
+ />
+
+ -
+
+ afhelgast
+
+
to become unholy, to be profaned.",
+ }
+ }
+ />
+
+ -
+
+ afhellir
+
+
side-cave.",
+ }
+ }
+ />
+
+ -
+
+ afhenda
+
+
+
+ -
+
+ afhendis
+
+
off one’s hand.",
+ }
+ }
+ />
+
+ -
+
+ afhendr
+
+
out of one’s hand; segja e-n (sér) afhendan, to give one up.",
+ }
+ }
+ />
+
+ -
+
+ afhent
+
+
unfit (e-m er e-t ~).",
+ }
+ }
+ />
+
+ -
+
+ afheyrandi
+
+
out of hearing; absent, opp. to áheyrandi.",
+ }
+ }
+ />
+
+ -
+
+ afheyris
+
+
out of hearing, opp. to áheyris.",
+ }
+ }
+ />
+
+ -
+
+ afhlaup
+
+
surplus.",
+ }
+ }
+ />
+
+ -
+
+ afhlutr
+
+
share of a thing.",
+ }
+ }
+ />
+
+ -
+
+ afhlýðast
+
+
to disobey.",
+ }
+ }
+ />
+
+ -
+
+ afhrapi
+
+
impoverishment.",
+ }
+ }
+ />
+
+ -
+
+ afhroð
+
+
damage, loss; gjalda mikit ~, to sustain a heavy loss; gera mikit ~, to make great havoc.",
+ }
+ }
+ />
+
+ -
+
+ afhuga
+
+
having turned one’s mind from; verða ~ e-u, to mind no more.",
+ }
+ }
+ />
+
+ -
+
+ afhugast
+
+
to put out of one’s mind, with dat.",
+ }
+ }
+ />
+
+ -
+
+ afhugaðr
+
+
+
+ -
+
+ afhvarf
+
+
deviation from the direct path (opp. to gagnvegr).",
+ }
+ }
+ />
+
+ -
+
+ afhylja
+
+
to uncover.",
+ }
+ }
+ />
+
+ -
+
+ afhyrning
+
+
by-corner, recess.",
+ }
+ }
+ />
+
+ -
+
+ afhæra
+
+
to cut off the hair.",
+ }
+ }
+ />
+
+ -
+
+ afhöfða
+
+
to behead.",
+ }
+ }
+ />
+
+ -
+
+ afhögg
+
+
hewing off, mutilation; chippings.",
+ }
+ }
+ />
+
+ -
+
+ afhús
+
+
outhouse, side-apartinent.",
+ }
+ }
+ />
+
+ -
+
+ afhýða
+
+
to scourge thoroughly.",
+ }
+ }
+ />
+
+ -
+
+ afi
+
+
+
+ -
+
+ afkaup
+
+
bad bargain (opp. to hagkeypi).",
+ }
+ }
+ />
+
+ -
+
+ afkleyfisorð
+
+
a superfluous word, syllable (in a verse).",
+ }
+ }
+ />
+
+ -
+
+ afkleyfissamstafa
+
+
a superfluous word, syllable (in a verse).",
+ }
+ }
+ />
+
+ -
+
+ afklæða
+
+
to undress;",
+ }
+ }
+ />
+
+
{
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"