Skip to content

Commit

Permalink
tweak getAbpSnippetArguments
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Jun 7, 2023
1 parent 23dd9f6 commit ac2782b
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/helpers/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ const REMOVE_ATTR_CLASS_APPLYING = ['asap', 'stay', 'complete'];
*
* @param str arbitrary string
* @returns result array
* @throws
*/
const getSentences = (str: string): RegExpMatchArray => {
const getAbpSnippetArguments = (str: string): RegExpMatchArray => {
const reg = /'.*?'|".*?"|\S+/g;
// given RegExp will never return null
return str.match(reg) as RegExpMatchArray;
const sentences = str.match(reg);
if (!sentences) {
throw new Error('Invalid ABP snippet args.');
}
return sentences;
};

/**
Expand Down Expand Up @@ -226,7 +230,7 @@ export const convertAbpSnippetToAdg = (rule: string): string[] => {
// abp-rule may have `;` at the end which makes last array item irrelevant
// https://github.com/AdguardTeam/Scriptlets/issues/236
.filter(isExisting)
.map((args) => getSentences(args)
.map((args) => getAbpSnippetArguments(args)
.map((arg, index) => (index === 0 ? `abp-${arg}` : arg))
.map((arg) => wrapInSingleQuotes(arg))
.join(`${COMMA_SEPARATOR} `))
Expand Down

0 comments on commit ac2782b

Please sign in to comment.