Skip to content

Instantly share code, notes, and snippets.

@timw4mail
Created October 20, 2011 19:15
Show Gist options
  • Save timw4mail/1302022 to your computer and use it in GitHub Desktop.
Save timw4mail/1302022 to your computer and use it in GitHub Desktop.
Javascript Kana transliterator
document.getElementById('sub').addEventListener('click', transliterate, false);
function transliterate()
{
var input = document.getElementById('in').value;
var action = document.getElementById('action').value;
var output;
switch(action)
{
case "to_hira":
output = trans.hiragana(input);
break;
case "to_kata":
output = trans.katakana(input);
break;
case "from_kana":
output = trans.from_kana(input);
break;
}
document.getElementById('output').innerHTML = output;
}
var trans = {
roman: {
hiragana: {
//vowels
a: 'あ',
i: 'い',
u: 'う',
e: 'え',
o: 'お',
//k
ka: 'か',
ki: 'き',
ku: 'く',
ke: 'け',
ko: 'こ',
ca: 'か',
ci: 'き',
cu: 'く',
ce: 'け',
co: 'こ',
//g
ga: 'が',
gi: 'ぎ',
gu: 'ぐ',
ge: 'げ',
go: 'ご',
//s
sa: 'さ',
shi: 'し',
si: 'し',
su: 'す',
se: 'せ',
so: 'そ',
//j
ja: 'じゃ',
ji: 'じ',
ju: 'じゅ',
je: 'じぇ',
jo: 'じょ',
//z
za: 'ざ',
zi: 'じ',
zu: 'ず',
ze: 'ぜ',
zo: 'ぞ',
//t
ta: 'た',
chi: 'ち',
ti: 'ち',
tsu: 'つ',
tu: 'つ',
te: 'て',
to: 'と',
//d
da: 'だ',
di: 'ぢ',
du: 'づ',
de: 'で',
'do': 'ど',
//n
na: 'な',
ni: 'に',
nu: 'ぬ',
ne: 'ね',
no: 'の',
//h
ha: 'は',
hi: 'ひ',
hu: 'ふ',
fu: 'ふ',
he: 'へ',
ho: 'ほ',
//b
ba: 'ば',
bi: 'び',
bu: 'ぶ',
be: 'べ',
bo: 'ぼ',
//p
pa: 'ぱ',
pi: 'ぴ',
pu: 'ぷ',
pe: 'ぺ',
po: 'ぽ',
//m
ma: 'ま',
mi: 'み',
mu: 'む',
me: 'め',
mo: 'も',
//y
ya: 'や',
yu: 'ゆ',
yo: 'よ',
//r
ra: 'ら',
ri: 'り',
ru: 'る',
re: 'れ',
ro: 'ろ',
//w
wa: 'わ',
wo: 'を',
n: 'ん',
//compound
kya: 'きゃ',
kyu: 'きゅ',
kyo: 'きょ',
gya: 'ぎゃ',
gyu: 'ぎゅ',
gyo: 'ぎょ',
sha: 'しゃ',
shu: 'しゅ',
sho: 'しょ',
cha: 'ちゃ',
chu: 'ちゅ',
cho: 'ちょ',
nya: 'にゃ',
nyu: 'にゅ',
nyo: 'にょ',
hya: 'ひゃ',
hyu: 'ひゅ',
hyo: 'ひょ',
bya: 'びゃ',
byu: 'びゅ',
byo: 'びょ',
pya: 'ぴゃ',
pyu: 'ぴゅ',
pyo: 'ぴょ',
mya: 'みゃ',
myu: 'みゅ',
myo: 'みょ',
rya: 'りゃ',
ryu: 'りゅ',
ryo: 'りょ'
},
hira_repeated: {
kk: 'っk',
ss: 'っs',
tt: 'っt',
nn: 'っn',
hh: 'っh',
mm: 'っm',
yy: 'っy',
rr: 'っr',
ww: 'っw',
gg: 'っg',
zz: 'っz',
jj: 'っj',
dd: 'っd',
bb: 'っb',
pp: 'っp',
oo: 'oう',
oh: 'oう'
},
katakana: {
//vowels
a: 'ア',
i: 'イ',
u: 'ウ',
e: 'エ',
o: 'オ',
//k
ca: 'カ',
ci: 'キ',
cu: 'ク',
ce: 'ケ',
co: 'コ',
ka: 'カ',
ki: 'キ',
ku: 'ク',
ke: 'ケ',
ko: 'コ',
//g
ga: 'ガ',
gi: 'ギ',
gu: 'グ',
ge: 'ゲ',
go: 'ゴ',
//s
sa: 'サ',
shi: 'シ',
si: 'シ',
su: 'ス',
se: 'セ',
so: 'ソ',
//j
ja: 'ジャ',
ji: 'ジ',
ju: 'ジュ',
je: 'ジェ',
jo: 'ジョ',
//z
za: 'ザ',
zi: 'ジ',
zu: 'ズ',
ze: 'ゼ',
zo: 'ゾ',
//t
ta: 'タ',
chi: 'チ',
ti: 'チ',
tu: 'ツ',
tsu: 'ツ',
te: 'テ',
to: 'ト',
//d
da: 'ダ',
di: 'ヂ',
du: 'ヅ',
de: 'デ',
'do': 'ド',
//n
na: 'ナ',
ni: 'ニ',
nu: 'ヌ',
ne: 'ネ',
no: 'ノ',
//h
ha: 'ハ',
hi: 'ヒ',
hu: 'フ',
fu: 'フ',
he: 'ヘ',
ho: 'ホ',
//b
ba: 'バ',
bi: 'ビ',
bu: 'ブ',
be: 'ベ',
bo: 'ボ',
//p
pa: 'パ',
pi: 'ピ',
pu: 'プ',
pe: 'ペ',
po: 'ポ',
//m
ma: 'マ',
mi: 'ミ',
mu: 'ム',
me: 'メ',
mo: 'モ',
//y
ya: 'ヤ',
yu: 'ユ',
yo: 'ヨ',
//r
ra: 'ラ',
ri: 'リ',
ru: 'ル',
re: 'レ',
ro: 'ロ',
//w
wa: 'ワ',
wo: 'ヲ',
n: 'ン',
//compound
kya: 'キャ',
kyu: 'キュ',
kyo: 'キョ',
gya: 'ギャ',
gyu: 'ギュ',
gyo: 'ギョ',
sha: 'シャ',
shu: 'シュ',
sho: 'ショ',
cha: 'チャ',
chu: 'チュ',
cho: 'チョ',
nya: 'ニャ',
nyu: 'ニュ',
nyo: 'ニョ',
hya: 'ヒャ',
hyu: 'ヒュ',
hyo: 'ヒョ',
bya: 'ビャ',
byu: 'ビュ',
byo: 'ビョ',
pya: 'ピャ',
pyu: 'ピュ',
pyo: 'ピョ',
mya: 'ミャ',
myu: 'ミュ',
myo: 'ミョ',
rya: 'リャ',
ryu: 'リュ',
ryo: 'リョ'
},
kata_repeated: {
kk: 'ッk',
ss: 'ッs',
tt: 'ッt',
nn: 'ッn',
hh: 'ッh',
mm: 'ッm',
yy: 'ッy',
rr: 'ッr',
ww: 'ッw',
gg: 'ッg',
zz: 'ッz',
jj: 'ッj',
dd: 'ッd',
bb: 'ッb',
pp: 'ッp',
aa: 'aー',
ii: 'iー',
uu: 'uー',
ee: 'eー',
oo: 'oー',
oh: 'oー'
},
kata_only: {
va: 'ヴァ',
fa: 'ファ',
wi: 'ウィ',
vi: 'ヴィ',
fi: 'フィ',
vu: 'ヴ',
ye: 'イェ',
we: 'ウェ',
ve: 'ヴェ',
fe: 'フェ',
vo: 'ヴォ',
fo: 'フォ',
kwa: 'クァ',
gwa: 'グァ',
tsa: 'ツァ',
kwi: 'クィ',
gwi: 'グィ',
tsi: 'ツィ',
thi: 'ティ',
kwe: 'クェ',
gwe: 'グェ',
she: 'シェ',
che: 'チェ',
tse: 'ツェ',
kwo: 'クォ',
gwo: 'グォ',
tso: 'ツォ',
vya: 'ヴャ',
tha: 'テャ',
dya: 'ヂャ',
fya: 'フャ',
vyu: 'ヴュ',
thu: 'テュ',
dyu: 'ヂュ',
fyu: 'フュ',
vyo: 'ヴョ',
tho: 'テョ',
dyo: 'ヂョ',
fyo: 'フョ'
}
},
kana: {
hiragana: {
//vowels
あ: 'a',
い: 'i',
う: 'u',
え: 'e',
お: 'o',
//k
か: 'ka',
き: 'ki',
く: 'ku',
け: 'ke',
こ: 'ko',
//g
が: 'ga',
ぎ: 'gi',
ぐ: 'gu',
げ: 'ge',
ご: 'go',
//s
さ: 'sa',
し: 'shi',
す: 'su',
せ: 'se',
そ: 'so',
//j
じゃ: 'ja',
じ: 'ji',
ぢ: 'ji',
じゅ: 'ju',
じぇ: 'je',
じょ: 'jo',
//z
ざ: 'za',
ず: 'zu',
ぜ: 'ze',
ぞ: 'zo',
//t
た: 'ta',
ち: 'chi',
つ: 'tsu',
て: 'te',
と: 'to',
//d
だ: 'da',
づ: 'du',
で: 'de',
ど: 'do',
//n
な: 'na',
に: 'ni',
ぬ: 'nu',
ね: 'ne',
の: 'no',
//h
は: 'ha',
ひ: 'hi',
ふ: 'fu',
へ: 'he',
ほ: 'ho',
//b
ば: 'ba',
び: 'bi',
ぶ: 'bu',
べ: 'be',
ぼ: 'bo',
//p
ぱ: 'pa',
ぴ: 'pi',
ぷ: 'pu',
ぺ: 'pe',
ぽ: 'po',
//m
ま: 'ma',
み: 'mi',
む: 'mu',
め: 'me',
も: 'mo',
//y
や: 'ya',
ゆ: 'yu',
よ: 'yo',
//r
ら: 'ra',
り: 'ri',
る: 'ru',
れ: 're',
ろ: 'ro',
//w
わ: 'wa',
を: 'wo',
ん: 'n',
//compound
きゃ: 'kya',
きゅ: 'kyu',
きょ: 'kyo',
ぎゃ: 'gya',
ぎゅ: 'gyu',
ぎょ: 'gyo',
しゃ: 'sha',
しゅ: 'shu',
しょ: 'sho',
ちゃ: 'cha',
ちゅ: 'chu',
ちょ: 'cho',
にゃ: 'nya',
にゅ: 'nyu',
にょ: 'nyo',
ひゃ: 'hya',
ひゅ: 'hyu',
ひょ: 'hyo',
びゃ: 'bya',
びゅ: 'byu',
びょ: 'byo',
ぴゃ: 'pya',
ぴゅ: 'pyu',
ぴょ: 'pyo',
みゃ: 'mya',
みゅ: 'myu',
みょ: 'myo',
りゃ: 'rya',
りゅ: 'ryu',
りょ: 'ryo'
},
hira_repeated: {
っk: 'kk',
っs: 'ss',
っt: 'tt',
っn: 'nn',
っh: 'hh',
っm: 'mm',
っy: 'yy',
っr: 'rr',
っw: 'ww',
っg: 'gg',
っz: 'zz',
っj: 'jj',
っd: 'dd',
っb: 'bb',
っp: 'pp'
},
katakana: {
//vowels
ア: 'a',
イ: 'i',
ウ: 'u',
エ: 'e',
オ: 'o',
//k
カ: 'ka',
キ: 'ki',
ク: 'ku',
ケ: 'ke',
コ: 'ko',
//g
ガ: 'ga',
ギ: 'gi',
グ: 'gu',
ゲ: 'ge',
ゴ: 'go',
//s
サ: 'sa',
シ: 'shi',
ス: 'su',
セ: 'se',
ソ: 'so',
//j
ジャ: 'ja',
ジ: 'ji',
ヂ: 'ji',
ジュ: 'ju',
ジェ: 'je',
ジョ: 'jo',
//z
ザ: 'za',
ズ: 'zu',
ゼ: 'ze',
ゾ: 'zo',
//t
タ: 'ta',
チ: 'chi',
ツ: 'tsu',
テ: 'te',
ト: 'to',
//d
ダ: 'da',
ヅ: 'du',
デ: 'de',
ド: 'do',
//n
ナ: 'na',
ニ: 'ni',
ヌ: 'nu',
ネ: 'ne',
ノ: 'no',
//h
ハ: 'ha',
ヒ: 'hi',
フ: 'fu',
ヘ: 'he',
ホ: 'ho',
//b
バ: 'ba',
ビ: 'bi',
ブ: 'bu',
ベ: 'be',
ボ: 'bo',
//p
パ: 'pa',
ピ: 'pi',
プ: 'pu',
ペ: 'pe',
ポ: 'po',
//m
マ: 'ma',
ミ: 'mi',
ム: 'mu',
メ: 'me',
モ: 'mo',
//y
ヤ: 'ya',
ユ: 'yu',
ヨ: 'yo',
//r
ラ: 'ra',
リ: 'ri',
ル: 'ru',
レ: 're',
ロ: 'ro',
//w
ワ: 'wa',
ヲ: 'wo',
ン: 'n',
//compound
キャ: 'kya',
キュ: 'kyu',
キョ: 'kyo',
ギャ: 'gya',
ギュ: 'gyu',
ギョ: 'gyo',
シャ: 'sha',
シュ: 'shu',
ショ: 'sho',
チャ: 'cha',
チュ: 'chu',
チョ: 'cho',
ニャ: 'nya',
ニュ: 'nyu',
ニョ: 'nyo',
ヒャ: 'hya',
ヒュ: 'hyu',
ヒョ: 'hyo',
ビャ: 'bya',
ビュ: 'byu',
ビョ: 'byo',
ピャ: 'pya',
ピュ: 'pyu',
ピョ: 'pyo',
ミャ: 'mya',
ミュ: 'myu',
ミョ: 'myo',
リャ: 'rya',
リュ: 'ryu',
リョ: 'ryo'
},
kata_repeated: {
ッk: 'kk',
ッs: 'ss',
ッt: 'tt',
ッn: 'nn',
ッh: 'hh',
ッm: 'mm',
ッy: 'yy',
ッr: 'rr',
ッw: 'ww',
ッg: 'gg',
ッz: 'zz',
ッj: 'jj',
ッd: 'dd',
ッb: 'bb',
ッp: 'pp',
aー: 'aa',
iー: 'ii',
uー: 'uu',
eー: 'ee',
oー: 'oo',
oー: 'oh'
},
kata_only: {
ヴァ: 'va',
ファ: 'fa',
ウィ: 'wi',
ヴィ: 'vi',
フィ: 'fi',
ヴ: 'vu',
イェ: 'ye',
ウェ: 'we',
ヴェ: 've',
フェ: 'fe',
ヴォ: 'vo',
フォ: 'fo',
クァ: 'kwa',
グァ: 'gwa',
ツァ: 'tsa',
クィ: 'kwi',
グィ: 'gwi',
ツィ: 'tsi',
ティ: 'thi',
クェ: 'kwe',
グェ: 'gwe',
シェ: 'she',
チェ: 'che',
ツェ: 'tse',
クォ: 'kwo',
グォ: 'gwo',
ツォ: 'tso',
ヴャ: 'vya',
テャ: 'tha',
ヂャ: 'dya',
フャ: 'fya',
ヴュ: 'vyu',
テュ: 'thu',
ヂュ: 'dyu',
フュ: 'fyu',
ヴョ: 'vyo',
テョ: 'tho',
ヂョ: 'dyo',
フョ: 'fyo'
}
},
hiragana: function(txt)
{
//Replace katakana with hiragana
txt = strtr(txt, this.kana_to_kana.kata_hira);
return this._to_kana(txt, this.roman.hira_repeated, this.roman.hiragana);
},
katakana: function(txt)
{
//Replace hiragana with katakana
txt = strtr(txt, this.kana_to_kana.hira_kata);
return this._to_kana(txt, this.roman.kata_repeated, this.roman.katakana);
},
from_kana: function(txt)
{
//Merge arrays
var pairs = array_merge(this.kana.hiragana, this.kana.katakana, this.kana.kata_only);
//Replace normal characters
txt = strtr(txt, pairs);
//Replace doubled letters
txt = strtr(txt, array_merge(this.kana.hira_repeated, this.kana.kata_repeated));
return txt;
},
_to_kana: function(txt, repeated, pairs)
{
//Replace doubled letters
var out = str_replace(array_keys(repeated), array_values(repeated), txt);
//Add in katakana only combinations
pairs = array_merge(pairs, this.roman.kata_only);
//Replace everything else
out = strtr(out, pairs);
return out;
}
};
trans.kana_to_kana = {
hira_kata: array_combine(
array_keys(trans.kana.hiragana),
array_keys(trans.kana.katakana)
),
kata_hira: array_combine(
array_keys(trans.kana.katakana),
array_keys(trans.kana.hiragana)
)
};
// --------------------------------------------------------------------------
function ini_set (varname, newvalue) {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brett-zamir.me)
// % note 1: This will not set a global_value or access level for the ini item
// * example 1: ini_set('date.timezone', 'America/Chicago');
// * returns 1: 'Asia/Hong_Kong'
var oldval = '',
that = this;
this.php_js = this.php_js || {};
this.php_js.ini = this.php_js.ini || {};
this.php_js.ini[varname] = this.php_js.ini[varname] || {};
oldval = this.php_js.ini[varname].local_value;
var _setArr = function (oldval) { // Although these are set individually, they are all accumulated
if (typeof oldval === 'undefined') {
that.php_js.ini[varname].local_value = [];
}
that.php_js.ini[varname].local_value.push(newvalue);
};
switch (varname) {
case 'extension':
if (typeof this.dl === 'function') {
this.dl(newvalue); // This function is only experimental in php.js
}
_setArr(oldval, newvalue);
break;
default:
this.php_js.ini[varname].local_value = newvalue;
break;
}
return oldval;
}
function in_array (needle, haystack, argStrict) {
// Checks if the given value exists in the array
//
// version: 1109.2015
// discuss at: http://phpjs.org/functions/in_array
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: vlado houba
// + input by: Billy
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// * example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
// * returns 1: true
// * example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
// * returns 2: false
// * example 3: in_array(1, ['1', '2', '3']);
// * returns 3: true
// * example 3: in_array(1, ['1', '2', '3'], false);
// * returns 3: true
// * example 4: in_array(1, ['1', '2', '3'], true);
// * returns 4: false
var key = '',
strict = !! argStrict;
if (strict) {
for (key in haystack) {
if (haystack[key] === needle) {
return true;
}
}
} else {
for (key in haystack) {
if (haystack[key] == needle) {
return true;
}
}
}
return false;
}
function krsort (inputArr, sort_flags) {
// http://kevin.vanzonneveld.net
// + original by: GeekFG (http://geekfg.blogspot.com)
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Brett Zamir (http://brett-zamir.me)
// % note 1: The examples are correct, this is a new way
// % note 2: This function deviates from PHP in returning a copy of the array instead
// % note 2: of acting by reference and returning true; this was necessary because
// % note 2: IE does not allow deleting and re-adding of properties without caching
// % note 2: of property position; you can set the ini of "phpjs.strictForIn" to true to
// % note 2: get the PHP behavior, but use this only if you are in an environment
// % note 2: such as Firefox extensions where for-in iteration order is fixed and true
// % note 2: property deletion is supported. Note that we intend to implement the PHP
// % note 2: behavior by default if IE ever does allow it; only gives shallow copy since
// % note 2: is by reference in PHP anyways
// % note 3: Since JS objects' keys are always strings, and (the
// % note 3: default) SORT_REGULAR flag distinguishes by key type,
// % note 3: if the content is a numeric string, we treat the
// % note 3: "original type" as numeric.
// - depends on: i18n_loc_get_default
// * example 1: data = {d: 'lemon', a: 'orange', b: 'banana', c: 'apple'};
// * example 1: data = krsort(data);
// * results 1: {d: 'lemon', c: 'apple', b: 'banana', a: 'orange'}
// * example 2: ini_set('phpjs.strictForIn', true);
// * example 2: data = {2: 'van', 3: 'Zonneveld', 1: 'Kevin'};
// * example 2: krsort(data);
// * results 2: data == {3: 'Kevin', 2: 'van', 1: 'Zonneveld'}
// * returns 2: true
var tmp_arr = {},
keys = [],
sorter, i, k, that = this,
strictForIn = false,
populateArr = {};
switch (sort_flags) {
case 'SORT_STRING':
// compare items as strings
sorter = function (a, b) {
return that.strnatcmp(b, a);
};
break;
case 'SORT_LOCALE_STRING':
// compare items as strings, based on the current locale (set with i18n_loc_set_default() as of PHP6)
var loc = this.i18n_loc_get_default();
sorter = this.php_js.i18nLocales[loc].sorting;
break;
case 'SORT_NUMERIC':
// compare items numerically
sorter = function (a, b) {
return (b - a);
};
break;
case 'SORT_REGULAR':
// compare items normally (don't change types)
default:
sorter = function (b, a) {
var aFloat = parseFloat(a),
bFloat = parseFloat(b),
aNumeric = aFloat + '' === a,
bNumeric = bFloat + '' === b;
if (aNumeric && bNumeric) {
return aFloat > bFloat ? 1 : aFloat < bFloat ? -1 : 0;
} else if (aNumeric && !bNumeric) {
return 1;
} else if (!aNumeric && bNumeric) {
return -1;
}
return a > b ? 1 : a < b ? -1 : 0;
};
break;
}
// Make a list of key names
for (k in inputArr) {
if (inputArr.hasOwnProperty(k)) {
keys.push(k);
}
}
keys.sort(sorter);
// BEGIN REDUNDANT
this.php_js = this.php_js || {};
this.php_js.ini = this.php_js.ini || {};
// END REDUNDANT
strictForIn = this.php_js.ini['phpjs.strictForIn'] && this.php_js.ini['phpjs.strictForIn'].local_value && this.php_js.ini['phpjs.strictForIn'].local_value !== 'off';
populateArr = strictForIn ? inputArr : populateArr;
// Rebuild array with sorted key names
for (i = 0; i < keys.length; i++) {
k = keys[i];
tmp_arr[k] = inputArr[k];
if (strictForIn) {
delete inputArr[k];
}
}
for (i in tmp_arr) {
if (tmp_arr.hasOwnProperty(i)) {
populateArr[i] = tmp_arr[i];
}
}
return strictForIn || populateArr;
}
function strtr (str, from, to) {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brett-zamir.me)
// + input by: uestla
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Alan C
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Taras Bogach
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// + input by: jpfle
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// - depends on: krsort
// - depends on: ini_set
// * example 1: $trans = {'hello' : 'hi', 'hi' : 'hello'};
// * example 1: strtr('hi all, I said hello', $trans)
// * returns 1: 'hello all, I said hi'
// * example 2: strtr('äaabaåccasdeöoo', 'äåö','aao');
// * returns 2: 'aaabaaccasdeooo'
// * example 3: strtr('ääääääää', 'ä', 'a');
// * returns 3: 'aaaaaaaa'
// * example 4: strtr('http', 'pthxyz','xyzpth');
// * returns 4: 'zyyx'
// * example 5: strtr('zyyx', 'pthxyz','xyzpth');
// * returns 5: 'http'
// * example 6: strtr('aa', {'a':1,'aa':2});
// * returns 6: '2'
var fr = '',
i = 0,
j = 0,
lenStr = 0,
lenFrom = 0,
tmpStrictForIn = false,
fromTypeStr = '',
toTypeStr = '',
istr = '';
var tmpFrom = [];
var tmpTo = [];
var ret = '';
var match = false;
// Received replace_pairs?
// Convert to normal from->to chars
if (typeof from === 'object') {
tmpStrictForIn = this.ini_set('phpjs.strictForIn', false); // Not thread-safe; temporarily set to true
from = this.krsort(from);
//this.ini_set('phpjs.strictForIn', tmpStrictForIn);
for (fr in from) {
if (from.hasOwnProperty(fr)) {
tmpFrom.push(fr);
tmpTo.push(from[fr]);
}
}
from = tmpFrom;
to = tmpTo;
}
// Walk through subject and replace chars when needed
lenStr = str.length;
lenFrom = from.length;
fromTypeStr = typeof from === 'string';
toTypeStr = typeof to === 'string';
for (i = 0; i < lenStr; i++) {
match = false;
if (fromTypeStr) {
istr = str.charAt(i);
for (j = 0; j < lenFrom; j++) {
if (istr == from.charAt(j)) {
match = true;
break;
}
}
} else {
for (j = 0; j < lenFrom; j++) {
if (str.substr(i, from[j].length) == from[j]) {
match = true;
// Fast forward
i = (i + from[j].length) - 1;
break;
}
}
}
if (match) {
ret += toTypeStr ? to.charAt(j) : to[j];
} else {
ret += str.charAt(i);
}
}
return ret;
}
function array_combine (keys, values) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Brett Zamir (http://brett-zamir.me)
// * example 1: array_combine([0,1,2], ['kevin','van','zonneveld']);
// * returns 1: {0: 'kevin', 1: 'van', 2: 'zonneveld'}
var new_array = {},
keycount = keys && keys.length,
i = 0;
// input sanitation
if (typeof keys !== 'object' || typeof values !== 'object' || // Only accept arrays or array-like objects
typeof keycount !== 'number' || typeof values.length !== 'number' || !keycount) { // Require arrays to have a count
return false;
}
// number of elements does not match
if (keycount != values.length) {
return false;
}
for (i = 0; i < keycount; i++) {
new_array[keys[i]] = values[i];
}
return new_array;
}
function str_replace (search, replace, subject, count) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Gabriel Paderni
// + improved by: Philip Peterson
// + improved by: Simon Willison (http://simonwillison.net)
// + revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// + bugfixed by: Anton Ongson
// + input by: Onno Marsman
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + tweaked by: Onno Marsman
// + input by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Oleg Eremeev
// + improved by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Oleg Eremeev
// % note 1: The count parameter must be passed as a string in order
// % note 1: to find a global variable in which the result will be given
// * example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
// * returns 1: 'Kevin.van.Zonneveld'
// * example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
// * returns 2: 'hemmo, mars'
var i = 0,
j = 0,
temp = '',
repl = '',
sl = 0,
fl = 0,
f = [].concat(search),
r = [].concat(replace),
s = subject,
ra = Object.prototype.toString.call(r) === '[object Array]',
sa = Object.prototype.toString.call(s) === '[object Array]';
s = [].concat(s);
if (count) {
this.window[count] = 0;
}
for (i = 0, sl = s.length; i < sl; i++) {
if (s[i] === '') {
continue;
}
for (j = 0, fl = f.length; j < fl; j++) {
temp = s[i] + '';
repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
s[i] = (temp).split(f[j]).join(repl);
if (count && s[i] !== temp) {
this.window[count] += (temp.length - s[i].length) / f[j].length;
}
}
}
return sa ? s : s[0];
}
function array_merge () {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Nate
// + input by: josh
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// * example 1: arr1 = {"color": "red", 0: 2, 1: 4}
// * example 1: arr2 = {0: "a", 1: "b", "color": "green", "shape": "trapezoid", 2: 4}
// * example 1: array_merge(arr1, arr2)
// * returns 1: {"color": "green", 0: 2, 1: 4, 2: "a", 3: "b", "shape": "trapezoid", 4: 4}
// * example 2: arr1 = []
// * example 2: arr2 = {1: "data"}
// * example 2: array_merge(arr1, arr2)
// * returns 2: {0: "data"}
var args = Array.prototype.slice.call(arguments),
argl = args.length,
arg,
retObj = {},
k = '',
argil = 0,
j = 0,
i = 0,
ct = 0,
toStr = Object.prototype.toString,
retArr = true;
for (i = 0; i < argl; i++) {
if (toStr.call(args[i]) !== '[object Array]') {
retArr = false;
break;
}
}
if (retArr) {
retArr = [];
for (i = 0; i < argl; i++) {
retArr = retArr.concat(args[i]);
}
return retArr;
}
for (i = 0, ct = 0; i < argl; i++) {
arg = args[i];
if (toStr.call(arg) === '[object Array]') {
for (j = 0, argil = arg.length; j < argil; j++) {
retObj[ct++] = arg[j];
}
}
else {
for (k in arg) {
if (arg.hasOwnProperty(k)) {
if (parseInt(k, 10) + '' === k) {
retObj[ct++] = arg[k];
}
else {
retObj[k] = arg[k];
}
}
}
}
}
return retObj;
}
function array_keys (input, search_value, argStrict) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + input by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: jd
// + improved by: Brett Zamir (http://brett-zamir.me)
// + input by: P
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// * example 1: array_keys( {firstname: 'Kevin', surname: 'van Zonneveld'} );
// * returns 1: {0: 'firstname', 1: 'surname'}
var search = typeof search_value !== 'undefined',
tmp_arr = [],
strict = !!argStrict,
include = true,
key = '';
if (input && typeof input === 'object' && input.change_key_case) { // Duck-type check for our own array()-created PHPJS_Array
return input.keys(search_value, argStrict);
}
for (key in input) {
if (input.hasOwnProperty(key)) {
include = true;
if (search) {
if (strict && input[key] !== search_value) {
include = false;
}
else if (input[key] != search_value) {
include = false;
}
}
if (include) {
tmp_arr[tmp_arr.length] = key;
}
}
}
return tmp_arr;
}
function array_values (input) {
// http://kevin.vanzonneveld.net
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: Brett Zamir (http://brett-zamir.me)
// * example 1: array_values( {firstname: 'Kevin', surname: 'van Zonneveld'} );
// * returns 1: {0: 'Kevin', 1: 'van Zonneveld'}
var tmp_arr = [],
key = '';
if (input && typeof input === 'object' && input.change_key_case) { // Duck-type check for our own array()-created PHPJS_Array
return input.values();
}
for (key in input) {
tmp_arr[tmp_arr.length] = input[key];
}
return tmp_arr;
}
<?php
// Without this, the characters are going to be garbled!
header('Content-encoding: utf-8');
?>
<!DOCTYPE html>
<html>
<head>
<title>Romaji/Kana transliteration</title>
<meta http-equiv="content-encoding" value="utf-8" />
</head>
<body>
<dl>
<dt><label for="in">Romaji</label></dt>
<dd><textarea rows="10" cols="80" name="in" id="in"></textarea></dd>
</dl>
<dl>
<dt><label for="action">Action</label></dt>
<dd>
<select name="action" id="action">
<option value="to_hira">To Hiragana</option>
<option value="to_kata">To Katakana</option>
<option value="from_kana">From Kana</option>
</select>
</dd>
</dl>
<button id="sub">Transliterate</button>
<p id="output"></p>
<script src="kana.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment