Skip to content

Commit

Permalink
add a quick and dirty regex validator
Browse files Browse the repository at this point in the history
  • Loading branch information
KentoNishi committed Jun 29, 2024
1 parent c6e8530 commit ad02f8a
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"cssnano": "^5.0.7",
"download": "8.0.0",
"eslint": "7",
"eslint-config-standard-with-typescript": "^20.0.0",
"eslint-config-standard-with-typescript": "^43.0.1",
"eslint-plugin-import": "2",
"eslint-plugin-node": "11",
"eslint-plugin-promise": "4",
Expand Down
9 changes: 6 additions & 3 deletions src/components/settings/YtcFilterFilterSummary.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { exioIcon } from 'exio/svelte';
import { languageNameCode } from '../../ts/tl-tag-detect';
import { isValidRegex } from '../../ts/ytcf-logic';
export let filter: YtcF.ChatFilter;
export let isTextFilter: (filter: YtcF.FilterCondition) => filter is YtcF.StringCondition;
export let compact = false;
Expand Down Expand Up @@ -38,13 +39,15 @@
const value = condition.type === 'tltag' ? languageNameCode[condition.value].selectionName : condition.value;
const trueFalse = condition.invert ? falses[condition.type] : trues[condition.type];
const suffix = ['includes', 'startsWith', 'endsWith', 'equals'].includes(condition.type) && condition.caseSensitive ? '(case sensitive)' : '';
const valid = condition.type === 'regex' ? isValidRegex(value) : Boolean(value);
return [{
type: 'string',
value: `${prefix} ${trueFalse}`
}, {
type: 'literal',
value,
suffix
suffix,
valid
}];
} else {
const prefix = propertyMap[condition.property];
Expand All @@ -66,8 +69,8 @@
<span>{index === index1 && index === 0 ? (run.value.charAt(0).toUpperCase() + run.value.slice(1)) : run.value}</span>
{:else if run.type === 'literal'}
&nbsp;"<code class:compact={compact}>{run.value}</code><span>"</span>
{#if !run.value}
<span use:exioIcon style="vertical-align: -3px; color: #ff9800;">warning</span>
{#if !run.valid}
<span use:exioIcon style="vertical-align: -3px; color: #ff9800;" title="Warning: invalid condition!">warning</span>
{/if}
{#if run.suffix}
<span>{run.suffix}</span>
Expand Down
9 changes: 9 additions & 0 deletions src/ts/ytcf-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ export const testRegex = (expression: string, value: string): { result: boolean;
}
}

export const isValidRegex = (expression: string): boolean => {
try {
parseRegex(expression);
return true;
} catch (e) {
return false;
}
}

export function shouldActivatePreset(preset: YtcF.FilterPreset, info: SimpleVideoInfo): boolean {
for (const trigger of preset.triggers) {
let compStr = '';
Expand Down
134 changes: 122 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@
eslint-scope "^5.1.1"
eslint-utils "^3.0.0"

"@typescript-eslint/parser@^4.0.0", "@typescript-eslint/parser@^4.28.5":
"@typescript-eslint/parser@^4.28.5":
version "4.28.5"
resolved "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-4.28.5.tgz"
integrity sha512-NPCOGhTnkXGMqTznqgVbA5LqVsnw+i3+XA1UKLnAb+MG1Y1rP4ZSK9GX0kJBmAZTMIktf+dTwXToT6kFwyimbw==
Expand All @@ -392,6 +392,17 @@
"@typescript-eslint/typescript-estree" "4.28.5"
debug "^4.3.1"

"@typescript-eslint/parser@^6.4.0":
version "6.21.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-6.21.0.tgz#af8fcf66feee2edc86bc5d1cf45e33b0630bf35b"
integrity sha512-tbsV1jPne5CkFQCgPBcDOt30ItF7aJoZL997JSF7MhGQqOeT3svWRYxiqlfA5RUdlHN6Fi+EI9bxqbdyAUZjYQ==
dependencies:
"@typescript-eslint/scope-manager" "6.21.0"
"@typescript-eslint/types" "6.21.0"
"@typescript-eslint/typescript-estree" "6.21.0"
"@typescript-eslint/visitor-keys" "6.21.0"
debug "^4.3.4"

"@typescript-eslint/scope-manager@4.28.5":
version "4.28.5"
resolved "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-4.28.5.tgz"
Expand All @@ -408,6 +419,14 @@
"@typescript-eslint/types" "4.29.1"
"@typescript-eslint/visitor-keys" "4.29.1"

"@typescript-eslint/scope-manager@6.21.0":
version "6.21.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-6.21.0.tgz#ea8a9bfc8f1504a6ac5d59a6df308d3a0630a2b1"
integrity sha512-OwLUIWZJry80O99zvqXVEioyniJMa+d2GrqpUTqi5/v5D5rOrppJVBPa0yKCblcigC0/aYAzxxqQ1B+DS2RYsg==
dependencies:
"@typescript-eslint/types" "6.21.0"
"@typescript-eslint/visitor-keys" "6.21.0"

"@typescript-eslint/types@4.28.5":
version "4.28.5"
resolved "https://registry.npmjs.org/@typescript-eslint/types/-/types-4.28.5.tgz"
Expand All @@ -418,6 +437,11 @@
resolved "https://registry.npmjs.org/@typescript-eslint/types/-/types-4.29.1.tgz"
integrity sha512-Jj2yu78IRfw4nlaLtKjVaGaxh/6FhofmQ/j8v3NXmAiKafbIqtAPnKYrf0sbGjKdj0hS316J8WhnGnErbJ4RCA==

"@typescript-eslint/types@6.21.0":
version "6.21.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-6.21.0.tgz#205724c5123a8fef7ecd195075fa6e85bac3436d"
integrity sha512-1kFmZ1rOm5epu9NZEZm1kckCDGj5UJEf7P1kliH4LKu/RkwpsfqqGmY2OOcUs18lSlQBKLDYBOGxRVtrMN5lpg==

"@typescript-eslint/typescript-estree@4.28.5":
version "4.28.5"
resolved "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-4.28.5.tgz"
Expand All @@ -444,6 +468,20 @@
semver "^7.3.5"
tsutils "^3.21.0"

"@typescript-eslint/typescript-estree@6.21.0":
version "6.21.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-6.21.0.tgz#c47ae7901db3b8bddc3ecd73daff2d0895688c46"
integrity sha512-6npJTkZcO+y2/kr+z0hc4HwNfrrP4kNYh57ek7yCNlrBjWQ1Y0OS7jiZTkgumrvkX5HkEKXFZkkdFNkaW2wmUQ==
dependencies:
"@typescript-eslint/types" "6.21.0"
"@typescript-eslint/visitor-keys" "6.21.0"
debug "^4.3.4"
globby "^11.1.0"
is-glob "^4.0.3"
minimatch "9.0.3"
semver "^7.5.4"
ts-api-utils "^1.0.1"

"@typescript-eslint/visitor-keys@4.28.5":
version "4.28.5"
resolved "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-4.28.5.tgz"
Expand All @@ -460,6 +498,14 @@
"@typescript-eslint/types" "4.29.1"
eslint-visitor-keys "^2.0.0"

"@typescript-eslint/visitor-keys@6.21.0":
version "6.21.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-6.21.0.tgz#87a99d077aa507e20e238b11d56cc26ade45fe47"
integrity sha512-JJtkDduxLi9bivAB+cYOVMtbkqdPOhZ+ZI5LC47MIRrDV4Yn2o+ZnW10Nkmr28xRpSpdJ6Sm42Hjf2+REYXm0A==
dependencies:
"@typescript-eslint/types" "6.21.0"
eslint-visitor-keys "^3.4.1"

accepts@~1.3.8:
version "1.3.8"
resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.8.tgz#0bf0be125b67014adcb0b0921e62db7bffe16b2e"
Expand Down Expand Up @@ -908,6 +954,13 @@ brace-expansion@^1.1.7:
balanced-match "^1.0.0"
concat-map "0.0.1"

brace-expansion@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-2.0.1.tgz#1edc459e0f0c548486ecf9fc99f2221364b9a0ae"
integrity sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==
dependencies:
balanced-match "^1.0.0"

braces@^3.0.1, braces@~3.0.2:
version "3.0.2"
resolved "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz"
Expand Down Expand Up @@ -1727,6 +1780,13 @@ debug@^4.0.1, debug@^4.1.1, debug@^4.3.1, debug@^4.3.2, debug@^4.3.3:
dependencies:
ms "2.1.2"

debug@^4.3.4:
version "4.3.5"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.5.tgz#e83444eceb9fedd4a1da56d671ae2446a01a6e1e"
integrity sha512-pt0bNEmneDIvdL1Xsd9oDQ/wrQRkXDT4AUWlNZNPKvW5x/jyO9VFXkJUP07vQ2upmw5PlaITaPKc31jK13V+jg==
dependencies:
ms "2.1.2"

decamelize@5.0.0:
version "5.0.0"
resolved "https://registry.npmjs.org/decamelize/-/decamelize-5.0.0.tgz"
Expand Down Expand Up @@ -2285,18 +2345,18 @@ escape-string-regexp@^4.0.0:
resolved "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz"
integrity sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==

eslint-config-standard-with-typescript@^20.0.0:
version "20.0.0"
resolved "https://registry.npmjs.org/eslint-config-standard-with-typescript/-/eslint-config-standard-with-typescript-20.0.0.tgz"
integrity sha512-IoySf3r0a2+P3Z6GMjv8p1HuOQ6GWQbMpdt9G8uEbkGpnNWAGBXpgaiutbZHbaQAvG5pkVtYepCfHUxYbVDLCA==
eslint-config-standard-with-typescript@^43.0.1:
version "43.0.1"
resolved "https://registry.yarnpkg.com/eslint-config-standard-with-typescript/-/eslint-config-standard-with-typescript-43.0.1.tgz#977862d7d41b0e1f27f399137bbf7b2e017037ff"
integrity sha512-WfZ986+qzIzX6dcr4yGUyVb/l9N3Z8wPXCc5z/70fljs3UbWhhV+WxrfgsqMToRzuuyX9MqZ974pq2UPhDTOcA==
dependencies:
"@typescript-eslint/parser" "^4.0.0"
eslint-config-standard "^16.0.0"
"@typescript-eslint/parser" "^6.4.0"
eslint-config-standard "17.1.0"

eslint-config-standard@^16.0.0:
version "16.0.3"
resolved "https://registry.npmjs.org/eslint-config-standard/-/eslint-config-standard-16.0.3.tgz"
integrity sha512-x4fmJL5hGqNJKGHSjnLdgA6U6h1YW/G2dW9fA+cyVur4SK6lyue8+UgNKWlZtUDTXvgKDD/Oa3GQjmB5kjtVvg==
eslint-config-standard@17.1.0:
version "17.1.0"
resolved "https://registry.yarnpkg.com/eslint-config-standard/-/eslint-config-standard-17.1.0.tgz#40ffb8595d47a6b242e07cbfd49dc211ed128975"
integrity sha512-IwHwmaBNtDK4zDHQukFDW5u/aTb8+meQWZvNFWkiGmbWjD6bqyuSSBxxXKkCftCUzc1zwCH2m/baCNDLGmuO5Q==

eslint-import-resolver-node@^0.3.5:
version "0.3.5"
Expand Down Expand Up @@ -2415,6 +2475,11 @@ eslint-visitor-keys@^2.0.0:
resolved "https://registry.npmjs.org/eslint-visitor-keys/-/eslint-visitor-keys-2.1.0.tgz"
integrity sha512-0rSmRBzXgDzIsD6mGdJgevzgezI534Cer5L/vyMX0kHzT/jiB43jRhd9YUlMGYLQy2zprNmoT8qasCGtY+QaKw==

eslint-visitor-keys@^3.4.1:
version "3.4.3"
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-3.4.3.tgz#0cd72fe8550e3c2eae156a96a4dddcd1c8ac5800"
integrity sha512-wpc+LXeiyiisxPlEkUzU6svyS1frIO3Mgxj1fdy7Pm8Ygzguax2N3Fa/D/ag1WqbOprdI+uY6wMUl8/a2G+iag==

eslint@7:
version "7.32.0"
resolved "https://registry.npmjs.org/eslint/-/eslint-7.32.0.tgz"
Expand Down Expand Up @@ -2711,6 +2776,17 @@ fast-glob@^3.1.1, fast-glob@^3.2.7:
merge2 "^1.3.0"
micromatch "^4.0.4"

fast-glob@^3.2.9:
version "3.3.2"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.3.2.tgz#a904501e57cfdd2ffcded45e99a54fef55e46129"
integrity sha512-oX2ruAFQwf/Orj8m737Y5adxDQO0LAB7/S5MnxCdTNDd4p6BsyIVsv9JQsATbTSq8KHRpLwIHbVlUNatxd+1Ow==
dependencies:
"@nodelib/fs.stat" "^2.0.2"
"@nodelib/fs.walk" "^1.2.3"
glob-parent "^5.1.2"
merge2 "^1.3.0"
micromatch "^4.0.4"

fast-json-patch@^2.0.6:
version "2.2.1"
resolved "https://registry.npmjs.org/fast-json-patch/-/fast-json-patch-2.2.1.tgz"
Expand Down Expand Up @@ -3149,6 +3225,18 @@ globby@^11.0.3:
merge2 "^1.3.0"
slash "^3.0.0"

globby@^11.1.0:
version "11.1.0"
resolved "https://registry.yarnpkg.com/globby/-/globby-11.1.0.tgz#bd4be98bb042f83d796f7e3811991fbe82a0d34b"
integrity sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g==
dependencies:
array-union "^2.1.0"
dir-glob "^3.0.1"
fast-glob "^3.2.9"
ignore "^5.2.0"
merge2 "^1.4.1"
slash "^3.0.0"

got@^8.3.1:
version "8.3.2"
resolved "https://registry.yarnpkg.com/got/-/got-8.3.2.tgz#1d23f64390e97f776cac52e5b936e5f514d2e937"
Expand Down Expand Up @@ -3411,6 +3499,11 @@ ignore@^5.1.1, ignore@^5.1.4:
resolved "https://registry.npmjs.org/ignore/-/ignore-5.1.8.tgz"
integrity sha512-BMpfD7PpiETpBl/A6S498BaIJ6Y/ABT93ETbby2fP00v4EbvPBXWEoaR1UBPKs3iR53pJY7EtZk5KACI57i1Uw==

ignore@^5.2.0:
version "5.3.1"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.3.1.tgz#5073e554cd42c5b33b394375f538b8593e34d4ef"
integrity sha512-5Fytz/IraMjqpwfd34ke28PTVMjZjJG2MPn5t7OE4eUCUNf8BAa7b5WUS9/Qvr6mwOQS7Mk6vdsMno5he+T8Xw==

image-size@1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/image-size/-/image-size-1.0.0.tgz"
Expand Down Expand Up @@ -4322,7 +4415,7 @@ merge-stream@^2.0.0:
resolved "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz"
integrity sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==

merge2@^1.2.3, merge2@^1.3.0:
merge2@^1.2.3, merge2@^1.3.0, merge2@^1.4.1:
version "1.4.1"
resolved "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz"
integrity sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==
Expand Down Expand Up @@ -4396,6 +4489,13 @@ min-indent@^1.0.0:
dependencies:
brace-expansion "^1.1.7"

minimatch@9.0.3:
version "9.0.3"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-9.0.3.tgz#a6e00c3de44c3a542bfaae70abfc22420a6da825"
integrity sha512-RHiac9mvaRw0x3AYRgDC1CxAP7HTcNrrECeA8YYJeWnpo+2Q5CegtZjaotWTWxDG3UeGA1coE05iH1mPjT/2mg==
dependencies:
brace-expansion "^2.0.1"

minimatch@~3.0.4:
version "3.0.8"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.8.tgz#5e6a59bd11e2ab0de1cfb843eb2d82e546c321c1"
Expand Down Expand Up @@ -6451,6 +6551,11 @@ semver@^7.2.1, semver@^7.3.2, semver@^7.3.4, semver@^7.3.5:
dependencies:
lru-cache "^6.0.0"

semver@^7.5.4:
version "7.6.2"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.2.tgz#1e3b34759f896e8f14d6134732ce798aeb0c6e13"
integrity sha512-FNAIBWCx9qcRhoHcgcJ0gvU7SN1lYU2ZXuSfl04bSC5OpvDHFyJCjdNHomPXxjQlCBU67YW64PzY7/VIEH7F2w==

send@0.17.2:
version "0.17.2"
resolved "https://registry.yarnpkg.com/send/-/send-0.17.2.tgz#926622f76601c41808012c8bf1688fe3906f7820"
Expand Down Expand Up @@ -7226,6 +7331,11 @@ truncate-utf8-bytes@^1.0.0:
dependencies:
utf8-byte-length "^1.0.1"

ts-api-utils@^1.0.1:
version "1.3.0"
resolved "https://registry.yarnpkg.com/ts-api-utils/-/ts-api-utils-1.3.0.tgz#4b490e27129f1e8e686b45cc4ab63714dc60eea1"
integrity sha512-UQMIo7pb8WRomKR1/+MFVLTroIvDVtMX3K6OUir8ynLyzB8Jeriont2bTAtmNPa1ekAgN7YPDyf6V+ygrdU+eQ==

tsconfig-paths@^3.9.0:
version "3.10.1"
resolved "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.10.1.tgz"
Expand Down

0 comments on commit ad02f8a

Please sign in to comment.