Skip to content

Commit cb27e16

Browse files
committed
Refactor code-style
1 parent f367254 commit cb27e16

File tree

2 files changed

+80
-65
lines changed

2 files changed

+80
-65
lines changed

lib/index.js

+28-17
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
/**
2-
* @typedef {import('hast').Root} Root
3-
* @typedef {import('hast').Content} Content
2+
* @typedef {import('hast').Nodes} Nodes
43
*/
54

65
/**
7-
* @typedef {Root | Content} Node
8-
*
96
* @typedef Options
107
* Configuration
118
* @property {number | null | undefined} [age=16]
12-
* Target age group.
9+
* Target age group (default: `16`).
1310
*
1411
* This is the age your target audience was still in school.
1512
* Set it to 18 if you expect all readers to have finished high school,
1613
* 21 if you expect your readers to all be college graduates, etc.
1714
*/
1815

16+
// @ts-expect-error: untyped.
17+
import computeMedian_ from 'compute-median'
1918
import {toText} from 'hast-util-to-text'
2019
import readabilityScores from 'readability-scores'
21-
// @ts-expect-error: untyped.
22-
import median from 'compute-median'
20+
21+
const computeMedian = /** @type {(x: Array<number>) => number | null} */ (
22+
computeMedian_
23+
)
24+
25+
/** @type {Readonly<Options>} */
26+
const emptyOptions = {}
27+
28+
/** @type {Readonly<Record<string, never>>} */
29+
const emptyScores = {}
2330

2431
// See <https://en.wikipedia.org/wiki/Educational_stage#United_States>
2532
// for more info on US education/grade levels.
@@ -65,36 +72,40 @@ const accuracy = 1e6
6572
*
6673
* > ⚠️ **Important**: this algorithm is specific to English.
6774
*
68-
* @param {Node} tree
75+
* @param {Nodes} tree
6976
* Tree to inspect.
70-
* @param {Options | null | undefined} [options]
71-
* Configuration.
77+
* @param {Readonly<Options> | null | undefined} [options]
78+
* Configuration (optional).
7279
* @returns {number}
7380
* Estimated reading time in minutes.
7481
*
7582
* The result is not rounded so it’s possible to retrieve estimated seconds
7683
* from it.
7784
*/
7885
export function readingTime(tree, options) {
79-
const settings = options || {}
86+
const settings = options || emptyOptions
8087
// Cap an age to a reasonable and meaningful age in school.
8188
const targetAge = Math.min(
8289
graduationAge,
8390
Math.max(firstGradeAge, Math.round(settings.age || 16))
8491
)
8592
const text = toText(tree)
86-
const scores = readabilityScores(text) || {}
87-
const score = median(
93+
const scores = readabilityScores(text) || emptyScores
94+
const scoreNumbers = /** @type {Array<number>} */ (
8895
[
89-
scores.daleChall,
9096
scores.ari,
9197
scores.colemanLiau,
98+
scores.daleChall,
9299
scores.fleschKincaid,
93-
scores.smog,
94-
scores.gunningFog
95-
].filter((d) => d !== undefined)
100+
scores.gunningFog,
101+
scores.smog
102+
].filter(function (d) {
103+
return d !== undefined
104+
})
96105
)
97106

107+
const score = computeMedian(scoreNumbers)
108+
98109
if (score === null) {
99110
return 0
100111
}

test.js

+52-48
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import assert from 'node:assert/strict'
22
import test from 'node:test'
33
import {fromHtml} from 'hast-util-from-html'
44
import {readingTime} from './index.js'
5-
import * as mod from './index.js'
65

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

31-
test('readingTime', () => {
32-
assert.deepEqual(
33-
Object.keys(mod).sort(),
34-
['readingTime'],
35-
'should expose the public api'
36-
)
30+
test('readingTime', async function (t) {
31+
await t.test('should expose the public api', async function () {
32+
assert.deepEqual(Object.keys(await import('./index.js')).sort(), [
33+
'readingTime'
34+
])
35+
})
3736

38-
assert.deepEqual(
39-
readingTime(tree).toFixed(2),
40-
'1.22',
41-
'should estimate (somewhat complex)'
42-
)
37+
await t.test('should estimate (somewhat complex)', async function () {
38+
assert.deepEqual(readingTime(tree).toFixed(2), '1.22')
39+
})
4340

44-
assert.deepEqual(
45-
readingTime(treeSomewhatSimple).toFixed(2),
46-
'1.10',
47-
'should estimate (somewhat simple)'
48-
)
41+
await t.test('should estimate (somewhat simple)', async function () {
42+
assert.deepEqual(readingTime(treeSomewhatSimple).toFixed(2), '1.10')
43+
})
4944

50-
assert.deepEqual(
51-
readingTime({type: 'root', children: []}).toFixed(2),
52-
'0.00',
53-
'should estimate (empty)'
54-
)
45+
await t.test('should estimate (empty)', async function () {
46+
assert.deepEqual(
47+
readingTime({type: 'root', children: []}).toFixed(2),
48+
'0.00'
49+
)
50+
})
5551

56-
assert.deepEqual(
57-
readingTime(tree, {age: 12}).toFixed(2),
58-
'2.44',
59-
'should take age into account (1, somewhat complex)'
60-
)
61-
assert.deepEqual(
62-
readingTime(treeSomewhatSimple, {age: 12}).toFixed(2),
63-
'1.98',
64-
'should take age into account (1, somewhat simple)'
52+
await t.test(
53+
'should take age into account (1, somewhat complex)',
54+
async function () {
55+
assert.deepEqual(readingTime(tree, {age: 12}).toFixed(2), '2.44')
56+
}
6557
)
6658

67-
assert.deepEqual(
68-
readingTime(tree, {age: 21}).toFixed(2),
69-
'0.75',
70-
'should take age into account (2, somewhat complex)'
71-
)
72-
assert.deepEqual(
73-
readingTime(treeSomewhatSimple, {age: 21}).toFixed(2),
74-
'0.71',
75-
'should take age into account (2, somewhat simple)'
59+
await t.test(
60+
'should take age into account (1, somewhat simple)',
61+
async function () {
62+
assert.deepEqual(
63+
readingTime(treeSomewhatSimple, {age: 12}).toFixed(2),
64+
'1.98'
65+
)
66+
}
7667
)
7768

78-
assert.deepEqual(
79-
readingTime(tree, {age: 1}).toFixed(2),
80-
'4.46',
81-
'should cap at a reasonable time (1)'
69+
await t.test(
70+
'should take age into account (2, somewhat complex)',
71+
async function () {
72+
assert.deepEqual(readingTime(tree, {age: 21}).toFixed(2), '0.75')
73+
}
8274
)
8375

84-
assert.deepEqual(
85-
readingTime(tree, {age: 81}).toFixed(2),
86-
'0.70',
87-
'should cap at a reasonable time (2)'
76+
await t.test(
77+
'should take age into account (2, somewhat simple)',
78+
async function () {
79+
assert.deepEqual(
80+
readingTime(treeSomewhatSimple, {age: 21}).toFixed(2),
81+
'0.71'
82+
)
83+
}
8884
)
85+
86+
await t.test('should cap at a reasonable time (1)', async function () {
87+
assert.deepEqual(readingTime(tree, {age: 1}).toFixed(2), '4.46')
88+
})
89+
90+
await t.test('should cap at a reasonable time (2)', async function () {
91+
assert.deepEqual(readingTime(tree, {age: 81}).toFixed(2), '0.70')
92+
})
8993
})

0 commit comments

Comments
 (0)