Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions src/readability-integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {describe, it, expect, vi} from 'vitest';

describe('readability (integration)', () => {
it('should not change scores for semantic line breaks vs normal wrapping', async () => {
// Ensure we use the real library (other tests mock this module).
vi.unmock('text-readability');

const {calculateReadabilityOfText} = await import('./readability');

const original = `
At ExampleDocs, your safety is our top priority.
That's why we require Know Your Customer (KYC) verification.
KYC protects your account from fraud and identity theft.
`;

// Same content, different line breaks (semantic line breaks / wrapping).
const semantic = `
At ExampleDocs,
your safety is our top priority.
That's why we require Know Your Customer (KYC) verification.
KYC protects your account from fraud and identity theft.
`;

const a = calculateReadabilityOfText(original);
const b = calculateReadabilityOfText(semantic);

const metrics = [
'readabilityScore',
'fleschReadingEase',
'gunningFog',
'automatedReadabilityIndex',
'daleChallReadabilityScore',
'colemanLiauIndex',
] as const;

for (const metric of metrics) {
expect(a[metric]).toBeCloseTo(b[metric], 9);
}
});
});
17 changes: 17 additions & 0 deletions src/readability.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,23 @@ Here is the sixth paragraph text.
});

describe('scoring', () => {
it('should not let semantic line breaks affect the scored input', () => {
vi.clearAllMocks();

calculateReadabilityOfText(
`
This is a sentence,
that continues on the next line without ending.
`,
);

const firstArg =
vi.mocked(readability.fleschReadingEase).mock.calls[0]?.[0] ?? '';

expect(firstArg).not.toContain('\n');
expect(firstArg).toContain('This is a sentence, that continues');
});

it('should return minimum values for empty text', () => {
expect(calculateReadabilityOfText('')).toMatchInlineSnapshot(`
{
Expand Down
5 changes: 4 additions & 1 deletion src/readability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,10 @@ export function calculateReadabilityOfText(text: string): ReadabilityScores {
};

const stripped = preprocessMarkdown(String(text));
const scores = scoreText(stripped);
// Normalize whitespace so formatting changes (like semantic line breaks)
// do not affect readability metrics.
const normalizedForScoring = stripped.replace(/\s+/g, ' ').trim();
const scores = scoreText(normalizedForScoring);
const normalized = normalizeScores(scores);
const readabilityScore = calculateReadabilityScore(normalized);

Expand Down