Created
May 2, 2009 16:20
-
-
Save vikhyat/105610 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Rufus = { | |
Mnemo: { | |
/** CONSTANTS **/ | |
V: [ 'a', 'e', 'i', 'o', 'u' ], | |
C: [ 'b', 'd', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'r', 's', 't', 'z' ], | |
SYL: [], // This is populated later on. [1] | |
SPECIAL: [ | |
[ "hu", "fu" ], | |
[ "si", "shi" ], | |
[ "ti", "chi" ], | |
[ "tu", "tsu" ], | |
[ "zi", "tzu" ] | |
], | |
/** FUNCTIONS **/ | |
from_integer: function(integer) { | |
var s = Rufus.Mnemo.from_i(integer) | |
return Rufus.Mnemo.to_special(s) | |
}, | |
to_integer: function (string) { | |
var s = Rufus.Mnemo.from_special(string) | |
return Rufus.Mnemo.to_i(s) | |
}, | |
to_number: function(syllable) { | |
return Rufus.Mnemo.SYL.indexOf(syllable); // Javascript 1.6 only | |
}, | |
from_i: function(integer) { | |
if( integer == 0 ) { return ''; } | |
var mod = integer % Rufus.Mnemo.SYL.length; | |
var rest = integer / Rufus.Mnemo.SYL.length; | |
rest = Math.floor(rest); | |
return Rufus.Mnemo.from_i(rest) + Rufus.Mnemo.SYL[mod]; | |
}, | |
to_i: function(s) { | |
if( s.length == 0 ) { return 0; } | |
return Rufus.Mnemo.SYL.length * Rufus.Mnemo.to_i(s.slice(0, -2)) + Rufus.Mnemo.to_number(s.slice(-2)); | |
}, | |
from_special: function(str) { | |
for(var i = 0; i < Rufus.Mnemo.SPECIAL.length; i++) { | |
str = str.replace(Rufus.Mnemo.SPECIAL[i][1], Rufus.Mnemo.SPECIAL[i][0]); | |
} | |
return str; | |
}, | |
to_special: function(str) { | |
for(var i = 0; i < Rufus.Mnemo.SPECIAL.length; i++) { | |
str = str.replace(Rufus.Mnemo.SPECIAL[i][0], Rufus.Mnemo.SPECIAL[i][1]); | |
} | |
return str; | |
} | |
} | |
} | |
// Populate the SYL array. [1] | |
for(var s = 0; s < Rufus.Mnemo.C.length; s++) { | |
for(var v = 0; v < Rufus.Mnemo.V.length; v++) { | |
Rufus.Mnemo.SYL.push( Rufus.Mnemo.C[s] + Rufus.Mnemo.V[v] ); | |
} | |
} | |
Rufus.Mnemo.SYL.push('wa', 'wo', 'ya', 'yo', 'yu'); // Add some more in. ;) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>rufus-mnemo</title> | |
<script src="rufusmnemo.js"></script> | |
<script src="http://9861.smfforfree.com/jquery.js"></script> | |
<script> | |
$(document).ready(function(){ | |
function check(word) { | |
if(Rufus.Mnemo.from_integer(Rufus.Mnemo.to_integer(word)) == word) return true; | |
return false; | |
} | |
words = [ 'kotoba', 'rubi', 'karasu', 'nada' ] | |
function append(str) { | |
$('div#tests').append(str + '<br />'); | |
} | |
for(var i=0; i < words.length; i++) { | |
if (check(words[i])) { | |
append("<b>Passed:</b> " + words[i]); | |
} else { | |
append("<b>Failed:</b> " + words[i]); | |
} | |
} | |
}); | |
</script> | |
</head> | |
<body> | |
<h1>Test Results</h1> | |
<div id="tests"></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@vikhyat, the library works wrong in situation when special is user more than once ("shishija"), because JS's str.replace replaces just first match.
To fix it line 52 must be:
and line 59