enhance(ServerFinder): 大文字小文字・半角カナのノーマライズを追加

Fix #116
This commit is contained in:
kakkokari-gtyih 2024-02-02 10:39:09 +09:00
parent ed20c70c07
commit a11816e5ec
3 changed files with 48 additions and 6 deletions

View file

@ -109,3 +109,35 @@ export function copyText(val: string) {
return result;
}
/**
* Converts half-width Katakana characters to full-width Katakana characters.
* @param str - The string to convert.
* @returns The converted string with full-width Katakana characters.
*/
export function kanaHalfToFull(str: string): string {
const kanaMap: Record<string, string> = {
'ガ': 'ガ', 'ギ': 'ギ', 'グ': 'グ', 'ゲ': 'ゲ', 'ゴ': 'ゴ',
'ザ': 'ザ', 'ジ': 'ジ', 'ズ': 'ズ', 'ゼ': 'ゼ', 'ゾ': 'ゾ',
'ダ': 'ダ', 'ヂ': 'ヂ', 'ヅ': 'ヅ', 'デ': 'デ', 'ド': 'ド',
'バ': 'バ', 'ビ': 'ビ', 'ブ': 'ブ', 'ベ': 'ベ', 'ボ': 'ボ',
'パ': 'パ', 'ピ': 'ピ', 'プ': 'プ', 'ペ': 'ペ', 'ポ': 'ポ',
'ヴ': 'ヴ', 'ヷ': 'ヷ', 'ヺ': 'ヺ',
'ア': 'ア', 'イ': 'イ', 'ウ': 'ウ', 'エ': 'エ', 'オ': 'オ',
'カ': 'カ', 'キ': 'キ', 'ク': 'ク', 'ケ': 'ケ', 'コ': 'コ',
'サ': 'サ', 'シ': 'シ', 'ス': 'ス', 'セ': 'セ', 'ソ': 'ソ',
'タ': 'タ', 'チ': 'チ', 'ツ': 'ツ', 'テ': 'テ', 'ト': 'ト',
'ナ': 'ナ', 'ニ': 'ニ', 'ヌ': 'ヌ', 'ネ': 'ネ', 'ノ': '',
'ハ': 'ハ', 'ヒ': 'ヒ', 'フ': 'フ', 'ヘ': 'ヘ', 'ホ': 'ホ',
'マ': 'マ', 'ミ': 'ミ', 'ム': 'ム', 'メ': 'メ', 'モ': 'モ',
'ヤ': 'ヤ', 'ユ': 'ユ', 'ヨ': 'ヨ',
'ラ': 'ラ', 'リ': 'リ', 'ル': 'ル', 'レ': 'レ', 'ロ': 'ロ',
'ワ': 'ワ', 'ヲ': 'ヲ', 'ン': 'ン',
'ァ': 'ァ', 'ィ': 'ィ', 'ゥ': 'ゥ', 'ェ': 'ェ', 'ォ': 'ォ',
'ッ': 'ッ', 'ャ': 'ャ', 'ュ': 'ュ', 'ョ': 'ョ',
'。': '。', '、': '、', 'ー': 'ー', '「': '「', '」': '」', '・': '・'
};
var reg = new RegExp('(' + Object.keys(kanaMap).join('|') + ')', 'g');
return str.replace(reg, (m) => kanaMap[m]).replace(/゙/g, '゛').replace(/゚/g, '゜');
};

View file

@ -18,7 +18,7 @@
<XIco class="w-7 h-7" />
</button>
</div>
<form @submit.prevent="() => { f_query = f_query_partial }">
<form @submit.prevent="applyQuery">
<label class="form-label" for="query">{{ $t('_servers._search.query') }}</label>
<div class="input-group">
<input class="form-control" type="search" autocomplete="off" id="query" v-model="f_query_partial" />
@ -129,7 +129,7 @@
<script setup lang="ts">
import type { InstanceInfo, InstanceItem, InstancesStatsObj } from '@/types/instances-info';
import { resolveObjPath } from '@/assets/js/misc';
import { resolveObjPath, kanaHalfToFull } from '@/assets/js/misc';
import langs from '@/assets/data/lang';
import SearchIco from 'bi/search.svg';
@ -232,10 +232,10 @@ const filteredInstances = computed<InstanceItem[]>(() => {
}
if (f_query.value) {
instances = instances.filter((instance) => instance.name.includes(f_query.value) || instance.description?.includes(f_query.value));
instances = instances.filter((instance) => normalizeString(instance.name).includes(f_query.value) || normalizeString(instance?.description ?? '').includes(f_query.value));
}
if (f_langs.value) {
instances = instances.filter((instance) => instance.langs.includes(f_langs.value));
instances = instances.filter((instance) => instance.langs.includes(f_langs.value ?? ''));
}
if (f_registerAcceptance.value) {
instances = instances.filter((instance) => {
@ -279,6 +279,16 @@ const filteredInstances = computed<InstanceItem[]>(() => {
function switchOrder() {
f_order.value = f_order.value === 'asc' ? 'desc' : 'asc';
}
function normalizeString(str: string) {
//
const _res = kanaHalfToFull(str.toLowerCase()).replace(/[ァ-ン]/g, (s) => String.fromCharCode(s.charCodeAt(0) - 0x60));
return _res;
}
function applyQuery() {
f_query.value = normalizeString(f_query_partial.value);
}
</script>
<style scoped>

View file

@ -56,7 +56,7 @@ const route = useRoute();
const instancesStats = ref<InstancesStatsObj>();
function setServerStats(val: InstancesStatsObj) {
function setServerStats(val?: InstancesStatsObj) {
instancesStats.value = val;
}