Détail du package

wanakana

WaniKani128.6kMIT5.3.1

Utility library for converting between Kanji, Hiragana, Katakana, and Romaji

english, japanese, hiragana, katakana

readme

ワナカナ <--> WanaKana <--> わなかな

Javascript utility library for detecting and transliterating Hiragana, Katakana, and Romaji

Demo

Visit the website to see WanaKana in action.

Usage

In the browser without a build step, use the minified (UMD) bundle (with browser polyfills)

https://unpkg.com/wanakana

<head>
  <meta charset="UTF-8">
  <script src="https://unpkg.com/wanakana"></script>
</head>
<body>
  <input type="text" id="wanakana-input" />
  <script>
    var textInput = document.getElementById('wanakana-input');
    wanakana.bind(textInput, /* options */); // uses IMEMode with toKana() as default
    // to remove event listeners: wanakana.unbind(textInput);
  </script>
</body>

ES Modules or Node

Install

npm install wanakana

ES Modules

import * as wanakana from 'wanakana';
// or
import { toKana, isRomaji } from 'wanakana';

Node (>=12 supported)

const wanakana = require('wanakana');

Documentation

Extended API reference

Quick Reference

/*** DOM HELPERS ***/
// Automatically converts text using an eventListener on input
// Sets option: { IMEMode: true } with toKana() as converter by default
wanakana.bind(domElement [, options]);

// Removes event listener
wanakana.unbind(domElement);

/*** TEXT CHECKING UTILITIES ***/
wanakana.isJapanese('泣き虫。!〜2¥zenkaku')
// => true

wanakana.isKana('あーア')
// => true

wanakana.isHiragana('すげー')
// => true

wanakana.isKatakana('ゲーム')
// => true

wanakana.isKanji('切腹')
// => true
wanakana.isKanji('勢い')
// => false

wanakana.isRomaji('Tōkyō and Ōsaka')
// => true

wanakana.toKana('ONAJI buttsuuji')
// => 'オナジ ぶっつうじ'
wanakana.toKana('座禅‘zazen’スタイル')
// => '座禅「ざぜん」スタイル'
wanakana.toKana('batsuge-mu')
// => 'ばつげーむ'
wanakana.toKana('wanakana', { customKanaMapping: { na: 'に', ka: 'bana' }) });
// => 'わにbanaに'

wanakana.toHiragana('toukyou, オオサカ')
// => 'とうきょう、 おおさか'
wanakana.toHiragana('only カナ', { passRomaji: true })
// => 'only かな'
wanakana.toHiragana('wi', { useObsoleteKana: true })
// => 'ゐ'

wanakana.toKatakana('toukyou, おおさか')
// => 'トウキョウ、 オオサカ'
wanakana.toKatakana('only かな', { passRomaji: true })
// => 'only カナ'
wanakana.toKatakana('wi', { useObsoleteKana: true })
// => 'ヰ'

wanakana.toRomaji('ひらがな カタカナ')
// => 'hiragana katakana'
wanakana.toRomaji('ひらがな カタカナ', { upcaseKatakana: true })
// => 'hiragana KATAKANA'
wanakana.toRomaji('つじぎり', { customRomajiMapping: { じ: 'zi', つ: 'tu', り: 'li' }) };
// => 'tuzigili'

/*** EXTRA UTILITIES ***/
wanakana.stripOkurigana('お祝い')
// => 'お祝'
wanakana.stripOkurigana('踏み込む')
// => '踏み込'
wanakana.stripOkurigana('お腹', { leading: true });
// => '腹'
wanakana.stripOkurigana('ふみこむ', { matchKanji: '踏み込む' });
// => 'ふみこ'
wanakana.stripOkurigana('おみまい', { matchKanji: 'お祝い', leading: true });
// => 'みまい'

wanakana.tokenize('ふふフフ')
// => ['ふふ', 'フフ']
wanakana.tokenize('hello 田中さん')
// => ['hello', ' ', '田中', 'さん']
wanakana.tokenize('I said 私はすごく悲しい', { compact: true })
// => [ 'I said ', '私はすごく悲しい']

Important

Only the browser build via unpkg or the root wanakana.min.js includes polyfills for older browsers.

Contributing

Please see CONTRIBUTING.md

Contributors

Credits

Project sponsored by Tofugu & WaniKani

Ports

The following ports have been created by the community:

changelog

Change Log

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

[5.3.1] - 2023-11-20

Fixed

Wanakana will now report its version correctly, this was briefly incorrect in version 5.3.0.

[5.3.0] - 2023-11-19

Added

Typescript definitions have been added 🎉

[5.2.0] - 2023-09-30

Fixed

The iteration mark was considered punctuation due to where it lies in unicode (alongside other punctuation). This caused some oddities in Wanakana when splitting characters. Since acts like a kanji character in words, it is no longer considered punctuation in Wanakana. See #163 for further discussion.

  • isKanji() now returns true for
  • isPunctuation() now returns false for
  • tokenize() no longer splits on
  • stripOkurigana() no longer splits on

[5.1.0] - 2023-02-03

Fixed

  • bind() could be used on the same element multiple times
  • unbind() now returns an element to its previous state (attributes etc)

Changed

  • unbind() removes the attributes that were added by Wanakana

[5.0.2] - 2022-05-17

Fixed

  • bind() was ignoring useObsoleteKana & customKanaMapping options
  • ku not converting immediately with IMEMode

[5.0.!] - 2022-04-15

Fixed

  • Unpkg should now properly source the correct module for use in the browser as a script tag

[5.0.0] - 2022-04-15

Fixed

  • When a custom mapping was provided in options and then a different one was given, we only replaced the mapping with the new one when a previous one did not exist. Now we will always replace the custom mapping when it changes. https://github.com/WaniKani/WanaKana/pull/132

Added

Changed

  • The UMD build now only includes polyfills for browserslist defaults (> 0.5%, last 2 versions, Firefox ESR, not dead - at time of writing) which drops support for IE.
  • Node support is now 12+ (though likely would continue to work fine on older versions)

4.0.2 - 2018-04-30

Added

  • react-native field in lib package.json

Fixed

  • corrected recent dates in changelog (2017 -> 2018)

4.0.1 - 2018-04-30

Fixed

  • stripOkurigana(): If the input has the same kana at different locations, such as '申し申し', the inner kana was stripped instead

4.0.0 - 2018-04-26

Changed

  • stripOkurigana() options are now { leading: Boolean, matchKanji: String }

Removed

  • stripOkurigana() "all" option has been removed but can be easily reproduced via [...text].filter(isKana)

3.1.1 - 2018-03-26

Fixed

  • some versions of Android GBoard English keyboard entering 'n' followed by a consonant would block further conversion

3.1.0 - 2018-03-01

Changed

  • bind() now sets necessary attributes automatically (autocomplete, spellcheck etc)
  • toKana() no longer converts zenkaku latin toKana now: ('imi imi' => 'imi いみ')
  • toRomaji() extends vowels for katakana chōonpu, IE: ゲーム => geemu, toHiragana() converts to hyphen => ge-mu
  • toKana() without IME mode converts lone 'n' => 'ん', 'nn' => 'んん' if no other chars present
  • isJapanese() now returns false for latin numbers (201) (still true for zenkaku 201)
  • tokenize() splits into finer categories, view API documentation and tests for full details
  • rewrite of conversion methods to allow custom mapping adapters

Added

  • isJapanese and isRomaji now accept a second param, a regexp that will also pass the check
  • global option romanization for toRomaji() (currently only 'hepburn' however)
  • global option customKanaMapping for toKana()
  • global option customRomajiMapping for toRomaji()

Fixed

  • inserting text between already existing characters in a bind() IME input field now properly sets cursor to correct position after conversion
  • toRomaji() little ヶヵ used in words like 一ヶ月 are no longer converted since they are used as symbols (like the kanji) and do not actually denote kana. Previous behaviour: 一ヶ月 => 一 ke 月
  • toRomaji() no longer incorrectly duplicates non-glottal stops following っ. Previous behaviour: あっ、 -> a,,

2.3.4 - 2017-12-16

Fixed

  • Uppercase input with toHiragana() including a double consonant was incorrectly producing a katakana ッ instead of っ

2.3.3 - 2017-12-12

Fixed

  • Add https to unpkg link

2.3.2 - 2017-12-12

Added

  • Specify minified bundle in package.json for easy use with unpkg

2.3.1 - 2017-10-17

Changed

  • Set cursor in IME mode to the current position after conversion (rather than the end of all input)

2.3.0 - 2017-08-28

Changed

  • Increase character coverage for isJapanese to include numbers and hankaku katakana

2.2.4 - 2017-08-24

Fixed

  • Pass through long vowel conversion using toHiragana with odd/mixed input

2.2.3 - 2017-08-05

Fixed

  • Mobile input not converting automatically during autosuggest (regression due to 2.2.1)

2.2.2 - 2017-07-30

Fixed

  • Keep track of event listeners by generated ids

2.2.1 - 2017-07-30

Fixed

2.2.0 - 2017-07-13

Added

  • Options object setting IMEMode can now accept 'toHiragana' or 'toKatakana' to enforce specific conversion on input

2.1.0 - 2017-07-09

Added

  • Set autocapitalize="none" on bound input fields
  • Handle multiple event listeners with separate options
  • Increased test coverage

Fixed

  • Hold onto merged options for dom utils via closure instead of global
  • Keep track of event listeners for removal on unbind

Changed

  • bind, unbind are now named exports in 'wanakana/domUtils'

2.0.4 - 2017-07-07

Fixed

  • Wanakana website address in package.json

2.0.3 - 2017-07-07

Fixed

  • Missing description field in package.json

2.0.2 - 2017-06-30

Added

  • Documentation regarding recommended use of autocapitalize="none" on input fields

Changed

  • Improve font readability on demo page

2.0.1 - 2017-06-17

Changed

  • Fixed some incorrect text references
  • Internal build modifications

2.0.0 - 2017-06-17

Added

  • Changelog!
  • Separate bundles for different environments (node, esmodules, browser)
  • New method: stripOkurigana()
  • New method: tokenize()
  • Default options extended with passRomaji and upcaseKatakana
  • Extended docs

Fixed

Changed

  • Transliteration converts major punctuation marks both ways.
  • isJapanese() & isRomaji() check major punctuation.
  • isRomaji() allows hepburn romanisation long vowels. (IE. Tōkyō)