From a53887a76ddae1a5f12cfebefaa6c447adc7ef09 Mon Sep 17 00:00:00 2001 From: Mikal S <7761729+revam@users.noreply.github.com> Date: Sat, 23 Sep 2023 20:44:31 +0200 Subject: [PATCH] fix: fix auto match regexes crashing safari <16.4 (#646) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit by encapulating the logic to create the array holding the regexes in a try…catch block. --- src/core/utilities/auto-match-regexes.ts | 168 ++++++++++++----------- 1 file changed, 87 insertions(+), 81 deletions(-) diff --git a/src/core/utilities/auto-match-regexes.ts b/src/core/utilities/auto-match-regexes.ts index 292bfb8a1..0fc017111 100644 --- a/src/core/utilities/auto-match-regexes.ts +++ b/src/core/utilities/auto-match-regexes.ts @@ -1,95 +1,101 @@ import type { PathDetails, PathMatchRule } from './auto-match-logic'; -// TODO: mohan couldn't find a dprint setting to make the = go to next line. This needs to be fixed. -// eslint-disable-next-line operator-linebreak -const TrimShowNameRegex = - /(?![\s_.]*\(part[\s_.]*[ivx]+\))(?![\s_.]*\((?:19|20)\d{2}\))(?:[\s_.]*(?:[([{][^)\]}\n]*[)\]}]|(?:(? { - const modifiedDetails = { ...originalDetails }; - - // Add crc32 if found - const crc32Result = Crc32Regex.exec(match[0]); - if (crc32Result) { - modifiedDetails.crc32 = crc32Result[1] || crc32Result[2]; - } - - // Fix up show name by removing unwanted details and fixing spaces. - if (modifiedDetails.showName) { - let showName = modifiedDetails.showName.replace(TrimShowNameRegex, ''); - - // Fix movie name when no episode number is provided. - const { episode, episodeName, year } = match.groups!; - if (episodeName && !episode) { - const { episodeName: [rangeEnd], showName: [, rangeStart] } = match.indices!.groups!; - const inBetween = match[0].slice(rangeStart, rangeEnd); - if (ReStitchRegex.test(inBetween)) { - showName = showName + inBetween + episodeName; - modifiedDetails.episodeName = null; - } +const PathMatchRuleSet: PathMatchRule[] = []; + +try { + // TODO: mohan couldn't find a dprint setting to make the = go to next line. This needs to be fixed. + // eslint-disable-next-line operator-linebreak + const TrimShowNameRegex = + /(?![\s_.]*\(part[\s_.]*[ivx]+\))(?![\s_.]*\((?:19|20)\d{2}\))(?:[\s_.]*(?:[([{][^)\]}\n]*[)\]}]|(?:(? { + const modifiedDetails = { ...originalDetails }; + + // Add crc32 if found + const crc32Result = Crc32Regex.exec(match[0]); + if (crc32Result) { + modifiedDetails.crc32 = crc32Result[1] || crc32Result[2]; } - // Convert underscores and dots to spaces if we don't have any spaces in - // the show name yet. - if (!showName.includes(' ')) { - showName = showName.replace(/[_.]+/g, ' ').replace(/\s+/g, ' ').trim(); - // A hack. - if (showName.startsWith('//')) { - showName = `.${showName}`; + // Fix up show name by removing unwanted details and fixing spaces. + if (modifiedDetails.showName) { + let showName = modifiedDetails.showName.replace(TrimShowNameRegex, ''); + + // Fix movie name when no episode number is provided. + const { episode, episodeName, year } = match.groups!; + if (episodeName && !episode) { + const { episodeName: [rangeEnd], showName: [, rangeStart] } = match.indices!.groups!; + const inBetween = match[0].slice(rangeStart, rangeEnd); + if (ReStitchRegex.test(inBetween)) { + showName = showName + inBetween + episodeName; + modifiedDetails.episodeName = null; + } } - } - // Append the "season" number to the show name to help with the search if a - // year was not found. - if (modifiedDetails.season !== null && modifiedDetails.season !== 1 && episode && !year) { - showName += ` S${modifiedDetails.season}`; + // Convert underscores and dots to spaces if we don't have any spaces in + // the show name yet. + if (!showName.includes(' ')) { + showName = showName.replace(/[_.]+/g, ' ').replace(/\s+/g, ' ').trim(); + // A hack. + if (showName.startsWith('//')) { + showName = `.${showName}`; + } + } + + // Append the "season" number to the show name to help with the search if a + // year was not found. + if (modifiedDetails.season !== null && modifiedDetails.season !== 1 && episode && !year) { + showName += ` S${modifiedDetails.season}`; + } + + modifiedDetails.showName = showName; } - modifiedDetails.showName = showName; - } + if (modifiedDetails.episodeName) { + let episodeName = modifiedDetails.episodeName.replace(TrimShowNameRegex, ''); - if (modifiedDetails.episodeName) { - let episodeName = modifiedDetails.episodeName.replace(TrimShowNameRegex, ''); + // Convert underscores and dots to spaces if we don't have any spaces in + // the show name yet. + if (!episodeName.includes(' ')) { + episodeName = episodeName.replace(/[_.]+/g, ' ').replace(/\s+/g, ' ').trim(); + } - // Convert underscores and dots to spaces if we don't have any spaces in - // the show name yet. - if (!episodeName.includes(' ')) { - episodeName = episodeName.replace(/[_.]+/g, ' ').replace(/\s+/g, ' ').trim(); + modifiedDetails.episodeName = episodeName; } - modifiedDetails.episodeName = episodeName; - } - - // Correct movie numbering. - if ( - (match.groups!.isMovie || match.groups!.isMovie2) && modifiedDetails.episodeStart === 0 - && modifiedDetails.episodeEnd === 0 - ) { - modifiedDetails.episodeStart = 1; - modifiedDetails.episodeEnd = 1; - } - return modifiedDetails; -}; - -const PathMatchRuleSet: PathMatchRule[] = [ - { - name: 'default', - regex: - /^(?:[{[(](?[^)}\]]+)[)}\]][\s_.]*)?(?(?gekijouban[\s_.]+)?(?:[a-z]+[\s_.]+\d+(?=[\s_.]*(?:-+[\s_.]*)[a-z]+))?.+?(?[\s_.]*(?:[-!+]+[\s_.]*)?(?:the[\s_.]+)?movie)?(?:[\s_.]*\(part[\s_.]*[ivx]+\))?(?:[\s_.]*\((?(?:19|20)\d{2})\))?)[\s_.]*(?:-+[\s_.]*)?(?:(?:(?(?sp(?:ecial)?)|(?ova)(?:[\s_.]+(?:[_-]+[\s_.]*)?e|(?=e))|s(?:eason)?(?\d+)(?:[\s_.]+(?:[_-]+[\s_.]*)?e?|(?=e))|)(?:(?\d+(?:-+\d+|\.5)?|(?<=(?:ed|op)\s*)\d+\.\d+))(?:v(?\d{1,2}))?(?:[\s_.]*-+(?:[\s_.]+(?(?!\d)[^([{\n]*?))?)?(?:[\s_.]+(?:[\s_.]*-+)?)?(?:[\s_.]*(?:\([^)]*\)|\[[^\]]*\]|{[^}]*}|[([{]+[^)\]}\n]*[)\]}]+|(?:(?[a-zA-Z0-9_\-+]+)$/id, - transform: defaultTransform, - }, - { - name: 'default+', - regex: - /^(?:[{[(](?[^)}\]]+)[)}\]][\s_.]*)?(?(?gekijouban[\s_.]+)?(?:[a-z]+[\s_.]+\d+(?=[\s_.]*(?:-+[\s_.]*)[a-z]+))?.+?(?[\s_.]*(?:[-!+]+[\s_.]*)?(?:the[\s_.]+)?movie)?(?:[\s_.]*\(part[\s_.]*[ivx]+\))?(?:[\s_.]*\((?(?:19|20)\d{2})\))?)[\s_.]*(?:-+[\s_.]*)?(?:(?:(?(?sp(?:ecial)?)|(?ova)(?:[\s_.]+(?:[_-]+[\s_.]*)?e|(?=e))|s(?:eason)?(?\d+)(?:[\s_.]+(?:[_-]+[\s_.]*)?e?|(?=e))|)(?:(?\d+(?:-+\d+|\.5)?|(?<=(?:ed|op)\s*)\d+\.\d+))?(?:v(?\d{1,2}))?(?:[\s_.]*-+(?:[\s_.]+(?(?!\d)[^([{\n]*?))?)?(?:[\s_.]+(?:[\s_.]*-+)?)?(?:[\s_.]*(?:\([^)]*\)|\[[^\]]*\]|{[^}]*}|[([{]+[^)\]}\n]*[)\]}]+|(?:(?[a-zA-Z0-9_\-+]+)$/id, - transform: defaultTransform, - }, - // TODO: Add more rules here. -]; + // Correct movie numbering. + if ( + (match.groups!.isMovie || match.groups!.isMovie2) && modifiedDetails.episodeStart === 0 + && modifiedDetails.episodeEnd === 0 + ) { + modifiedDetails.episodeStart = 1; + modifiedDetails.episodeEnd = 1; + } + return modifiedDetails; + }; + + PathMatchRuleSet.push( + { + name: 'default', + regex: + /^(?:[{[(](?[^)}\]]+)[)}\]][\s_.]*)?(?(?gekijouban[\s_.]+)?(?:[a-z]+[\s_.]+\d+(?=[\s_.]*(?:-+[\s_.]*)[a-z]+))?.+?(?[\s_.]*(?:[-!+]+[\s_.]*)?(?:the[\s_.]+)?movie)?(?:[\s_.]*\(part[\s_.]*[ivx]+\))?(?:[\s_.]*\((?(?:19|20)\d{2})\))?)[\s_.]*(?:-+[\s_.]*)?(?:(?:(?(?sp(?:ecial)?)|(?ova)(?:[\s_.]+(?:[_-]+[\s_.]*)?e|(?=e))|s(?:eason)?(?\d+)(?:[\s_.]+(?:[_-]+[\s_.]*)?e?|(?=e))|)(?:(?\d+(?:-+\d+|\.5)?|(?<=(?:ed|op)\s*)\d+\.\d+))(?:v(?\d{1,2}))?(?:[\s_.]*-+(?:[\s_.]+(?(?!\d)[^([{\n]*?))?)?(?:[\s_.]+(?:[\s_.]*-+)?)?(?:[\s_.]*(?:\([^)]*\)|\[[^\]]*\]|{[^}]*}|[([{]+[^)\]}\n]*[)\]}]+|(?:(?[a-zA-Z0-9_\-+]+)$/id, + transform: defaultTransform, + }, + { + name: 'default+', + regex: + /^(?:[{[(](?[^)}\]]+)[)}\]][\s_.]*)?(?(?gekijouban[\s_.]+)?(?:[a-z]+[\s_.]+\d+(?=[\s_.]*(?:-+[\s_.]*)[a-z]+))?.+?(?[\s_.]*(?:[-!+]+[\s_.]*)?(?:the[\s_.]+)?movie)?(?:[\s_.]*\(part[\s_.]*[ivx]+\))?(?:[\s_.]*\((?(?:19|20)\d{2})\))?)[\s_.]*(?:-+[\s_.]*)?(?:(?:(?(?sp(?:ecial)?)|(?ova)(?:[\s_.]+(?:[_-]+[\s_.]*)?e|(?=e))|s(?:eason)?(?\d+)(?:[\s_.]+(?:[_-]+[\s_.]*)?e?|(?=e))|)(?:(?\d+(?:-+\d+|\.5)?|(?<=(?:ed|op)\s*)\d+\.\d+))?(?:v(?\d{1,2}))?(?:[\s_.]*-+(?:[\s_.]+(?(?!\d)[^([{\n]*?))?)?(?:[\s_.]+(?:[\s_.]*-+)?)?(?:[\s_.]*(?:\([^)]*\)|\[[^\]]*\]|{[^}]*}|[([{]+[^)\]}\n]*[)\]}]+|(?:(?[a-zA-Z0-9_\-+]+)$/id, + transform: defaultTransform, + }, + // TODO: Add more rules here. + ); +} catch (error) { + console.error(error); +} export default PathMatchRuleSet;