Members
DefaultOptions :DefaultOptions
Default config for WanaKana, user passed options will be merged with these
Properties:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
useObsoleteKana |
Boolean |
<optional> |
false |
Set to true to use obsolete characters, such as ゐ and ゑ. |
passRomaji |
Boolean |
<optional> |
false |
Set to true to pass romaji when using mixed syllabaries with toKatakana() or toHiragana() |
convertLongVowelMark |
Boolean |
<optional> |
true |
Set to false to prevent conversions of 'ー' to extended vowels with toHiragana() |
upcaseKatakana |
Boolean |
<optional> |
false |
Set to true to convert katakana to uppercase using toRomaji() |
IMEMode |
Boolean | 'toHiragana' | 'toKatakana' |
<optional> |
false |
Set to true, 'toHiragana', or 'toKatakana' to handle conversion while it is being typed. |
romanization |
'hepburn' |
<optional> |
'hepburn' |
choose toRomaji() romanization map (currently only 'hepburn') |
customKanaMapping |
Object.<String, String> |
<optional> |
custom map will be merged with default conversion |
|
customRomajiMapping |
Object.<String, String> |
<optional> |
custom map will be merged with default conversion |
- Source:
Examples
toHiragana('we', { useObsoleteKana: true })
// => 'ゑ'
toHiragana('only convert the katakana: ヒラガナ', { passRomaji: true })
// => "only convert the katakana: ひらがな"
toHiragana('ラーメン', { convertLongVowelMark: false });
// => 'らーめん
toRomaji('ひらがな カタカナ', { upcaseKatakana: true })
// => "hiragana KATAKANA"
toKana('wanakana', { customKanaMapping: { na: 'に', ka: 'Bana' }) };
// => 'わにBanaに'
toRomaji('つじぎり', { customRomajiMapping: { じ: 'zi', つ: 'tu', り: 'li' }) };
// => 'tuzigili'
Methods
bind(element, optionsopt)
Binds eventListener for 'input' events to an input field to automagically replace values with kana
Can pass { IMEMode: 'toHiragana' || 'toKatakana' }
to enforce kana conversion type
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
element |
HTMLInputElement | HTMLTextAreaElement |
textarea, input[type="text"] etc |
||
options |
DefaultOptions |
<optional> |
defaultOptions |
defaults to { IMEMode: true } using |
isHiragana(inputopt) → {Boolean}
Test if input
is Hiragana
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
input |
String |
<optional> |
'' |
text |
- Source:
isJapanese(inputopt, allowedopt) → {Boolean}
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
input |
String |
<optional> |
'' |
text |
allowed |
RegExp |
<optional> |
additional test allowed to pass for each char |
- Source:
Example
isJapanese('泣き虫')
// => true
isJapanese('あア')
// => true
isJapanese('2月') // Zenkaku numbers allowed
// => true
isJapanese('泣き虫。!〜$') // Zenkaku/JA punctuation
// => true
isJapanese('泣き虫.!~$') // Latin punctuation fails
// => false
isJapanese('A泣き虫')
// => false
isJapanese('≪偽括弧≫', /[≪≫]/);
// => true
isKana(inputopt) → {Boolean}
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
input |
String |
<optional> |
'' |
text |
Example
isKana('あ')
// => true
isKana('ア')
// => true
isKana('あーア')
// => true
isKana('A')
// => false
isKana('あAア')
// => false
isKanji(inputopt) → {Boolean}
Tests if input
is Kanji (Japanese CJK ideographs)
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
input |
String |
<optional> |
'' |
text |
- Source:
Example
isKanji('刀')
// => true
isKanji('切腹')
// => true
isKanji('勢い')
// => false
isKanji('あAア')
// => false
isKanji('🐸')
// => false
isKatakana(inputopt) → {Boolean}
Test if input
is Katakana
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
input |
String |
<optional> |
'' |
text |
- Source:
Example
isKatakana('ゲーム')
// => true
isKatakana('あ')
// => false
isKatakana('A')
// => false
isKatakana('あア')
// => false
isMixed(input, optionsopt) → {Boolean}
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
input |
String |
text |
||
options |
Object |
<optional> |
{ passKanji: true } |
optional config to pass through kanji |
- Source:
Example
isMixed('Abあア'))
// => true
isMixed('お腹A')) // ignores kanji by default
// => true
isMixed('お腹A', { passKanji: false }))
// => false
isMixed('ab'))
// => false
isMixed('あア'))
// => false
isRomaji(inputopt, allowedopt) → {Boolean}
Test if input
is Romaji (allowing Hepburn romanisation)
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
input |
String |
<optional> |
'' |
text |
allowed |
RegExp |
<optional> |
additional test allowed to pass for each char |
- Source:
Example
isRomaji('Tōkyō and Ōsaka')
// => true
isRomaji('12a*b&c-d')
// => true
isRomaji('あアA')
// => false
isRomaji('お願い')
// => false
isRomaji('a!b&cーd') // Zenkaku punctuation fails
// => false
isRomaji('a!b&cーd', /[!ー]/)
// => true
stripOkurigana(input, optionsopt) → {String}
Strips Okurigana
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
input |
String |
text |
||
options |
Object |
<optional> |
{ leading: false, matchKanji: '' } |
optional config |
- Source:
Example
stripOkurigana('踏み込む')
// => '踏み込'
stripOkurigana('お祝い')
// => 'お祝'
stripOkurigana('お腹', { leading: true });
// => '腹'
stripOkurigana('ふみこむ', { matchKanji: '踏み込む' });
// => 'ふみこ'
stripOkurigana('おみまい', { matchKanji: 'お祝い', leading: true });
// => 'みまい'
toHiragana(inputopt, optionsopt) → {String}
Convert input to Hiragana
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
input |
String |
<optional> |
'' |
text |
options |
DefaultOptions |
<optional> |
defaultOptions |
- Source:
Example
toHiragana('toukyou, オオサカ')
// => 'とうきょう、 おおさか'
toHiragana('only カナ', { passRomaji: true })
// => 'only かな'
toHiragana('wi')
// => 'うぃ'
toHiragana('wi', { useObsoleteKana: true })
// => 'ゐ'
toKana(inputopt, optionsopt, mapopt) → {String}
Convert Romaji to Kana, lowercase text will result in Hiragana and uppercase text will result in Katakana.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
input |
String |
<optional> |
'' |
text |
options |
DefaultOptions |
<optional> |
defaultOptions | |
map |
Object.<string, string> |
<optional> |
custom mapping |
Example
toKana('onaji BUTTSUUJI')
// => 'おなじ ブッツウジ'
toKana('ONAJI buttsuuji')
// => 'オナジ ぶっつうじ'
toKana('座禅‘zazen’スタイル')
// => '座禅「ざぜん」スタイル'
toKana('batsuge-mu')
// => 'ばつげーむ'
toKana('!?.:/,~-‘’“”[](){}') // Punctuation conversion
// => '!?。:・、〜ー「」『』[](){}'
toKana('we', { useObsoleteKana: true })
// => 'ゑ'
toKana('wanakana', { customKanaMapping: { na: 'に', ka: 'bana' } });
// => 'わにbanaに'
toKatakana(inputopt, optionsopt) → {String}
Convert input to Katakana
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
input |
String |
<optional> |
'' |
text |
options |
DefaultOptions |
<optional> |
defaultOptions |
- Source:
Example
toKatakana('toukyou, おおさか')
// => 'トウキョウ、 オオサカ'
toKatakana('only かな', { passRomaji: true })
// => 'only カナ'
toKatakana('wi')
// => 'ウィ'
toKatakana('wi', { useObsoleteKana: true })
// => 'ヰ'
tokenize(input, optionsopt) → {Array.<String>|Array.<{type: String, value: String}>}
Splits input into array of strings separated by opinionated token types
'en', 'ja', 'englishNumeral', 'japaneseNumeral','englishPunctuation', 'japanesePunctuation','kanji', 'hiragana', 'katakana', 'space', 'other'
.
If { compact: true }
then many same-language tokens are combined (spaces + text, kanji + kana, numeral + punctuation).
If { detailed: true }
then return array will contain { type, value }
instead of 'value'
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
input |
String |
text |
||
options |
Object |
<optional> |
{ compact: false, detailed: false} |
options to modify output style |
- Source:
Example
tokenize('ふふフフ')
// ['ふふ', 'フフ']
tokenize('感じ')
// ['感', 'じ']
tokenize('人々')
// ['人々']
tokenize('truly 私は悲しい')
// ['truly', ' ', '私', 'は', '悲', 'しい']
tokenize('truly 私は悲しい', { compact: true })
// ['truly ', '私は悲しい']
tokenize('5romaji here...!?人々漢字ひらがなカタ カナ4「SHIO」。!')
// [ '5', 'romaji', ' ', 'here', '...!?', '人々漢字', 'ひらがな', 'カタ', ' ', 'カナ', '4', '「', 'SHIO', '」。!']
tokenize('5romaji here...!?人々漢字ひらがなカタ カナ4「SHIO」。!', { compact: true })
// [ '5', 'romaji here', '...!?', '人々漢字ひらがなカタ カナ', '4「', 'SHIO', '」。!']
tokenize('5romaji here...!?人々漢字ひらがなカタ カナ4「SHIO」。! لنذهب', { detailed: true })
// [
{ type: 'englishNumeral', value: '5' },
{ type: 'en', value: 'romaji' },
{ type: 'space', value: ' ' },
{ type: 'en', value: 'here' },
{ type: 'englishPunctuation', value: '...!?' },
{ type: 'kanji', value: '人々漢字' },
{ type: 'hiragana', value: 'ひらがな' },
{ type: 'katakana', value: 'カタ' },
{ type: 'space', value: ' ' },
{ type: 'katakana', value: 'カナ' },
{ type: 'japaneseNumeral', value: '4' },
{ type: 'japanesePunctuation', value: '「' },
{ type: 'ja', value: 'SHIO' },
{ type: 'japanesePunctuation', value: '」。!' },
{ type: 'space', value: ' ' },
{ type: 'other', value: 'لنذهب' },
]
tokenize('5romaji here...!?人々漢字ひらがなカタ カナ4「SHIO」。! لنذهب', { compact: true, detailed: true})
// [
{ type: 'other', value: '5' },
{ type: 'en', value: 'romaji here' },
{ type: 'other', value: '...!?' },
{ type: 'ja', value: '人々漢字ひらがなカタ カナ' },
{ type: 'other', value: '4「' },
{ type: 'ja', value: 'SHIO' },
{ type: 'other', value: '」。!' },
{ type: 'en', value: ' ' },
{ type: 'other', value: 'لنذهب' },
]
toRomaji(kana, optionsopt, mapopt) → {String}
Convert kana to romaji
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
kana |
String |
text input |
||
options |
DefaultOptions |
<optional> |
defaultOptions | |
map |
Object.<string, string> |
<optional> |
custom mapping |
- Source:
Example
toRomaji('ひらがな カタカナ')
// => 'hiragana katakana'
toRomaji('げーむ ゲーム')
// => 'ge-mu geemu'
toRomaji('ひらがな カタカナ', { upcaseKatakana: true })
// => 'hiragana KATAKANA'
toRomaji('つじぎり', { customRomajiMapping: { じ: 'zi', つ: 'tu', り: 'li' } });
// => 'tuzigili'
unbind(element)
Unbinds eventListener from input field
Parameters:
Name | Type | Description |
---|---|---|
element |
HTMLInputElement | HTMLTextAreaElement |
textarea, input |