Skip to content

Commit 833a083

Browse files
committed
Refactor simenticallyEqual function to normalize input strings for comparison. Replaced isComposedOfAllowedChars with normalizeString to remove special characters and ensure case-insensitive equality check.
1 parent 6b4b92a commit 833a083

File tree

3 files changed

+118
-9
lines changed

3 files changed

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

710
// 主要针对 x.com 的 user 和 hashtag
811
export const simenticallyEqual = (a: string, b: string) => {
9-
// 检查 a 和 b 是否都由英文字母、特殊符号和空格组成
10-
const aIsValid = isComposedOfAllowedChars(a);
11-
const bIsValid = isComposedOfAllowedChars(b);
12+
const normalizedA = normalizeString(a);
13+
const normalizedB = normalizeString(b);
1214

13-
const isEqual = aIsValid && bIsValid;
14-
15-
return isEqual;
15+
return normalizedA === normalizedB;
1616
};
17+
18+
// ChatGPT 2.1 和 @chatgpt21 应该是相等的

tests/run-test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
3+
echo "运行 simenticallyEqual 测试..."
4+
node tests/test-simenticallyEqual.test.js
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// 简单的 simenticallyEqual 函数实现(用于测试)
2+
function normalizeString(str) {
3+
// 移除 @ 符号、空格、点号等特殊字符,并转换为小写
4+
return str
5+
.toLowerCase()
6+
.replace(/[@\s\\.]/g, '') // 移除 @、空格、点号
7+
.replace(/[^\w]/g, ''); // 只保留字母和数字
8+
}
9+
10+
function simenticallyEqual(a, b) {
11+
const normalizedA = normalizeString(a);
12+
const normalizedB = normalizeString(b);
13+
return normalizedA === normalizedB;
14+
}
15+
16+
// 简单的测试运行器
17+
function runTest(testName, testFn) {
18+
try {
19+
const result = testFn();
20+
if (result) {
21+
globalThis.console.log(`✅ ${testName} - PASSED`);
22+
} else {
23+
globalThis.console.log(`❌ ${testName} - FAILED`);
24+
}
25+
return result;
26+
} catch (error) {
27+
globalThis.console.log(`❌ ${testName} - ERROR: ${error}`);
28+
return false;
29+
}
30+
}
31+
32+
// 测试用例
33+
const tests = [
34+
{
35+
name: 'ChatGPT 2.1 和 @chatgpt21 应该相等',
36+
test: () => simenticallyEqual('ChatGPT 2.1', '@chatgpt21'),
37+
},
38+
{
39+
name: 'OpenAI 和 @openai 应该相等',
40+
test: () => simenticallyEqual('OpenAI', '@openai'),
41+
},
42+
{
43+
name: '#chatgpt 和 @chatgpt 应该相等',
44+
test: () => simenticallyEqual('#chatgpt', '@chatgpt'),
45+
},
46+
{
47+
name: 'Microsoft 和 @microsoft 应该相等',
48+
test: () => simenticallyEqual('Microsoft', '@microsoft'),
49+
},
50+
{
51+
name: 'Google AI 和 @googleai 应该相等',
52+
test: () => simenticallyEqual('Google AI', '@googleai'),
53+
},
54+
{
55+
name: 'Anthropic 和 @anthropic 应该相等',
56+
test: () => simenticallyEqual('Anthropic', '@anthropic'),
57+
},
58+
{
59+
name: '相同的字符串应该相等',
60+
test: () => simenticallyEqual('test', 'test'),
61+
},
62+
{
63+
name: '不同的大小写应该相等',
64+
test: () => simenticallyEqual('Test', 'test'),
65+
},
66+
{
67+
name: '包含空格和特殊字符的应该相等',
68+
test: () => simenticallyEqual('Test User', '@testuser'),
69+
},
70+
{
71+
name: '不同的字符串不应该相等',
72+
test: () => !simenticallyEqual('test', 'different'),
73+
},
74+
{
75+
name: '空字符串应该相等',
76+
test: () => simenticallyEqual('', ''),
77+
},
78+
];
79+
80+
// 运行所有测试
81+
function runAllTests() {
82+
globalThis.console.log('🧪 开始运行 simenticallyEqual 测试...\n');
83+
84+
let passed = 0;
85+
let total = tests.length;
86+
87+
for (const test of tests) {
88+
if (runTest(test.name, test.test)) {
89+
passed++;
90+
}
91+
}
92+
93+
globalThis.console.log(`\n📊 测试结果: ${passed}/${total} 通过`);
94+
95+
if (passed === total) {
96+
globalThis.console.log('🎉 所有测试都通过了!');
97+
} else {
98+
globalThis.console.log('⚠️ 有测试失败了,请检查代码。');
99+
}
100+
}
101+
102+
// 运行测试
103+
runAllTests();

0 commit comments

Comments
 (0)