Comparing version 3.1.1 to 4.0.0
@@ -27,2 +27,12 @@ # Change Log | ||
## [4.0.0] - 2017-03-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] - 2017-03-26 | ||
@@ -29,0 +39,0 @@ |
@@ -1,2 +0,2 @@ | ||
export const VERSION = '3.1.1'; | ||
export const VERSION = '4.0.0'; | ||
@@ -3,0 +3,0 @@ export const TO_KANA_METHODS = { |
@@ -1,12 +0,14 @@ | ||
import isEmpty from './utils/isEmpty'; | ||
import isCharKana from './utils/isCharKana'; | ||
import isCharPunctuation from './utils/isCharPunctuation'; | ||
import isJapanese from './isJapanese'; | ||
import isKana from './isKana'; | ||
import isKanji from './isKanji'; | ||
import tokenize from './tokenize'; | ||
const leadingWithoutInitialKana = (input, leading) => leading && !isKana(input[0]); | ||
const trailingWithoutFinalKana = (input, leading) => !leading && !isKana(input[input.length - 1]); | ||
const inValidMatcher = (input, matchKanji) => matchKanji && ![...matchKanji].some(isKanji) || !matchKanji && isKana(input); | ||
/** | ||
* Strips trailing [Okurigana](https://en.wikipedia.org/wiki/Okurigana) if `input` is a mix of [Kanji](https://en.wikipedia.org/wiki/Kanji) and [Kana](https://en.wikipedia.org/wiki/Kana) | ||
* Strips [Okurigana](https://en.wikipedia.org/wiki/Okurigana) | ||
* @param {String} input text | ||
* @param {Object} [options={ all: false }] config object specifying if *all* kana should be removed, not just trailing okurigana | ||
* @param {Object} [options={ leading: false, matchKanji: '' }] optional config | ||
* @return {String} text with okurigana removed | ||
@@ -16,37 +18,21 @@ * @example | ||
* // => '踏み込' | ||
* stripOkurigana('粘り。') | ||
* // => '粘。' | ||
* stripOkurigana('お祝い') | ||
* // => 'お祝' | ||
* stripOkurigana('踏み込む', { all: true }) | ||
* // => '踏込' | ||
* stripOkurigana('お祝い', { all: true }) | ||
* // => '祝' | ||
* stripOkurigana('お腹', { leading: true }); | ||
* // => '腹' | ||
* stripOkurigana('ふみこむ', { matchKanji: '踏み込む' }); | ||
* // => 'ふみこ' | ||
* stripOkurigana('おみまい', { matchKanji: 'お祝い', leading: true }); | ||
* // => 'みまい' | ||
*/ | ||
function stripOkurigana(input = '', options = { all: false }) { | ||
if (isEmpty(input) || !isJapanese(input) || isKana(input)) { | ||
function stripOkurigana(input = '', { leading = false, matchKanji = '' } = {}) { | ||
if (!isJapanese(input) || leadingWithoutInitialKana(input, leading) || trailingWithoutFinalKana(input, leading) || inValidMatcher(input, matchKanji)) { | ||
return input; | ||
} | ||
const chars = [...input]; | ||
// strip every kana | ||
if (options.all) { | ||
return chars.filter(char => !isCharKana(char)).join(''); | ||
} | ||
// strip trailing only | ||
const reverseChars = chars.reverse(); | ||
for (let i = 0, len = reverseChars.length; i < len; i += 1) { | ||
const char = reverseChars[i]; | ||
// pass if it's punctuation | ||
if (isCharPunctuation(char)) continue; // eslint-disable-line no-continue | ||
// blank out if not kanji | ||
if (!isKanji(char)) { | ||
reverseChars[i] = ''; | ||
} else break; // stop when we hit a kanji char | ||
} | ||
return reverseChars.reverse().join(''); | ||
const chars = matchKanji || input; | ||
const toMatch = leading ? [...chars].reverse().join() : chars; | ||
return input.replace(tokenize(toMatch).pop(), ''); | ||
} | ||
export default stripOkurigana; |
{ | ||
"name": "wanakana", | ||
"version": "3.1.1", | ||
"version": "4.0.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "homepage": "http://www.wanakana.com", |
@@ -134,6 +134,8 @@ <div align="center"> | ||
// => '踏み込' | ||
wanakana.stripOkurigana('踏み込む', { all: true }) | ||
// => '踏込' | ||
wanakana.stripOkurigana('お祝い', { all: true }) | ||
// => '祝' | ||
wanakana.stripOkurigana('お腹', { leading: true }); | ||
// => '腹' | ||
wanakana.stripOkurigana('ふみこむ', { matchKanji: '踏み込む' }); | ||
// => 'ふみこ' | ||
wanakana.stripOkurigana('おみまい', { matchKanji: 'お祝い', leading: true }); | ||
// => 'みまい' | ||
@@ -140,0 +142,0 @@ wanakana.tokenize('ふふフフ') |
@@ -59,3 +59,3 @@ 'use strict'; | ||
const VERSION = '3.1.1'; | ||
const VERSION = '4.0.0'; | ||
@@ -1439,55 +1439,2 @@ const TO_KANA_METHODS = { | ||
/** | ||
* Tests a character. Returns true if the character is considered Japanese or English punctuation. | ||
* @param {String} char character string to test | ||
* @return {Boolean} | ||
*/ | ||
function isCharPunctuation(char = '') { | ||
if (isEmpty(char)) return false; | ||
return isCharEnglishPunctuation(char) || isCharJapanesePunctuation(char); | ||
} | ||
/** | ||
* Strips trailing [Okurigana](https://en.wikipedia.org/wiki/Okurigana) if `input` is a mix of [Kanji](https://en.wikipedia.org/wiki/Kanji) and [Kana](https://en.wikipedia.org/wiki/Kana) | ||
* @param {String} input text | ||
* @param {Object} [options={ all: false }] config object specifying if *all* kana should be removed, not just trailing okurigana | ||
* @return {String} text with okurigana removed | ||
* @example | ||
* stripOkurigana('踏み込む') | ||
* // => '踏み込' | ||
* stripOkurigana('粘り。') | ||
* // => '粘。' | ||
* stripOkurigana('お祝い') | ||
* // => 'お祝' | ||
* stripOkurigana('踏み込む', { all: true }) | ||
* // => '踏込' | ||
* stripOkurigana('お祝い', { all: true }) | ||
* // => '祝' | ||
*/ | ||
function stripOkurigana(input = '', options = { all: false }) { | ||
if (isEmpty(input) || !isJapanese(input) || isKana(input)) { | ||
return input; | ||
} | ||
const chars = [...input]; | ||
// strip every kana | ||
if (options.all) { | ||
return chars.filter(char => !isCharKana(char)).join(''); | ||
} | ||
// strip trailing only | ||
const reverseChars = chars.reverse(); | ||
for (let i = 0, len = reverseChars.length; i < len; i += 1) { | ||
const char = reverseChars[i]; | ||
// pass if it's punctuation | ||
if (isCharPunctuation(char)) continue; // eslint-disable-line no-continue | ||
// blank out if not kanji | ||
if (!isKanji(char)) { | ||
reverseChars[i] = ''; | ||
} else break; // stop when we hit a kanji char | ||
} | ||
return reverseChars.reverse().join(''); | ||
} | ||
const isCharEnSpace = x => x === ' '; | ||
@@ -1653,2 +1600,33 @@ const isCharJaSpace = x => x === ' '; | ||
const leadingWithoutInitialKana = (input, leading) => leading && !isKana(input[0]); | ||
const trailingWithoutFinalKana = (input, leading) => !leading && !isKana(input[input.length - 1]); | ||
const inValidMatcher = (input, matchKanji) => matchKanji && ![...matchKanji].some(isKanji) || !matchKanji && isKana(input); | ||
/** | ||
* Strips [Okurigana](https://en.wikipedia.org/wiki/Okurigana) | ||
* @param {String} input text | ||
* @param {Object} [options={ leading: false, matchKanji: '' }] optional config | ||
* @return {String} text with okurigana removed | ||
* @example | ||
* stripOkurigana('踏み込む') | ||
* // => '踏み込' | ||
* stripOkurigana('お祝い') | ||
* // => 'お祝' | ||
* stripOkurigana('お腹', { leading: true }); | ||
* // => '腹' | ||
* stripOkurigana('ふみこむ', { matchKanji: '踏み込む' }); | ||
* // => 'ふみこ' | ||
* stripOkurigana('おみまい', { matchKanji: 'お祝い', leading: true }); | ||
* // => 'みまい' | ||
*/ | ||
function stripOkurigana(input = '', { leading = false, matchKanji = '' } = {}) { | ||
if (!isJapanese(input) || leadingWithoutInitialKana(input, leading) || trailingWithoutFinalKana(input, leading) || inValidMatcher(input, matchKanji)) { | ||
return input; | ||
} | ||
const chars = matchKanji || input; | ||
const toMatch = leading ? [...chars].reverse().join() : chars; | ||
return input.replace(tokenize(toMatch).pop(), ''); | ||
} | ||
exports.bind = bind; | ||
@@ -1655,0 +1633,0 @@ exports.unbind = unbind; |
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
174
825681
7904
10090