ナベアツ
目的は
「ナベアツは1から40まで数えて,3の倍数と3がついた数字のときにアホになります.ではスタート」
という自然文的な書き方がしたかったのです
ということで
Nabeatsu.count(1,40).if([_multi(3), _with(3)]).then(aho).start();
みたいなことができるように実装.
個人的には,ifの中とかが嫌い.
きちんと「or」と「and」を自然文に近づけたい!!!
まあ,とりあえず,動作してます.
var toHiragana = function(int){ var unit = ['', 'まん','おく','ちょう','けい']; var hatsuon=[['', 'いち','に','さん','よん','ご', 'ろく','なな','はち','きゅう'], ['', 'じゅう','にじゅう','さんじゅう','よんじゅう','ごじゅう', 'ろくじゅう','ななじゅう','はちじゅう','きゅうじゅう'], ['', 'ひゃく','にひゃく','さんびゃく','よんひゃく','ごひゃく', 'ろっぴゃく','ななひゃく','はっぴゃく','きゅうひゃく'], ['', 'せん','にせん','さんぜん','よんせん','ごせん', 'ろくせん','ななせん','はっせん','きゅうせん']]; var digit = new Array(); var pronoun = ''; while(int){ var m = int % 10; if(digit.length % 4 == 0) pronoun = unit[digit.length / 4] + pronoun; pronoun = hatsuon[digit.length % 4][m] + pronoun; digit.push(m); int = int / 10 | 0; } return pronoun; } Number.prototype.toHiragana = function(){ return toHiragana(this); }; var aho = function(msg){ var rand = Math.random(); if(rand > 0.66){ alert(toHiragana(msg)+'〜っ'); }else if(rand > 0.33){ alert(toHiragana(msg).replace(/さん/, 'さっん')); }else{ alert(toHiragana(msg).replace(/さん/, 'すぁん')+'♪'); } }; var _multi = function(mod){ return function(x){ return !(x % mod); } }; var _with = function(opt){ return function(x){ return x.toString().match(eval('/'+opt+'/')); }; }; var Count = function(){this.initialize.apply(this, arguments)}; Count.prototype = { name : 'Count', initialize : function(begin, end){ this._begin = begin; this._end = end; this._process = null; }, start : function(){ for(this.memory = this._begin; this.memory < this._end; this.memory++){ if(this._process){ this._process(); } } }, }; var Nabeatsu = { name : 'Nabeatsu', count : function(begin, end){ this._count = new Count(begin, end); return this; }, if : function(cond){ if(typeof cond == 'object' && cond.length){ this._cond = function(x){ for(var i in cond){ if(cond[i](x)) return true; } return false; } }else{ this._cond = cond; } return this; }, then : function(action){ var cond = this._cond; this._count._process = function(){ if(cond(this.memory)){ action(this.memory); }else{ alert(toHiragana(this.memory)); } }; return this; }, start : function(){ this._count.start(); } } Nabeatsu.count(1,41).if([_multi(3),_with(3)]).then(aho).start()