Skip to content

Commit

Permalink
Normalize accents/diacritics (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadashi-aikawa committed Feb 11, 2022
1 parent 7ccc9c4 commit cea95e6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/utils/strings.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
excludeEmoji,
excludeSpace,
normalizeAccentsDiacritics,
smartIncludes,
smartStartsWith,
} from "./strings";
Expand Down Expand Up @@ -31,6 +32,18 @@ describe.each`
});
});

describe.each`
text | expected
${"abcde"} | ${"abcde"}
${"àáâãäå"} | ${"aaaaaa"}
${"çüöà"} | ${"cuoa"}
${"a🍰b"} | ${"a🍰b"}
`("normalizeAccentsDiacritics", ({ text, expected }) => {
test(`normalizeAccentsDiacritics(${text}) = ${expected}`, () => {
expect(normalizeAccentsDiacritics(text)).toBe(expected);
});
});

describe.each`
text | query | expected
${"abcd"} | ${"bc"} | ${true}
Expand Down
17 changes: 13 additions & 4 deletions src/utils/strings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { normalize } from "jest-config";

const regEmoji = new RegExp(
/[\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF]|[\uFE0E-\uFE0F]/,
"g"
Expand All @@ -11,12 +13,19 @@ export function excludeEmoji(text: string): string {
return text.replace(regEmoji, "");
}

export function normalizeAccentsDiacritics(text: string): string {
// https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript
return text.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
}

export function smartIncludes(text: string, query: string): boolean {
return excludeSpace(text.toLowerCase()).includes(query.toLowerCase());
return excludeSpace(normalizeAccentsDiacritics(text.toLowerCase())).includes(
normalizeAccentsDiacritics(query.toLowerCase())
);
}

export function smartStartsWith(text: string, query: string): boolean {
return excludeSpace(excludeEmoji(text.toLowerCase())).startsWith(
query.toLowerCase()
);
return excludeSpace(
excludeEmoji(normalizeAccentsDiacritics(text.toLowerCase()))
).startsWith(normalizeAccentsDiacritics(query.toLowerCase()));
}

0 comments on commit cea95e6

Please sign in to comment.