PSeitz / wana_kana_rust

Utility library for checking and converting between Japanese characters - Hiragana, Katakana - and Romaji
MIT License
70 stars 13 forks source link

Implement some syntax sugar traits #8

Closed TianyiShi2001 closed 2 years ago

TianyiShi2001 commented 3 years ago

This PR addresses issue #7 . Three traits are being implemented, which allows ergonomic conversion between kana and romaji; checking whether a &str is Japanese; and checking whether a char is Japanese, using default options (although additional methods with a _with_opt suffix can be added to these traits, which can fully replace the current API). Should we deprecate the old functions and fully migrate to the new API in the next major version, or should both sets of API be kept?

Examples:

use wana_kana::Convert;
// or use wana_kana::traits::*
// to kana
assert_eq!("o".to_kana(), "お");
assert_eq!("ona".to_kana(), "おな");
assert_eq!("onaji".to_kana(), "おなじ");
assert_eq!("onaji BUTTSUUJI".to_kana(), "おなじ ブッツウジ");
assert_eq!("ONAJI buttsuuji".to_kana(), "オナジ ぶっつうじ");
assert_eq!("座禅‘zazen’スタイル".to_kana(), "座禅「ざぜん」スタイル");
// to hiragana
assert_eq!("toukyou,オオサカ".to_hiragana(), "とうきょう、おおさか");
// to katakana
assert_eq!("toukyou,おおさか".to_katakana(), "トウキョウ、オオサカ");
// to romaji
assert_eq!("ひらがな カタカナ".to_romaji(), "hiragana katakana");
use wana_kana::IsJapaneseStr;
// or `use wana_kana::traits::*`
assert_eq!("げーむ".is_hiragana(), true);
assert_eq!("A".is_hiragana(), false);
assert_eq!("あア".is_hiragana(), false);

assert_eq!("ゲーム".is_katakana(), true);
assert_eq!("あ".is_katakana(), false);
assert_eq!("A".is_katakana(), false);
assert_eq!("あア".is_katakana(), false);

assert_eq!("あ".is_kana(), true);
assert_eq!("ア".is_kana(), true);
assert_eq!("あーア".is_kana(), true);
assert_eq!("A".is_kana(), false);
assert_eq!("あAア".is_kana(), false);

assert_eq!("A".is_romaji(), true);
assert_eq!("xYz".is_romaji(), true);
assert_eq!("Tōkyō and Ōsaka".is_romaji(), true);
assert_eq!("あアA".is_romaji(), false);
assert_eq!("お願い".is_romaji(), false);
assert_eq!("熟成".is_romaji(), false);
assert_eq!("a*b&c-d".is_romaji(), true);
assert_eq!("0123456789".is_romaji(), true);
assert_eq!("a!b&cーd".is_romaji(), false);
assert_eq!("hello".is_romaji(), false);

assert_eq!("刀".is_kanji(), true);
assert_eq!("切腹".is_kanji(), true);
assert_eq!("勢い".is_kanji(), false);
assert_eq!("あAア".is_kanji(), false);
assert_eq!("🐸".is_kanji(), false);

assert_eq!("泣き虫".is_japanese(), true);
assert_eq!("あア".is_japanese(), true);
assert_eq!("2月".is_japanese(), true); // Zenkaku numbers allowed
assert_eq!("泣き虫。!〜$".is_japanese(), true); // Zenkaku/JA punctuation
assert_eq!("泣き虫.!~$".is_japanese(), false); // Latin punctuation fails
assert_eq!("A".is_japanese(), false);

assert_eq!("Aア".is_mixed(), true);
assert_eq!("Aあ".is_mixed(), true);
assert_eq!("Aあア".is_mixed(), true);
assert_eq!("2あア".is_mixed(), false);
assert_eq!("お腹A".is_mixed(), true);
PSeitz commented 3 years ago

Looks good except minor comment regarding naming.

I think it would be good to keep the API, to avoid break if it is not necessary, but the docs should be updated to use the traits as the preferred way.

magnus-ISU commented 2 years ago

Any reason this isn't merged? I'm considering using this library for a project, and it seems like work is done.

PSeitz commented 2 years ago

Sorry completely forgot about that, I will look at it tomorrow

PSeitz commented 2 years ago

Looks good, thank you!

PSeitz commented 2 years ago

@magnus-ISU @TianyiShi2001 released with 2.1.0 https://crates.io/crates/wana_kana