Skip to content

Commit

Permalink
Fix #287
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Dec 31, 2024
1 parent bc0de47 commit 3e8b502
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/utils/__tests__/flag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ test('prependFlag', (t) => {
t.is(prependFlag('阿联酋'), '🇦🇪 阿联酋')
t.is(prependFlag('US'), '🇺🇸 US')
t.is(prependFlag('us'), '🇺🇸 us')
t.is(prependFlag('uk plus'), '🇬🇧 uk plus')
t.is(prependFlag('英国 Plus'), '🇬🇧 英国 Plus')
t.is(prependFlag('UsA-Node'), '🇺🇸 UsA-Node')
t.is(prependFlag('香港_HK'), '🇭🇰 香港_HK')
t.is(prependFlag('新加坡.sg'), '🇸🇬 新加坡.sg')
t.is(prependFlag('日本|JP|'), '🇯🇵 日本|JP|')
t.is(prependFlag('台湾.TWN'), '🇨🇳 台湾.TWN')
t.is(prependFlag('德国Frankfurt'), '🇩🇪 德国Frankfurt')
t.is(prependFlag('🇺🇸 jp'), '🇺🇸 jp')
t.is(prependFlag('🇯🇵 US'), '🇯🇵 US')
t.is(prependFlag('🇺🇸 jp', true), '🇯🇵 jp')
Expand All @@ -32,4 +40,9 @@ test('prependFlag', (t) => {
test('removeFlag', (t) => {
t.is(removeFlag('🇺🇸 jp'), 'jp')
t.is(removeFlag('🇺🇸 🇺🇸 jp'), 'jp')
t.is(removeFlag('🇭🇰 香港节点'), '香港节点')
t.is(removeFlag('🇯🇵 🇺🇸 东京'), '东京')
t.is(removeFlag('🚀 测试节点'), '测试节点')
t.is(removeFlag('节点 🇨🇳'), '节点')
t.is(removeFlag('🇸🇬 新加坡 🇸🇬'), '新加坡')
})
14 changes: 12 additions & 2 deletions src/utils/flag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ export const prependFlag = (
return `${value} ${str}`
}
} else {
if (str.toUpperCase().includes(key)) {
const isKeyChineseCharacters = /[\u4E00-\u9FA5]/.test(key)
const regex = new RegExp(`(^|\\b)${key}(\\b|$)`, 'i')

if (isKeyChineseCharacters && str.toUpperCase().includes(key)) {
return `${value} ${str}`
} else if (!isKeyChineseCharacters && regex.test(str)) {
return `${value} ${str}`
}
}
Expand All @@ -54,7 +59,12 @@ export const prependFlag = (
return `${value} ${str}`
}
} else {
if (str.toUpperCase().includes(key)) {
const isKeyChineseCharacters = /[\u4E00-\u9FA5]/.test(key)
const regex = new RegExp(`(^|\\b)${key}(\\b|$)`, 'i')

if (isKeyChineseCharacters && str.toUpperCase().includes(key)) {
return `${value} ${str}`
} else if (!isKeyChineseCharacters && regex.test(str)) {
return `${value} ${str}`
}
}
Expand Down

0 comments on commit 3e8b502

Please sign in to comment.