From b21a8f88955f392f8561a7a6af0e4fa79fc78579 Mon Sep 17 00:00:00 2001 From: billlyzhaoyh Date: Thu, 26 Nov 2026 01:29:19 +0000 Subject: [PATCH 1/2] fix(core): edits keep untouched words' timing and vocabulary original editSentence, editParagraph and replaceAll rebuilt the whole sentence with respread, so any edit (even opening an editor and leaving it unchanged) moved every word's timing and dropped `original`. Find & Replace of one word did the same to its whole sentence. They now align the old and new word lists (longest common subsequence, exact text) and keep each untouched word as it was. A run of new words that replaces old ones takes those words' time; a run inserted between kept words takes the gap between them. With nothing kept, the text spreads across the span as before. An edited vocabulary word still drops `original`; that choice belongs with the vocabulary underline step. Closes #28 --- core/src/editor.ts | 165 +++++++++++++++++++++++++---- core/test/edit-keeps-words.test.ts | 89 ++++++++++++++++ 2 files changed, 234 insertions(+), 20 deletions(-) create mode 100644 core/test/edit-keeps-words.test.ts diff --git a/core/src/editor.ts b/core/src/editor.ts index 94345d4..d750cb5 100644 --- a/core/src/editor.ts +++ b/core/src/editor.ts @@ -79,6 +79,25 @@ function tokenize(text: string): string[] { .filter(Boolean); } +/** Spread `span` seconds from `start` across `tokens` by character length. */ +function spread( + start: number, + span: number, + tokens: string[], + speaker: SpeakerId, +): EditorWord[] { + const total = tokens.join("").length || 1; + let acc = 0; + const rebuilt: EditorWord[] = []; + for (const tok of tokens) { + const st = start + span * (acc / total); + acc += tok.length; + const en = start + span * (acc / total); + rebuilt.push({ text: tok, start: st, end: en, speaker }); + } + return rebuilt; +} + /** * Spread `[spanStart, spanEnd]` across `tokens` by character length, all assigned * `speaker`. A degenerate span (end <= start) becomes 0.3s per token so timings stay @@ -94,19 +113,116 @@ function respread( spanEnd - spanStart > 0 ? spanEnd - spanStart : Math.max(0.3 * tokens.length, 0.3); - const total = tokens.join("").length || 1; - let acc = 0; - const rebuilt: EditorWord[] = []; - for (const tok of tokens) { - const st = spanStart + span * (acc / total); - acc += tok.length; - const en = spanStart + span * (acc / total); - rebuilt.push({ text: tok, start: st, end: en, speaker }); + return spread(spanStart, span, tokens, speaker); +} + +/** + * `[oldIndex, newIndex]` pairs for the words an edit kept: a longest common + * subsequence of the two word lists, compared by exact text. + */ +function keptPairs(oldTexts: string[], newTexts: string[]): Array<[number, number]> { + // Most edits touch a few words, so match the shared prefix and suffix directly. + let head = 0; + while ( + head < oldTexts.length && + head < newTexts.length && + oldTexts[head] === newTexts[head] + ) { + head++; } - return rebuilt; + let tail = 0; + while ( + tail < oldTexts.length - head && + tail < newTexts.length - head && + oldTexts[oldTexts.length - 1 - tail] === newTexts[newTexts.length - 1 - tail] + ) { + tail++; + } + + const a = oldTexts.slice(head, oldTexts.length - tail); + const b = newTexts.slice(head, newTexts.length - tail); + // lcs[i][j]: length of the longest common subsequence of a[i..] and b[j..]. + const lcs = Array.from({ length: a.length + 1 }, () => new Int32Array(b.length + 1)); + for (let i = a.length - 1; i >= 0; i--) { + for (let j = b.length - 1; j >= 0; j--) { + lcs[i]![j] = + a[i] === b[j] ? lcs[i + 1]![j + 1]! + 1 : Math.max(lcs[i + 1]![j]!, lcs[i]![j + 1]!); + } + } + + const pairs: Array<[number, number]> = []; + for (let k = 0; k < head; k++) pairs.push([k, k]); + let i = 0; + let j = 0; + while (i < a.length && j < b.length) { + if (a[i] === b[j]) { + pairs.push([head + i, head + j]); + i++; + j++; + } else if (lcs[i + 1]![j]! >= lcs[i]![j + 1]!) { + i++; + } else { + j++; + } + } + for (let k = tail; k > 0; k--) { + pairs.push([oldTexts.length - k, newTexts.length - k]); + } + return pairs; +} + +/** + * The words for edited text `tokens` that replaces `old`, all assigned `speaker`. + * Words the edit kept stay as they were: their timing and `original`. A run of new + * words that replaces old words takes those words' time; a run inserted between kept + * words takes the gap between them (no duration if there is none). With nothing kept, + * the new words spread across `[spanStart, spanEnd]`. + */ +function retime( + old: EditorWord[], + tokens: string[], + spanStart: number, + spanEnd: number, + speaker: SpeakerId, +): EditorWord[] { + const pairs = keptPairs( + old.map((w) => w.text), + tokens, + ); + if (pairs.length === 0) return respread(spanStart, spanEnd, tokens, speaker); + + const words: EditorWord[] = []; + let oldAt = 0; + let newAt = 0; + // A sentinel pair past both ends flushes the run after the last kept word. + for (const [keptOld, keptNew] of [...pairs, [old.length, tokens.length] as [number, number]]) { + const added = tokens.slice(newAt, keptNew); + if (added.length > 0) { + const replaced = keptOld > oldAt; + const from = replaced + ? old[oldAt]!.start + : oldAt > 0 + ? old[oldAt - 1]!.end + : spanStart; + const to = replaced + ? old[keptOld - 1]!.end + : keptOld < old.length + ? old[keptOld]!.start + : spanEnd; + words.push(...spread(from, Math.max(to - from, 0), added, speaker)); + } + if (keptOld < old.length) words.push({ ...old[keptOld]!, speaker }); + oldAt = keptOld + 1; + newAt = keptNew + 1; + } + return words; } -/** Replace a sentence's text, keeping its original time span. Blank text deletes the sentence. */ +/** + * Replace a sentence's text. Words the edit didn't touch keep their timing and + * `original`; changed and inserted words are timed from their neighbours (see + * `retime`). Blank text deletes the sentence. + */ export function editSentence( state: EditorState, sentenceId: string, @@ -129,7 +245,7 @@ export function editSentence( return next; } - sentence.words = respread(s, e, toks, speaker); + sentence.words = retime(orig, toks, s, e, speaker); return next; } @@ -152,11 +268,13 @@ function contiguousRun( } /** - * Paragraph-scope edit. Re-tokenize `newText` and re-spread it across the run's ANCHOR - * span — the first listed sentence's first-word `start` to the last listed sentence's - * last-word `end` — then collapse the whole contiguous run to ONE sentence whose id is - * the FIRST sentence's id (so highlight/edit ids stay stable) and whose speaker is the - * first word's speaker. Blank/whitespace `newText` deletes the entire run. + * Paragraph-scope edit. Re-tokenize `newText` and time it against the run's words + * (see `retime`): untouched words keep their timing and `original`, and with nothing + * kept the text spreads across the run's ANCHOR span — the first listed sentence's + * first-word `start` to the last listed sentence's last-word `end`. The whole + * contiguous run collapses to ONE sentence whose id is the FIRST sentence's id (so + * highlight/edit ids stay stable) and whose speaker is the first word's speaker. + * Blank/whitespace `newText` deletes the entire run. * * Non-contiguous, unknown or empty ids return the state unchanged. */ @@ -199,7 +317,13 @@ export function editParagraph( const collapsed: EditorSentence = { id: runSentences[0]!.id, - words: respread(spanStart, spanEnd, toks, speaker), + words: retime( + runSentences.flatMap((s) => s.words), + toks, + spanStart, + spanEnd, + speaker, + ), }; next.sentences.splice(startIdx, run.length, collapsed); return next; @@ -219,8 +343,9 @@ function escapeRegExp(s: string): string { /** * Literal, per-sentence find/replace (a multi-word `find` matches across word gaps). - * `replace` is inserted verbatim (no `$`-substitution). A changed sentence keeps its - * time span; a replacement that would empty a sentence is skipped. Empty `find` is a + * `replace` is inserted verbatim (no `$`-substitution). In a changed sentence only the + * replaced words are re-timed (see `retime`); a replacement that would empty a sentence + * is skipped. Empty `find` is a * no-op. Returns the new state and the number of matches replaced. */ export function replaceAll( @@ -258,7 +383,7 @@ export function replaceAll( const s = orig.length > 0 ? orig[0]!.start : 0; const e = orig.length > 0 ? orig[orig.length - 1]!.end : 0; const speaker = orig.length > 0 ? orig[0]!.speaker : UNKNOWN_SPEAKER; - sentence.words = respread(s, e, toks, speaker); + sentence.words = retime(orig, toks, s, e, speaker); count += matches; } diff --git a/core/test/edit-keeps-words.test.ts b/core/test/edit-keeps-words.test.ts new file mode 100644 index 0000000..0a543d1 --- /dev/null +++ b/core/test/edit-keeps-words.test.ts @@ -0,0 +1,89 @@ +// Issue #28: an edit re-times only the words it changes; untouched words keep their +// native timing and the record of what the model wrote. +import { describe, it, expect } from "vitest"; + +import { loadTranscript, editSentence, replaceAll } from "../src/editor.js"; +import type { Transcript } from "../src/types.js"; + +// "I met Shivaun on Tuesday." where the vocabulary file turned "Siobhan" into +// "Shivaun", and a long pause comes before "Tuesday.". +function makeTranscript(): Transcript { + return { + segments: [ + { + start: 0.0, + end: 4.6, + text: "I met Shivaun on Tuesday.", + speaker: "SPEAKER_00", + words: [ + { word: "I", start: 0.0, end: 0.2, speaker: "SPEAKER_00" }, + { word: "met", start: 0.3, end: 0.6, speaker: "SPEAKER_00" }, + { + word: "Shivaun", + start: 0.7, + end: 1.4, + speaker: "SPEAKER_00", + original: "Siobhan", + }, + { word: "on", start: 1.5, end: 1.6, speaker: "SPEAKER_00" }, + { word: "Tuesday.", start: 4.0, end: 4.6, speaker: "SPEAKER_00" }, + ], + }, + ], + }; +} + +const I = { text: "I", start: 0.0, end: 0.2, speaker: "SPEAKER_00" }; +const MET = { text: "met", start: 0.3, end: 0.6, speaker: "SPEAKER_00" }; +const SHIVAUN = { + text: "Shivaun", + start: 0.7, + end: 1.4, + speaker: "SPEAKER_00", + original: "Siobhan", +}; +const ON = { text: "on", start: 1.5, end: 1.6, speaker: "SPEAKER_00" }; +const TUESDAY = { text: "Tuesday.", start: 4.0, end: 4.6, speaker: "SPEAKER_00" }; + +describe("edits keep untouched words", () => { + it("Find & Replace of one word leaves every other word's timing and original", () => { + const state = loadTranscript(makeTranscript()); + const { state: next, count } = replaceAll(state, "Tuesday", "Wednesday"); + + expect(count).toBe(1); + const words = next.sentences[0]!.words; + expect(words.map((w) => w.text)).toEqual(["I", "met", "Shivaun", "on", "Wednesday."]); + expect(words.slice(0, 4)).toEqual([I, MET, SHIVAUN, ON]); + }); + + it("editSentence that changes one word re-times only that word, within its neighbours' gap", () => { + const state = loadTranscript(makeTranscript()); + const sid = state.sentences[0]!.id; + const words = editSentence(state, sid, "I met Siobhán on Tuesday.").sentences[0]!.words; + + expect(words.map((w) => w.text)).toEqual(["I", "met", "Siobhán", "on", "Tuesday."]); + expect(words[0]).toEqual(I); + expect(words[1]).toEqual(MET); + expect(words[3]).toEqual(ON); + expect(words[4]).toEqual(TUESDAY); + // "met" ends at 0.6 and "on" starts at 1.5. + expect(words[2]!.start).toBeGreaterThanOrEqual(0.6); + expect(words[2]!.end).toBeLessThanOrEqual(1.5); + expect(words[2]!.end).toBeGreaterThanOrEqual(words[2]!.start); + }); + + it("an edit that inserts a word places it between its neighbours' times", () => { + const state = loadTranscript(makeTranscript()); + const sid = state.sentences[0]!.id; + const words = editSentence(state, sid, "I met Shivaun again on Tuesday.").sentences[0]! + .words; + + expect(words.map((w) => w.text)).toEqual(["I", "met", "Shivaun", "again", "on", "Tuesday."]); + expect(words[2]).toEqual(SHIVAUN); + expect(words[4]).toEqual(ON); + // "Shivaun" ends at 1.4 and "on" starts at 1.5. + expect(words[3]!.start).toBeGreaterThanOrEqual(1.4); + expect(words[3]!.end).toBeLessThanOrEqual(1.5); + expect(words[3]!.end).toBeGreaterThanOrEqual(words[3]!.start); + }); +}); From d86ef603e171e4e7b3c30100e00e608d6da7ef59 Mon Sep 17 00:00:00 2001 From: billlyzhaoyh Date: Thu, 26 Nov 2026 01:45:12 +0000 Subject: [PATCH 2/2] refactor(core): simplify the #28 retime and stop re-timing on the live match count From /simplify on #36; behaviour is unchanged (20,000 random edits compared against the previous commit came out identical). - retime(old, tokens) works out the span and speaker from `old`, so editParagraph loses its two span loops and editSentence becomes editParagraph on one sentence. - Flatter run timing: the replaced words' own time, else the gap between the kept neighbours, with `??` instead of nested ternaries. - keptPairs' comment says why the shared prefix and suffix are matched first: a smaller table, and the words at both ends stay matched when a repeated word could match in more than one place. - countReplacements gives replaceAll's count without building the new state. The editor's live match count used a full replaceAll on every keystroke, which now also re-timed each matching sentence. --- app/src/Editor.tsx | 6 +- core/src/editor.ts | 173 +++++++++++++---------------- core/test/edit-keeps-words.test.ts | 5 +- 3 files changed, 80 insertions(+), 104 deletions(-) diff --git a/app/src/Editor.tsx b/app/src/Editor.tsx index a6ab4f6..e2ca3a4 100644 --- a/app/src/Editor.tsx +++ b/app/src/Editor.tsx @@ -14,6 +14,7 @@ import { reassignSentenceSpeaker, groupParagraphs, replaceAll, + countReplacements, } from "@core/editor"; import type { EditorState, EditorSentence } from "@core/editor"; import { UNKNOWN_SPEAKER } from "@core/types"; @@ -118,13 +119,14 @@ export default function Editor({ ); const starts = useMemo(() => flatWords(state).map((w) => w.start), [state]); - // Dry-run replaceAll for the live match count; the real replace runs on "Replace all". + // Live match count without building the replaced state; the real replace runs on + // "Replace all". const findOpts = useMemo( () => ({ caseSensitive: findCase, wholeWord: findWhole }), [findCase, findWhole], ); const findCount = useMemo( - () => replaceAll(state, findText, replaceText, findOpts).count, + () => countReplacements(state, findText, replaceText, findOpts), [state, findText, replaceText, findOpts], ); diff --git a/core/src/editor.ts b/core/src/editor.ts index d750cb5..c8969a5 100644 --- a/core/src/editor.ts +++ b/core/src/editor.ts @@ -121,7 +121,9 @@ function respread( * subsequence of the two word lists, compared by exact text. */ function keptPairs(oldTexts: string[], newTexts: string[]): Array<[number, number]> { - // Most edits touch a few words, so match the shared prefix and suffix directly. + // Match the shared prefix and suffix first. Edits usually touch a few words in the + // middle, so this keeps the table small; and where a repeated word could match in + // more than one place, it keeps the words at both ends matched. let head = 0; while ( head < oldTexts.length && @@ -172,43 +174,34 @@ function keptPairs(oldTexts: string[], newTexts: string[]): Array<[number, numbe } /** - * The words for edited text `tokens` that replaces `old`, all assigned `speaker`. - * Words the edit kept stay as they were: their timing and `original`. A run of new - * words that replaces old words takes those words' time; a run inserted between kept - * words takes the gap between them (no duration if there is none). With nothing kept, - * the new words spread across `[spanStart, spanEnd]`. + * The words for edited text `tokens` that replaces `old`, all given the speaker of + * `old`'s first word. Words the edit kept stay as they were: their timing and + * `original`. A run of new words that replaces old words takes those words' time; a + * run inserted between kept words takes the gap between them (no duration if there + * is none). With nothing kept, the new words spread across `old`'s whole span. */ -function retime( - old: EditorWord[], - tokens: string[], - spanStart: number, - spanEnd: number, - speaker: SpeakerId, -): EditorWord[] { +function retime(old: EditorWord[], tokens: string[]): EditorWord[] { + const spanStart = old[0]?.start ?? 0; + const spanEnd = old.at(-1)?.end ?? 0; + const speaker = old[0]?.speaker ?? UNKNOWN_SPEAKER; const pairs = keptPairs( old.map((w) => w.text), tokens, ); if (pairs.length === 0) return respread(spanStart, spanEnd, tokens, speaker); + // A sentinel pair past both ends flushes the run after the last kept word. + pairs.push([old.length, tokens.length]); const words: EditorWord[] = []; let oldAt = 0; let newAt = 0; - // A sentinel pair past both ends flushes the run after the last kept word. - for (const [keptOld, keptNew] of [...pairs, [old.length, tokens.length] as [number, number]]) { - const added = tokens.slice(newAt, keptNew); - if (added.length > 0) { + for (const [keptOld, keptNew] of pairs) { + if (keptNew > newAt) { + // The replaced words' own time, else the gap between the kept neighbours. const replaced = keptOld > oldAt; - const from = replaced - ? old[oldAt]!.start - : oldAt > 0 - ? old[oldAt - 1]!.end - : spanStart; - const to = replaced - ? old[keptOld - 1]!.end - : keptOld < old.length - ? old[keptOld]!.start - : spanEnd; + const from = replaced ? old[oldAt]!.start : (old[oldAt - 1]?.end ?? spanStart); + const to = replaced ? old[keptOld - 1]!.end : (old[keptOld]?.start ?? spanEnd); + const added = tokens.slice(newAt, keptNew); words.push(...spread(from, Math.max(to - from, 0), added, speaker)); } if (keptOld < old.length) words.push({ ...old[keptOld]!, speaker }); @@ -219,34 +212,16 @@ function retime( } /** - * Replace a sentence's text. Words the edit didn't touch keep their timing and - * `original`; changed and inserted words are timed from their neighbours (see - * `retime`). Blank text deletes the sentence. + * Replace a sentence's text: `editParagraph` on that one sentence. Words the edit + * didn't touch keep their timing and `original` (see `retime`). Blank text deletes + * the sentence; an unknown id returns the state unchanged. */ export function editSentence( state: EditorState, sentenceId: string, newText: string, ): EditorState { - const next = cloneState(state); - const idx = next.sentences.findIndex((s) => s.id === sentenceId); - if (idx === -1) return next; - - const sentence = next.sentences[idx]!; - const orig = sentence.words; - const s = orig.length > 0 ? orig[0]!.start : 0; - const e = orig.length > 0 ? orig[orig.length - 1]!.end : 0; - const speaker = orig.length > 0 ? orig[0]!.speaker : UNKNOWN_SPEAKER; - - const toks = tokenize(newText); - - if (toks.length === 0) { - next.sentences.splice(idx, 1); - return next; - } - - sentence.words = retime(orig, toks, s, e, speaker); - return next; + return editParagraph(state, [sentenceId], newText); } /** Indexes of `sentenceIds` if they name consecutive sentences in document order, else null. */ @@ -269,9 +244,7 @@ function contiguousRun( /** * Paragraph-scope edit. Re-tokenize `newText` and time it against the run's words - * (see `retime`): untouched words keep their timing and `original`, and with nothing - * kept the text spreads across the run's ANCHOR span — the first listed sentence's - * first-word `start` to the last listed sentence's last-word `end`. The whole + * (see `retime`), so untouched words keep their timing and `original`. The whole * contiguous run collapses to ONE sentence whose id is the FIRST sentence's id (so * highlight/edit ids stay stable) and whose speaker is the first word's speaker. * Blank/whitespace `newText` deletes the entire run. @@ -297,32 +270,11 @@ export function editParagraph( return next; } - let spanStart = 0; - let spanEnd = 0; - let speaker: SpeakerId = UNKNOWN_SPEAKER; - for (const s of runSentences) { - if (s.words.length > 0) { - spanStart = s.words[0]!.start; - speaker = s.words[0]!.speaker; - break; - } - } - for (let i = runSentences.length - 1; i >= 0; i--) { - const w = runSentences[i]!.words; - if (w.length > 0) { - spanEnd = w[w.length - 1]!.end; - break; - } - } - const collapsed: EditorSentence = { id: runSentences[0]!.id, words: retime( runSentences.flatMap((s) => s.words), toks, - spanStart, - spanEnd, - speaker, ), }; next.sentences.splice(startIdx, run.length, collapsed); @@ -342,19 +294,16 @@ function escapeRegExp(s: string): string { } /** - * Literal, per-sentence find/replace (a multi-word `find` matches across word gaps). - * `replace` is inserted verbatim (no `$`-substitution). In a changed sentence only the - * replaced words are re-timed (see `retime`); a replacement that would empty a sentence - * is skipped. Empty `find` is a - * no-op. Returns the new state and the number of matches replaced. + * The sentences a replace-all changes: each one's index, its number of matches and + * its words after replacing. A replacement that would empty a sentence is skipped. */ -export function replaceAll( - state: EditorState, +function replacements( + sentences: EditorSentence[], find: string, replace: string, - opts: ReplaceOptions = {}, -): { state: EditorState; count: number } { - if (find === "") return { state, count: 0 }; + opts: ReplaceOptions, +): Array<{ index: number; matches: number; toks: string[] }> { + if (find === "") return []; const escaped = escapeRegExp(find); const pattern = opts.wholeWord ? `\\b${escaped}\\b` : escaped; @@ -362,34 +311,62 @@ export function replaceAll( const regex = new RegExp(pattern, flags); const replacer = (): string => replace; - const next = cloneState(state); - let count = 0; - - for (const sentence of next.sentences) { - const orig = sentence.words; - const joined = orig.map((w) => w.text).join(" "); + const changed: Array<{ index: number; matches: number; toks: string[] }> = []; + sentences.forEach((sentence, index) => { + const joined = sentence.words.map((w) => w.text).join(" "); regex.lastIndex = 0; let matches = 0; while (regex.exec(joined) !== null) { matches++; } - if (matches === 0) continue; + if (matches === 0) return; + + const toks = tokenize(joined.replace(regex, replacer)); + if (toks.length === 0) return; // no-delete rule + changed.push({ index, matches, toks }); + }); + return changed; +} - const newText = joined.replace(regex, replacer); - const toks = tokenize(newText); - if (toks.length === 0) continue; // no-delete rule +/** + * Literal, per-sentence find/replace (a multi-word `find` matches across word gaps). + * `replace` is inserted verbatim (no `$`-substitution). In a changed sentence only the + * replaced words are re-timed (see `retime`); a replacement that would empty a sentence + * is skipped. Empty `find` is a no-op. Returns the new state and the number of matches + * replaced. + */ +export function replaceAll( + state: EditorState, + find: string, + replace: string, + opts: ReplaceOptions = {}, +): { state: EditorState; count: number } { + if (find === "") return { state, count: 0 }; - const s = orig.length > 0 ? orig[0]!.start : 0; - const e = orig.length > 0 ? orig[orig.length - 1]!.end : 0; - const speaker = orig.length > 0 ? orig[0]!.speaker : UNKNOWN_SPEAKER; - sentence.words = retime(orig, toks, s, e, speaker); + const next = cloneState(state); + let count = 0; + for (const { index, matches, toks } of replacements(state.sentences, find, replace, opts)) { + const sentence = next.sentences[index]!; + sentence.words = retime(sentence.words, toks); count += matches; } - return { state: next, count }; } +/** The `count` that `replaceAll` would return, without building the new state. */ +export function countReplacements( + state: EditorState, + find: string, + replace: string, + opts: ReplaceOptions = {}, +): number { + return replacements(state.sentences, find, replace, opts).reduce( + (sum, r) => sum + r.matches, + 0, + ); +} + /** * Concatenate a CONTIGUOUS run of sentences into ONE sentence (id = the first sentence's * id), preserving every word's timing in document order. No re-spread. Used to join diff --git a/core/test/edit-keeps-words.test.ts b/core/test/edit-keeps-words.test.ts index 0a543d1..ebac49d 100644 --- a/core/test/edit-keeps-words.test.ts +++ b/core/test/edit-keeps-words.test.ts @@ -62,10 +62,7 @@ describe("edits keep untouched words", () => { const words = editSentence(state, sid, "I met Siobhán on Tuesday.").sentences[0]!.words; expect(words.map((w) => w.text)).toEqual(["I", "met", "Siobhán", "on", "Tuesday."]); - expect(words[0]).toEqual(I); - expect(words[1]).toEqual(MET); - expect(words[3]).toEqual(ON); - expect(words[4]).toEqual(TUESDAY); + expect([words[0], words[1], words[3], words[4]]).toEqual([I, MET, ON, TUESDAY]); // "met" ends at 0.6 and "on" starts at 1.5. expect(words[2]!.start).toBeGreaterThanOrEqual(0.6); expect(words[2]!.end).toBeLessThanOrEqual(1.5);