Comparing version 0.3.0 to 0.4.0
@@ -67,2 +67,30 @@ var bulkReplace = require("bulk-replace"); | ||
// Used to convert old Nihon-Shiki style romaji into the modern Hepburn form. | ||
// Source: http://nayuki.eigenstate.org/page/variations-on-japanese-romanization | ||
var nihonShiki = { | ||
"SI": "SHI", | ||
"ZI": "JI", | ||
"TI": "CHI", | ||
"DI": "JI", | ||
"TU": "TSU", | ||
"DU": "ZU", | ||
"SHU": "SHU", // Prevent HU from accidentally converting | ||
"HU": "FU", | ||
"CYA": "CHA", | ||
"CYO": "CHO", | ||
"CYU": "CHU", | ||
"SYA": "SHA", | ||
"SYU": "SHU", | ||
"SYO": "SHO", | ||
"ZYA": "JA", | ||
"ZYU": "JU", | ||
"ZYO": "JO", | ||
"TYA": "CHA", | ||
"TYU": "CHU", | ||
"TYO": "CHO", | ||
"DYA": "JA", | ||
"DYU": "JU", | ||
"DYO": "JO" | ||
}; | ||
// For use with toHiragana | ||
@@ -131,3 +159,3 @@ var hiraganaMap = {}; | ||
exports.toHiragana = function(str) { | ||
// For to be uppercase | ||
// All conversion is done in upper-case | ||
str = str.toUpperCase(); | ||
@@ -148,3 +176,3 @@ | ||
exports.toKatakana = function(str) { | ||
// For to be uppercase | ||
// All conversion is done in upper-case | ||
str = str.toUpperCase(); | ||
@@ -164,2 +192,27 @@ | ||
exports.cleanRomaji = function(str) { | ||
// Follows many of the suggestions from: | ||
// http://nayuki.eigenstate.org/page/variations-on-japanese-romanization | ||
// All conversion is done in upper-case | ||
str = str.toUpperCase(); | ||
// Should be using N instead of M | ||
str = str.replace(/(\w)M([^AEIOUY]|$)/g, "$1N$2"); | ||
// Convert the NN form into the more common N' | ||
str = str.replace(/NN/g, "N'"); | ||
// Convert usage of OU into the more common OO | ||
// Handle cases like Toukyou | ||
str = str.replace(/OU/g, "OO"); | ||
// Fix antiquated usage of OH to mean OO | ||
// (handle ambiguous cases like 'Kohusai' vs. 'Tohkyoh') | ||
str = str.replace(/OH([^AIEO]|$)/g, "OO$1"); | ||
// Replace old Nihon-shiki usage with modern Hepburn form | ||
str = bulkReplace(str, nihonShiki); | ||
return str; | ||
}; |
{ | ||
"name": "hepburn", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"main": "./lib/hepburn", | ||
@@ -5,0 +5,0 @@ "description": "Convert Japanese Hiragana and Katakana script to, and from, Romaji using Hepburn romanisation", |
@@ -48,2 +48,19 @@ # Hepburn | ||
### cleanRomaji(string) | ||
```javascript | ||
var cleaned = hepburn.cleanRomaji("SYUNNEI"); | ||
// cleaned === "SHUN'EI" | ||
``` | ||
Cleans up a romaji string, changing old romaji forms into the more-modern | ||
Hepburn form (for further processing). Generally matches the style used by [Wapro romaji](https://en.wikipedia.org/wiki/W%C4%81puro_r%C5%8Dmaji). A larger [guide to modern romaji conventions](http://nayuki.eigenstate.org/page/variations-on-japanese-romanization) was used in building this method. | ||
What this methods fixes: | ||
* Incorrect usage of the letter M. For example "Shumman" should be written as "Shunman". | ||
* Changing usage of NN into N', for example "Shunnei" becomes "Shun'ei". | ||
* Converting the usage of OU and OH (to indicate a long vowel) into OO. | ||
* Correct old usages [Nihon-shiki romanization](https://en.wikipedia.org/wiki/Nihon-shiki_romanization) into Hepburn form. A full list of the conversions can be found in the `hepburn.js` file. For example "Eisyosai" becomes "Eishosai" and "Yoshihuji" becomes "Yoshifuji". | ||
## Testing [](https://travis-ci.org/lovell/hepburn) | ||
@@ -50,0 +67,0 @@ |
@@ -516,2 +516,15 @@ var assert = require("assert"); | ||
var cleanTests = { | ||
"shunsho": "SHUNSHO", | ||
"SYUNMAN": "SHUNMAN", | ||
"SHUMMAN": "SHUNMAN", | ||
"EISYOSAI": "EISHOSAI", | ||
"SYUNNEI": "SHUN'EI", | ||
"OHUCHI": "OOUCHI", | ||
"TOUKYOU": "TOOKYOO", | ||
"TOHKYOH": "TOOKYOO", | ||
"SENCYUSYA": "SENCHUSHA", | ||
"YOSHIHUJI": "YOSHIFUJI" | ||
}; | ||
for (var hiragana in hiraganaTests) { | ||
@@ -534,1 +547,5 @@ assert.equal(hepburn.fromKana(hiragana), hiraganaTests[hiragana], "Hiragana conversion failed on " + hiragana); | ||
} | ||
for (var hiragana in cleanTests) { | ||
assert.equal(hepburn.cleanRomaji(hiragana), cleanTests[hiragana], "Failed to clean " + hiragana); | ||
} |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
24767
717
84
1