forked from wagtail/wagtail
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add language-specific reading time calculation
- Loading branch information
Showing
3 changed files
with
70 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,35 @@ | ||
import { getContentMetrics } from './contentMetrics'; | ||
import { getWordCount, getReadingTime } from './contentMetrics'; | ||
|
||
describe('getContentMetrics', () => { | ||
it('should return correct wordCount and readingTime using Intl.Segmenter', () => { | ||
const result = getContentMetrics('en-US', 'This is a test sentence.'); | ||
expect(result.wordCount).toBe(5); | ||
expect(result.readingTime).toBe(0); | ||
}); | ||
|
||
it('should handle empty text', () => { | ||
const result = getContentMetrics('en-US', ''); | ||
expect(result.wordCount).toBe(0); | ||
expect(result.readingTime).toBe(0); | ||
}); | ||
|
||
it('should handle text with punctuation correctly', () => { | ||
const text = `This is a longer text to test the word count and reading time calculation! | ||
Bread is a staple food prepared from a dough of flour and water; usually by baking. | ||
Throughout recorded history it has been popular around the world and is one of the | ||
oldest artificial foods: having been of importance since the dawn of agriculture. | ||
Proportions of types of flour and other ingredients vary widely? as do modes of preparation. | ||
As a result, types, shapes, sizes, and textures of breads differ around the world. | ||
Bread may be leavened by processes such as reliance on naturally occurring sourdough | ||
microbes, chemicals, industrially produced yeast, or high-pressure aeration... | ||
Some breads are baked before they have a chance to rise, often for traditional | ||
or religious reasons. Inclusions like fruits; nuts, and fats are sometimes added. | ||
Commercial bread typically includes additives to enhance flavor, texture, color, | ||
longevity, and production efficiency! | ||
`; | ||
const result = getContentMetrics('en-US', text); | ||
expect(result.wordCount).toBe(148); | ||
expect(result.readingTime).toBe(1); | ||
describe.each` | ||
text | lang | wordCount | ||
${'¿Donde esta la biblioteca?'} | ${'es'} | ${4} | ||
${"It's lots. Of; Punctuation"} | ${'en'} | ${4} | ||
${'האהבה היא אוקיינוס שאין לו התחלה ואין לו סוף.'} | ${'he'} | ${9} | ||
${'元気です、ありがとう。あなたは?'} | ${'zh'} | ${5} | ||
${'Dit is een testzin in het Nederlands.'} | ${'nl'} | ${7} | ||
${'Je suis content de te voir!'} | ${'fr'} | ${6} | ||
${'Ich liebe dich!'} | ${'de'} | ${3} | ||
${'Mi piace molto questo libro.'} | ${'it'} | ${5} | ||
${'저는 오늘 날씨가 좋아요.'} | ${'ko'} | ${4} | ||
`('getWordCount', ({ text, lang, wordCount }) => { | ||
test(`correctly counts words in '${text}' for language '${lang}'`, () => { | ||
expect(getWordCount(lang, text)).toBe(wordCount); | ||
}); | ||
}); | ||
|
||
it('should return integers for wordCount and readingTime', () => { | ||
const text = 'Yet another text'; | ||
const result = getContentMetrics('en-US', text); | ||
expect(Number.isInteger(result.wordCount)).toBe(true); | ||
expect(Number.isInteger(result.readingTime)).toBe(true); | ||
describe.each` | ||
lang | wordCount | readingTime | ||
${'es'} | ${1000} | ${4} | ||
${'fr'} | ${1000} | ${5} | ||
${'ar'} | ${360} | ${2} | ||
${'it'} | ${360} | ${1} | ||
${'en'} | ${238} | ${1} | ||
${'he'} | ${224} | ${1} | ||
${'zh'} | ${520} | ${2} | ||
${'nl'} | ${320} | ${1} | ||
${'ko'} | ${50} | ${0} | ||
`('getReadingTime', ({ lang, wordCount, readingTime }) => { | ||
test(`calculates reading time for '${wordCount}' words in language '${lang}'`, () => { | ||
expect(getReadingTime(lang, wordCount)).toBe(readingTime); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters