Skip to content

Commit

Permalink
Remove lookbehind because safari
Browse files Browse the repository at this point in the history
Resolves #1626
  • Loading branch information
ajayyy committed Dec 26, 2022
1 parent 06a112a commit 27f3ced
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/utils/exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ export function importTimes(data: string, videoDuration: number): SponsorTime[]
const startTime = getFormattedTimeToSeconds(match[0]);
if (startTime !== null) {
// Remove "seconds", "at", special characters, and ")" if there was a "("
const specialCharsMatcher = /^(?:\s+seconds?)?[-:()\s]*|(?:\s+at)?[-:(\s]+$|(?<=^\s*\(.+)[-:()\s]*$/g
const titleLeft = line.split(match[0])[0].replace(specialCharsMatcher, "");
const specialCharMatchers = [{
matcher: /^(?:\s+seconds?)?[-:()\s]*|(?:\s+at)?[-:(\s]+$/g
}, {
matcher: /[-:()\s]*$/g,
condition: (value) => !!value.match(/^\s*\(/)
}];
const titleLeft = removeIf(line.split(match[0])[0], specialCharMatchers);
let titleRight = null;
const split2 = line.split(match[1] || match[0]);
titleRight = split2[split2.length - 1].replace(specialCharsMatcher, "");
titleRight = removeIf(split2[split2.length - 1], specialCharMatchers)

const title = titleLeft?.length > titleRight?.length ? titleLeft : titleRight;
if (title) {
Expand Down Expand Up @@ -77,6 +82,17 @@ export function importTimes(data: string, videoDuration: number): SponsorTime[]
return result;
}

function removeIf(value: string, matchers: Array<{ matcher: RegExp, condition?: (value: string) => boolean }>): string {
let result = value;
for (const matcher of matchers) {
if (!matcher.condition || matcher.condition(value)) {
result = result.replace(matcher.matcher, "");
}
}

return result;
}

export function exportTimesAsHashParam(segments: SponsorTime[]): string {
const hashparamSegments = segments.map(segment => ({
actionType: segment.actionType,
Expand Down

0 comments on commit 27f3ced

Please sign in to comment.