Comparing version 3.1.0-rc.3 to 3.1.0-rc.4
@@ -27,9 +27,7 @@ # Change Log | ||
## [3.1.0] - 2017-2-23 | ||
## [3.1.0] - 2017-03-01 | ||
### Changed | ||
* bind and unbind are now flat modules (wanakana/bind) rather than named exports from domUtils | ||
* domUtils moved to utils/dom | ||
* rewrite of conversion methods to allow custom mapping adapters | ||
* bind() now sets necessary attributes automatically (autocomplete, spellcheck etc) | ||
* toKana() no longer converts zenkaku latin toKana now: ('imi imi' => 'imi いみ') | ||
@@ -39,3 +37,4 @@ * toRomaji() extends vowels for katakana chōonpu, IE: ゲーム => geemu, toHiragana() converts to hyphen => ge-mu | ||
* isJapanese() now returns false for latin numbers (201) (still true for zenkaku 201) | ||
* tokenize() splits into finer categories, view tests for full examples | ||
* tokenize() splits into finer categories, view API documentation and tests for full details | ||
* rewrite of conversion methods to allow custom mapping adapters | ||
@@ -45,3 +44,3 @@ ### 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') | ||
* global option romanization for toRomaji() (currently only 'hepburn' however) | ||
* global option customKanaMapping for toKana() | ||
@@ -48,0 +47,0 @@ * global option customRomajiMapping for toRomaji() |
@@ -25,4 +25,8 @@ import { makeOnInput, onComposition, trackListeners } from './utils/dom'; | ||
input.setAttribute('data-wanakana-id', id); | ||
input.setAttribute('lang', 'ja'); | ||
input.setAttribute('autoCapitalize', 'none'); | ||
input.setAttribute('autoCorrect', 'off'); | ||
input.setAttribute('autoComplete', 'off'); | ||
input.setAttribute('spellCheck', 'false'); | ||
input.addEventListener('input', onInput); | ||
input.addEventListener('compositionstart', onComposition); | ||
input.addEventListener('compositionupdate', onComposition); | ||
@@ -29,0 +33,0 @@ input.addEventListener('compositionend', onComposition); |
@@ -1,2 +0,2 @@ | ||
export const VERSION = '3.1.0-rc.3'; | ||
export const VERSION = '3.1.0-rc.4'; | ||
@@ -3,0 +3,0 @@ export const TO_KANA_METHODS = { |
import mergeWithDefaultOptions from './utils/mergeWithDefaultOptions'; | ||
import katakanaToHiragana from './utils/katakanaToHiragana'; | ||
import romajiToHiragana from './utils/romajiToHiragana'; | ||
import isCharEnglishPunctuation from './utils/isCharEnglishPunctuation'; | ||
import isRomaji from './isRomaji'; | ||
import isMixed from './isMixed'; | ||
import toKana from './toKana'; | ||
import toRomaji from './toRomaji'; | ||
@@ -26,17 +27,17 @@ /** | ||
if (config.passRomaji) { | ||
return katakanaToHiragana(input); | ||
return katakanaToHiragana(input, toRomaji); | ||
} | ||
if (isRomaji(input) || isCharEnglishPunctuation(input)) { | ||
return romajiToHiragana(input, config); | ||
if (isMixed(input, { passKanji: true })) { | ||
const convertedKatakana = katakanaToHiragana(input, toRomaji); | ||
return toKana(convertedKatakana.toLowerCase(), config); | ||
} | ||
if (isMixed(input, { passKanji: true })) { | ||
const romaji = katakanaToHiragana(input); | ||
return romajiToHiragana(romaji, config); | ||
if (isRomaji(input) || isCharEnglishPunctuation(input)) { | ||
return toKana(input.toLowerCase(), config); | ||
} | ||
return katakanaToHiragana(input); | ||
return katakanaToHiragana(input, toRomaji); | ||
} | ||
export default toHiragana; |
import mergeWithDefaultOptions from './utils/mergeWithDefaultOptions'; | ||
import hiraganaToKatakana from './utils/hiraganaToKatakana'; | ||
import romajiToHiragana from './utils/romajiToHiragana'; | ||
import isCharEnglishPunctuation from './utils/isCharEnglishPunctuation'; | ||
import toKana from './toKana'; | ||
import isRomaji from './isRomaji'; | ||
@@ -30,4 +30,4 @@ import isMixed from './isMixed'; | ||
if (isMixed(input) || isRomaji(input) || isCharEnglishPunctuation(input)) { | ||
const romaji = romajiToHiragana(input, mergedOptions); | ||
return hiraganaToKatakana(romaji); | ||
const hiragana = toKana(input.toLowerCase(), mergedOptions); | ||
return hiraganaToKatakana(hiragana); | ||
} | ||
@@ -34,0 +34,0 @@ |
import mergeWithDefaultOptions from './utils/mergeWithDefaultOptions'; | ||
import toHiragana from './toHiragana'; | ||
import katakanaToHiragana from './utils/katakanaToHiragana'; | ||
import isKatakana from './isKatakana'; | ||
@@ -43,5 +43,5 @@ import { getKanaToRomajiTree } from './utils/kanaToRomajiMap'; | ||
return applyMapping(toHiragana(input, { passRomaji: true }), map, !options.IMEMode); | ||
return applyMapping(katakanaToHiragana(input, toRomaji, true), map, !options.IMEMode); | ||
} | ||
export default toRomaji; |
import { KATAKANA_START, HIRAGANA_START } from '../constants'; | ||
import toRomaji from '../toRomaji'; | ||
@@ -18,15 +17,4 @@ import isCharLongDash from './isCharLongDash'; | ||
/** | ||
* Convert [Katakana](https://en.wikipedia.org/wiki/Katakana) to [Hiragana](https://en.wikipedia.org/wiki/Hiragana) | ||
* Passes through any non-katakana chars | ||
* @param {String} [input=''] text input | ||
* @return {String} converted text | ||
* @example | ||
* katakanaToHiragana('カタカナ') | ||
* // => "かたかな" | ||
* katakanaToHiragana('カタカナ is a type of kana') | ||
* // => "かたかな is a type of kana" | ||
* @ignore | ||
*/ | ||
function katakanaToHiragana(input = '') { | ||
// inject toRomaji to avoid circular dependency between toRomaji <-> katakanaToHiragana | ||
function katakanaToHiragana(input = '', toRomaji, isDestinationRomaji) { | ||
let previousKana = ''; | ||
@@ -42,2 +30,6 @@ | ||
const romaji = toRomaji(previousKana).slice(-1); | ||
// However, ensure 'オー' => 'おお' => 'oo' if this is a transform on the way to romaji | ||
if (isCharKatakana(input[index - 1]) && romaji === 'o' && isDestinationRomaji) { | ||
return hira.concat('お'); | ||
} | ||
return hira.concat(LONG_VOWELS[romaji]); | ||
@@ -44,0 +36,0 @@ } else if (!isCharLongDash(char) && isCharKatakana(char)) { |
{ | ||
"name": "wanakana", | ||
"version": "3.1.0-rc.3", | ||
"version": "3.1.0-rc.4", | ||
"license": "MIT", | ||
@@ -10,3 +10,3 @@ "homepage": "http://www.wanakana.com", | ||
}, | ||
"description": "Utility library for checking and converting between Kanji, Hiragana, Katakana, and Romaji", | ||
"description": "Utility library for converting between Kanji, Hiragana, Katakana, and Romaji", | ||
"keywords": [ | ||
@@ -13,0 +13,0 @@ "english", |
@@ -28,8 +28,4 @@ <div align="center"> | ||
## Documentation | ||
## Usage | ||
[Extended API reference](http://www.wanakana.com/docs/global.html) | ||
## Quick Start | ||
### Browser | ||
@@ -70,26 +66,17 @@ | ||
## Usage | ||
## Documentation | ||
[Extended API reference](http://www.wanakana.com/docs/global.html) | ||
## Quick Reference | ||
```javascript | ||
/*** OPTIONS ***/ | ||
{ | ||
// Use obsolete kana characters, such as ゐ and ゑ. | ||
useObsoleteKana: false, | ||
// Pass through romaji when using toKatakana() or toHiragana() | ||
passRomaji: false, | ||
// Convert katakana to uppercase when using toRomaji() | ||
upcaseKatakana: false, | ||
// Convert characters from a text input while being typed. | ||
IMEMode: false, // alternatives are: true, 'toHiragana', or 'toKatakana' | ||
// Choose toRomaji() romanization map | ||
romanization: 'hepburn' // (currently only hepburn) | ||
// custom map will be merged with default conversion | ||
customKanaMapping: {} | ||
// toKana('wanikani', { customKanaMapping: { na: 'に', ka: 'bana' }) }); | ||
// => 'わにbanaに' | ||
customRomajiMapping: {} | ||
// toRomaji('つじぎり', { customRomajiMapping: { じ: 'zi', つ: 'tu', り: 'li' }) }; | ||
// => 'tuzigili' | ||
} | ||
/*** 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 ***/ | ||
@@ -114,11 +101,2 @@ wanakana.isJapanese('泣き虫。!〜2¥zenkaku') | ||
/* | ||
* toKana notes: | ||
* Lowercase -> Hiragana. | ||
* Uppercase -> Katakana. | ||
* Non-romaji and _English_ punctuation is passed through: 123 @#$% | ||
* Limited Japanese equivalent punctuation is converted: | ||
* !?.:/,~-‘’“”[](){} | ||
* !?。:・、〜ー「」『』[](){} | ||
*/ | ||
wanakana.toKana('ONAJI buttsuuji') | ||
@@ -130,2 +108,4 @@ // => 'オナジ ぶっつうじ' | ||
// => 'ばつげーむ' | ||
wanakana.toKana('wanikani', { customKanaMapping: { na: 'に', ka: 'bana' }) }); | ||
// => 'わにbanaに' | ||
@@ -150,4 +130,5 @@ wanakana.toHiragana('toukyou, オオサカ') | ||
// => 'hiragana KATAKANA' | ||
wanakana.toRomaji('つじぎり', { customRomajiMapping: { じ: 'zi', つ: 'tu', り: 'li' }) }; | ||
// => 'tuzigili' | ||
/*** EXTRA UTILITIES ***/ | ||
@@ -160,9 +141,11 @@ wanakana.stripOkurigana('お祝い') | ||
// => '踏込' | ||
wanakana.stripOkurigana('お祝い', { all: true }) | ||
// => '祝' | ||
wanakana.tokenize('ふふフフ') | ||
// => ['ふふ', 'フフ'] | ||
wanakana.tokenize('感じ') | ||
// => ['感', 'じ'] | ||
wanakana.tokenize('I said "私は悲しい"') | ||
// => ['I said "','私', 'は', '悲', 'しい', '"'] | ||
wanakana.tokenize('hello 田中さん') | ||
// => ['hello', ' ', '田中', 'さん'] | ||
wanakana.tokenize('I said 私はすごく悲しい', { compact: true }) | ||
// => [ 'I said ', '私はすごく悲しい'] | ||
``` | ||
@@ -172,3 +155,3 @@ | ||
Only the browser build via unpkg or `wanakana/umd/*.js` include polyfills for older browsers. | ||
Only the browser build via unpkg or `wanakana/umd/*.js` includes full polyfills for older browsers. | ||
@@ -175,0 +158,0 @@ ## Contributing |
134
wanakana.js
@@ -59,3 +59,3 @@ 'use strict'; | ||
const VERSION = '3.1.0-rc.3'; | ||
const VERSION = '3.1.0-rc.4'; | ||
@@ -870,4 +870,8 @@ const TO_KANA_METHODS = { | ||
input.setAttribute('data-wanakana-id', id); | ||
input.setAttribute('lang', 'ja'); | ||
input.setAttribute('autoCapitalize', 'none'); | ||
input.setAttribute('autoCorrect', 'off'); | ||
input.setAttribute('autoComplete', 'off'); | ||
input.setAttribute('spellCheck', 'false'); | ||
input.addEventListener('input', onInput); | ||
input.addEventListener('compositionstart', onComposition); | ||
input.addEventListener('compositionupdate', onComposition); | ||
@@ -1083,15 +1087,4 @@ input.addEventListener('compositionend', onComposition); | ||
/** | ||
* Convert [Katakana](https://en.wikipedia.org/wiki/Katakana) to [Hiragana](https://en.wikipedia.org/wiki/Hiragana) | ||
* Passes through any non-katakana chars | ||
* @param {String} [input=''] text input | ||
* @return {String} converted text | ||
* @example | ||
* katakanaToHiragana('カタカナ') | ||
* // => "かたかな" | ||
* katakanaToHiragana('カタカナ is a type of kana') | ||
* // => "かたかな is a type of kana" | ||
* @ignore | ||
*/ | ||
function katakanaToHiragana(input = '') { | ||
// inject toRomaji to avoid circular dependency between toRomaji <-> katakanaToHiragana | ||
function katakanaToHiragana(input = '', toRomaji, isDestinationRomaji) { | ||
let previousKana = ''; | ||
@@ -1107,2 +1100,6 @@ | ||
const romaji = toRomaji(previousKana).slice(-1); | ||
// However, ensure 'オー' => 'おお' => 'oo' if this is a transform on the way to romaji | ||
if (isCharKatakana(input[index - 1]) && romaji === 'o' && isDestinationRomaji) { | ||
return hira.concat('お'); | ||
} | ||
return hira.concat(LONG_VOWELS[romaji]); | ||
@@ -1122,60 +1119,2 @@ } else if (!isCharLongDash(char) && isCharKatakana(char)) { | ||
/** | ||
* Convert [Romaji](https://en.wikipedia.org/wiki/Romaji) to [Hiragana](https://en.wikipedia.org/wiki/Hiragana) | ||
* @param {String} [input=''] text | ||
* @param {Object} options used internally to pass along default options | ||
* @return {String} converted text | ||
* @example | ||
* romajiToHiragana('hiragana') | ||
* // => "ひらがな" | ||
* @ignore | ||
*/ | ||
function romajiToHiragana(input = '', options = {}) { | ||
const text = input.toLowerCase(); // ensure hiragana | ||
return toKana(text, options); | ||
} | ||
/** | ||
* Tests a character. Returns true if the character is considered English punctuation. | ||
* @param {String} char character string to test | ||
* @return {Boolean} | ||
*/ | ||
function isCharEnglishPunctuation(char = '') { | ||
if (isEmpty(char)) return false; | ||
return EN_PUNCTUATION_RANGES.some(([start, end]) => isCharInRange(char, start, end)); | ||
} | ||
/** | ||
* Convert input to [Hiragana](https://en.wikipedia.org/wiki/Hiragana) | ||
* @param {String} [input=''] text | ||
* @param {DefaultOptions} [options=defaultOptions] | ||
* @return {String} converted text | ||
* @example | ||
* toHiragana('toukyou, オオサカ') | ||
* // => 'とうきょう、 おおさか' | ||
* toHiragana('only カナ', { passRomaji: true }) | ||
* // => 'only かな' | ||
* toHiragana('wi') | ||
* // => 'うぃ' | ||
* toHiragana('wi', { useObsoleteKana: true }) | ||
* // => 'ゐ' | ||
*/ | ||
function toHiragana(input = '', options = {}) { | ||
const config = mergeWithDefaultOptions(options); | ||
if (config.passRomaji) { | ||
return katakanaToHiragana(input); | ||
} | ||
if (isRomaji(input) || isCharEnglishPunctuation(input)) { | ||
return romajiToHiragana(input, config); | ||
} | ||
if (isMixed(input, { passKanji: true })) { | ||
const romaji = katakanaToHiragana(input); | ||
return romajiToHiragana(romaji, config); | ||
} | ||
return katakanaToHiragana(input); | ||
} | ||
let kanaToHepburnMap = null; | ||
@@ -1409,6 +1348,49 @@ | ||
return applyMapping(toHiragana(input, { passRomaji: true }), map, !options.IMEMode); | ||
return applyMapping(katakanaToHiragana(input, toRomaji, true), map, !options.IMEMode); | ||
} | ||
/** | ||
* Tests a character. Returns true if the character is considered English punctuation. | ||
* @param {String} char character string to test | ||
* @return {Boolean} | ||
*/ | ||
function isCharEnglishPunctuation(char = '') { | ||
if (isEmpty(char)) return false; | ||
return EN_PUNCTUATION_RANGES.some(([start, end]) => isCharInRange(char, start, end)); | ||
} | ||
/** | ||
* Convert input to [Hiragana](https://en.wikipedia.org/wiki/Hiragana) | ||
* @param {String} [input=''] text | ||
* @param {DefaultOptions} [options=defaultOptions] | ||
* @return {String} converted text | ||
* @example | ||
* toHiragana('toukyou, オオサカ') | ||
* // => 'とうきょう、 おおさか' | ||
* toHiragana('only カナ', { passRomaji: true }) | ||
* // => 'only かな' | ||
* toHiragana('wi') | ||
* // => 'うぃ' | ||
* toHiragana('wi', { useObsoleteKana: true }) | ||
* // => 'ゐ' | ||
*/ | ||
function toHiragana(input = '', options = {}) { | ||
const config = mergeWithDefaultOptions(options); | ||
if (config.passRomaji) { | ||
return katakanaToHiragana(input, toRomaji); | ||
} | ||
if (isMixed(input, { passKanji: true })) { | ||
const convertedKatakana = katakanaToHiragana(input, toRomaji); | ||
return toKana(convertedKatakana.toLowerCase(), config); | ||
} | ||
if (isRomaji(input) || isCharEnglishPunctuation(input)) { | ||
return toKana(input.toLowerCase(), config); | ||
} | ||
return katakanaToHiragana(input, toRomaji); | ||
} | ||
/** | ||
* Convert input to [Katakana](https://en.wikipedia.org/wiki/Katakana) | ||
@@ -1435,4 +1417,4 @@ * @param {String} [input=''] text | ||
if (isMixed(input) || isRomaji(input) || isCharEnglishPunctuation(input)) { | ||
const romaji = romajiToHiragana(input, mergedOptions); | ||
return hiraganaToKatakana(romaji); | ||
const hiragana = toKana(input.toLowerCase(), mergedOptions); | ||
return hiraganaToKatakana(hiragana); | ||
} | ||
@@ -1439,0 +1421,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
784051
52
9384
171