Skip to content

Commit

Permalink
unicode-collation-algorithm2 を消した
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-20 committed Mar 24, 2024
1 parent dfc1690 commit ff28808
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 30 deletions.
2 changes: 0 additions & 2 deletions workspaces/admin/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import './setup';

import { ChakraProvider, useToast } from '@chakra-ui/react';
import { QueryClientProvider } from '@tanstack/react-query';
import { RouterProvider } from '@tanstack/react-router';
Expand Down
18 changes: 8 additions & 10 deletions workspaces/admin/src/lib/filter/isContains.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { compareWithFlags, PRIMARY as UCA_L1_FLAG, SECONDARY as UCA_L2_FLAG } from 'unicode-collation-algorithm2';

// UCA_L1_FLAG はベース文字、UCA_L2_FLAG は濁点・半濁点・アクセントを区別する (sensitivity: accent に相当)
const SENSITIVITY_ACCENT_FLAG = UCA_L1_FLAG ^ UCA_L2_FLAG;

type Params = {
query: string;
target: string;
};

const normalize = (str: string) => {
str = str.normalize('NFKC');
// カタカナを平仮名に
if (str >= 'ァ' && str <= 'ン') return str.charCodeAt(0) + ('あ'.charCodeAt(0) - 'ア'.charCodeAt(0));
return str;
};

// ひらがな・カタカナ・半角・全角を区別せずに文字列が含まれているかを調べる
export function isContains({ query, target }: Params): boolean {
// target の先頭から順に query が含まれているかを調べる
TARGET_LOOP: for (let offset = 0; offset <= target.length - query.length; offset++) {
for (let idx = 0; idx < query.length; idx++) {
// 1文字ずつ Unicode Collation Algorithm で比較する
// unicode-collation-algorithm2 は Default Unicode Collation Element Table (DUCET) を collation として使う
if (compareWithFlags(target[offset + idx]!, query[idx]!, SENSITIVITY_ACCENT_FLAG) !== 0) {
continue TARGET_LOOP;
}
if (normalize(target[offset + idx]!) === normalize(query[idx]!)) continue TARGET_LOOP;
}
// query のすべての文字が含まれていたら true を返す
return true;
Expand Down
3 changes: 0 additions & 3 deletions workspaces/admin/src/setup.ts

This file was deleted.

2 changes: 0 additions & 2 deletions workspaces/app/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import './setup';

import { Dialog } from './foundation/components/Dialog';
import { GlobalStyle } from './foundation/styles/GlobalStyle';
import { Router } from './routes';
Expand Down
18 changes: 8 additions & 10 deletions workspaces/app/src/lib/filter/isContains.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { compareWithFlags, PRIMARY as UCA_L1_FLAG, SECONDARY as UCA_L2_FLAG } from 'unicode-collation-algorithm2';

// UCA_L1_FLAG はベース文字、UCA_L2_FLAG は濁点・半濁点・アクセントを区別する (sensitivity: accent に相当)
const SENSITIVITY_ACCENT_FLAG = UCA_L1_FLAG ^ UCA_L2_FLAG;

type Params = {
query: string;
target: string;
};

const normalize = (str: string) => {
str = str.normalize('NFKC');
// カタカナを平仮名に
if (str >= 'ァ' && str <= 'ン') return str.charCodeAt(0) + ('あ'.charCodeAt(0) - 'ア'.charCodeAt(0));
return str;
};

// ひらがな・カタカナ・半角・全角を区別せずに文字列が含まれているかを調べる
export function isContains({ query, target }: Params): boolean {
// target の先頭から順に query が含まれているかを調べる
TARGET_LOOP: for (let offset = 0; offset <= target.length - query.length; offset++) {
for (let idx = 0; idx < query.length; idx++) {
// 1文字ずつ Unicode Collation Algorithm で比較する
// unicode-collation-algorithm2 は Default Unicode Collation Element Table (DUCET) を collation として使う
if (compareWithFlags(target[offset + idx]!, query[idx]!, SENSITIVITY_ACCENT_FLAG) !== 0) {
continue TARGET_LOOP;
}
if (normalize(target[offset + idx]!) === normalize(query[idx]!)) continue TARGET_LOOP;
}
// query のすべての文字が含まれていたら true を返す
return true;
Expand Down
3 changes: 0 additions & 3 deletions workspaces/app/src/setup.ts

This file was deleted.

0 comments on commit ff28808

Please sign in to comment.