Skip to content

Commit b7aa028

Browse files
committed
Refactor simenticallyEqual function to validate input strings for allowed characters before comparison. Introduced isComposedOfAllowedChars helper function to check for valid characters, enhancing input validation.
1 parent cda8ea4 commit b7aa028

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

pages/content/src/simenticallyEqual.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
/* eslint-disable arrow-body-style */
2-
const normalizeString = (str: string): string => {
3-
return str
4-
.replace(/^[^a-zA-Z]+|[^a-zA-Z]+$/g, '') // 移除首尾的非英文字母字符
5-
.toLowerCase(); // 转换为小写
2+
const isComposedOfAllowedChars = (str: string): boolean => {
3+
// 检查字符串是否只包含英文字母、特殊符号和空格
4+
return /^[a-zA-Z\s!@#$%^&*()_+\-=\\[\]{};':"\\|,.<>\\/?`~]+$/.test(str);
65
};
76

87
// 主要针对 x.com 的 user 和 hashtag
98
export const simenticallyEqual = (a: string, b: string) => {
10-
const isEqual = normalizeString(a) === normalizeString(b);
9+
// 检查 a 和 b 是否都由英文字母、特殊符号和空格组成
10+
const aIsValid = isComposedOfAllowedChars(a);
11+
const bIsValid = isComposedOfAllowedChars(b);
12+
13+
const isEqual = aIsValid && bIsValid;
1114
if (isEqual) {
1215
console.log({ a, b }, isEqual);
1316
}

0 commit comments

Comments
 (0)