Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 8, 2023
1 parent f367254 commit cb27e16
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 65 deletions.
45 changes: 28 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
/**
* @typedef {import('hast').Root} Root
* @typedef {import('hast').Content} Content
* @typedef {import('hast').Nodes} Nodes
*/

/**
* @typedef {Root | Content} Node
*
* @typedef Options
* Configuration
* @property {number | null | undefined} [age=16]
* Target age group.
* Target age group (default: `16`).
*
* This is the age your target audience was still in school.
* Set it to 18 if you expect all readers to have finished high school,
* 21 if you expect your readers to all be college graduates, etc.
*/

// @ts-expect-error: untyped.
import computeMedian_ from 'compute-median'
import {toText} from 'hast-util-to-text'
import readabilityScores from 'readability-scores'
// @ts-expect-error: untyped.
import median from 'compute-median'

const computeMedian = /** @type {(x: Array<number>) => number | null} */ (
computeMedian_
)

/** @type {Readonly<Options>} */
const emptyOptions = {}

/** @type {Readonly<Record<string, never>>} */
const emptyScores = {}

// See <https://en.wikipedia.org/wiki/Educational_stage#United_States>
// for more info on US education/grade levels.
Expand Down Expand Up @@ -65,36 +72,40 @@ const accuracy = 1e6
*
* > ⚠️ **Important**: this algorithm is specific to English.
*
* @param {Node} tree
* @param {Nodes} tree
* Tree to inspect.
* @param {Options | null | undefined} [options]
* Configuration.
* @param {Readonly<Options> | null | undefined} [options]
* Configuration (optional).
* @returns {number}
* Estimated reading time in minutes.
*
* The result is not rounded so it’s possible to retrieve estimated seconds
* from it.
*/
export function readingTime(tree, options) {
const settings = options || {}
const settings = options || emptyOptions
// Cap an age to a reasonable and meaningful age in school.
const targetAge = Math.min(
graduationAge,
Math.max(firstGradeAge, Math.round(settings.age || 16))
)
const text = toText(tree)
const scores = readabilityScores(text) || {}
const score = median(
const scores = readabilityScores(text) || emptyScores
const scoreNumbers = /** @type {Array<number>} */ (
[
scores.daleChall,
scores.ari,
scores.colemanLiau,
scores.daleChall,
scores.fleschKincaid,
scores.smog,
scores.gunningFog
].filter((d) => d !== undefined)
scores.gunningFog,
scores.smog
].filter(function (d) {
return d !== undefined
})
)

const score = computeMedian(scoreNumbers)

if (score === null) {
return 0
}
Expand Down
100 changes: 52 additions & 48 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import assert from 'node:assert/strict'
import test from 'node:test'
import {fromHtml} from 'hast-util-from-html'
import {readingTime} from './index.js'
import * as mod from './index.js'

// https://simple.wikipedia.org/wiki/Reading
const somewhatSimple = `<p>Reading is what we do when we understand writing.</p>
Expand All @@ -28,62 +27,67 @@ const somewhatComplex = `<p>Since the length or duration of words is clearly var
const tree = fromHtml(somewhatComplex, {fragment: true})
const treeSomewhatSimple = fromHtml(somewhatSimple, {fragment: true})

test('readingTime', () => {
assert.deepEqual(
Object.keys(mod).sort(),
['readingTime'],
'should expose the public api'
)
test('readingTime', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('./index.js')).sort(), [
'readingTime'
])
})

assert.deepEqual(
readingTime(tree).toFixed(2),
'1.22',
'should estimate (somewhat complex)'
)
await t.test('should estimate (somewhat complex)', async function () {
assert.deepEqual(readingTime(tree).toFixed(2), '1.22')
})

assert.deepEqual(
readingTime(treeSomewhatSimple).toFixed(2),
'1.10',
'should estimate (somewhat simple)'
)
await t.test('should estimate (somewhat simple)', async function () {
assert.deepEqual(readingTime(treeSomewhatSimple).toFixed(2), '1.10')
})

assert.deepEqual(
readingTime({type: 'root', children: []}).toFixed(2),
'0.00',
'should estimate (empty)'
)
await t.test('should estimate (empty)', async function () {
assert.deepEqual(
readingTime({type: 'root', children: []}).toFixed(2),
'0.00'
)
})

assert.deepEqual(
readingTime(tree, {age: 12}).toFixed(2),
'2.44',
'should take age into account (1, somewhat complex)'
)
assert.deepEqual(
readingTime(treeSomewhatSimple, {age: 12}).toFixed(2),
'1.98',
'should take age into account (1, somewhat simple)'
await t.test(
'should take age into account (1, somewhat complex)',
async function () {
assert.deepEqual(readingTime(tree, {age: 12}).toFixed(2), '2.44')
}
)

assert.deepEqual(
readingTime(tree, {age: 21}).toFixed(2),
'0.75',
'should take age into account (2, somewhat complex)'
)
assert.deepEqual(
readingTime(treeSomewhatSimple, {age: 21}).toFixed(2),
'0.71',
'should take age into account (2, somewhat simple)'
await t.test(
'should take age into account (1, somewhat simple)',
async function () {
assert.deepEqual(
readingTime(treeSomewhatSimple, {age: 12}).toFixed(2),
'1.98'
)
}
)

assert.deepEqual(
readingTime(tree, {age: 1}).toFixed(2),
'4.46',
'should cap at a reasonable time (1)'
await t.test(
'should take age into account (2, somewhat complex)',
async function () {
assert.deepEqual(readingTime(tree, {age: 21}).toFixed(2), '0.75')
}
)

assert.deepEqual(
readingTime(tree, {age: 81}).toFixed(2),
'0.70',
'should cap at a reasonable time (2)'
await t.test(
'should take age into account (2, somewhat simple)',
async function () {
assert.deepEqual(
readingTime(treeSomewhatSimple, {age: 21}).toFixed(2),
'0.71'
)
}
)

await t.test('should cap at a reasonable time (1)', async function () {
assert.deepEqual(readingTime(tree, {age: 1}).toFixed(2), '4.46')
})

await t.test('should cap at a reasonable time (2)', async function () {
assert.deepEqual(readingTime(tree, {age: 81}).toFixed(2), '0.70')
})
})

0 comments on commit cb27e16

Please sign in to comment.