|
| 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