Skip to content

Commit

Permalink
fix: fix reorganizeWords expecting segments in a different pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
nihey committed Jun 11, 2024
1 parent c77e493 commit acbdef1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kassellabs/autoeditor",
"version": "1.4.0",
"version": "1.4.1",
"description": "Autoeditor as an Embeddable component",
"main": "dist/editor.js",
"scripts": {
Expand Down
22 changes: 10 additions & 12 deletions src/reorganizeWords.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const convertTimeStringToSeconds = require('./convertTimeStringToSeconds');

const WORD_END_CHARACTERS = new Set(['?', '.', '!']);

const reorganizeWords = (segments, {
Expand All @@ -12,30 +10,30 @@ const reorganizeWords = (segments, {
const reorganizedSegments = [];

let currentSegment = {
startTime: null,
endTime: null,
start: null,
end: null,
words: [],
};

const words = segments.map((l) => l.words).flat();

let currentCharCount = 0;
words.forEach((word, index) => {
const wordEnd = convertTimeStringToSeconds(word.endTime);
const wordEnd = word.end;
const wordString = word.word.trim();
const wordCharCount = wordString.length;

if (currentSegment.startTime === null) {
currentSegment.startTime = word.startTime;
currentSegment.endTime = word.endTime;
if (currentSegment.start === null) {
currentSegment.start = word.start;
currentSegment.end = word.end;
currentSegment.words.push({
...word,
word: removePunctuation ? word.word.trim().replace(/[.?!,]/g, '') : word.word,
});
currentCharCount += wordCharCount;
return; // continue
}
const segmentStart = convertTimeStringToSeconds(currentSegment.startTime);
const segmentStart = currentSegment.start;

const lastWordString = words[index - 1]?.word?.trim() || '';
const isWordEnding = WORD_END_CHARACTERS.has(lastWordString.slice(-1));
Expand All @@ -52,14 +50,14 @@ const reorganizeWords = (segments, {
) {
reorganizedSegments.push(currentSegment);
currentSegment = {
startTime: word.startTime,
endTime: null,
start: word.start,
end: null,
words: [],
};
currentCharCount = 0;
}

currentSegment.endTime = word.endTime;
currentSegment.end = word.end;
currentSegment.words.push({
...word,
word: removePunctuation ? word.word.trim().replace(/[.?!,]/g, '') : word.word,
Expand Down

0 comments on commit acbdef1

Please sign in to comment.