remove string proto fake polyfill

This commit is contained in:
MathMan05
2025-11-26 17:20:22 -06:00
parent 10649cdbc6
commit d18cdd2d28
5 changed files with 10 additions and 56 deletions
@@ -41,6 +41,7 @@ import {
Snowflake,
uploadFile,
User,
stringGlobToRegexp,
} from "@spacebar/util";
import { Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";
@@ -453,8 +454,8 @@ router.post(
if (rule.trigger_type == AutomodTriggerTypes.CUSTOM_WORDS) {
const triggerMeta = rule.trigger_metadata as AutomodCustomWordsRule;
const regexes = triggerMeta.regex_patterns.map((x) => new RegExp(x, "i")).concat(triggerMeta.keyword_filter.map((k) => k.globToRegexp("i")));
const allowedRegexes = triggerMeta.allow_list.map((k) => k.globToRegexp("i"));
const regexes = triggerMeta.regex_patterns.map((x) => new RegExp(x, "i")).concat(triggerMeta.keyword_filter.map((k) => stringGlobToRegexp(k, "i")));
const allowedRegexes = triggerMeta.allow_list.map((k) => stringGlobToRegexp(k, "i"));
const matches = regexes
.map((r) => message.content!.match(r))
+7 -1
View File
@@ -37,4 +37,10 @@ export function centerString(str: string, len: number): string {
const pad = len - str.length;
const padLeft = Math.floor(pad / 2) + str.length;
return str.padStart(padLeft).padEnd(len);
}
}
export function stringGlobToRegexp(str: string, flags?: string): RegExp {
// Convert simple wildcard patterns to regex
const escaped = str.replace(".", "\\.").replace("?", ".").replace("*", ".*");
return new RegExp(escaped, flags);
}
-15
View File
@@ -1,15 +0,0 @@
import moduleAlias from "module-alias";
moduleAlias();
import './String';
import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
describe("String extensions", () => {
it("globToRegexp", () => {
const pattern = "file-*.txt";
const regex = pattern.globToRegexp();
assert.ok(regex.test("file-123.txt"));
});
});
-37
View File
@@ -1,37 +0,0 @@
/*
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2025 Spacebar and Spacebar Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare global {
interface String {
globToRegexp(flags?: string): RegExp;
}
}
export function stringGlobToRegexp(str: string, flags?: string): RegExp {
// Convert simple wildcard patterns to regex
const escaped = str.replace(".", "\\.")
.replace("?", ".")
.replace("*", ".*")
return new RegExp(escaped, flags);
}
// Register extensions
if (!String.prototype.globToRegexp)
String.prototype.globToRegexp = function (str: string, flags?: string) {
return stringGlobToRegexp.call(null, str, flags);
};
-1
View File
@@ -1,2 +1 @@
export * from "./Array";
export * from "./String";