ilib
Version:
iLib is a cross-engine library of internationalization (i18n) classes written in pure JS
2 lines • 4.78 kB
JavaScript
// !data ccc nfd nfc nfkd nfkc
var ilib=require("../index.js"),Utils=require("./Utils.js"),JSUtils=require("./JSUtils.js"),IString=require("./IString.js"),GlyphString=require("./GlyphString.js"),NormString=function(str,options){GlyphString.call(this,str,options)};NormString.prototype=new GlyphString("",{noinstance:!0}),NormString.prototype.parent=GlyphString,(NormString.prototype.constructor=NormString).init=function(options){var form="nfkc",script="all",sync=!0,loadParams=void 0,files=(options&&(options.form&&(form=options.form),options.script&&(script=options.script),options.loadParams&&(loadParams=options.loadParams),"boolean"==typeof options.sync)&&(sync=options.sync),[]),toLoad=[];({nfd:["nfd"],nfc:["nfc","nfd"],nfkd:["nfkd","nfd"],nfkc:["nfc","nfkd","nfd"]})[form].forEach(function(f){ilib.data.norm[f]&&!JSUtils.isEmpty(ilib.data.norm[f])||(files.push(f+"/"+script+".json"),toLoad.push(f))}),files.length||!ilib.data.ccc||JSUtils.isEmpty(ilib.data.ccc)?Utils.loadData({object:"NormString",name:"ccc.json",locale:"-",nonlocale:!0,sync:sync,loadParams:loadParams,callback:ilib.bind(this,function(normdata){ilib.data.ccc=normdata,files.length?Utils._callLoadData(files,sync,loadParams,void 0,function(arr){for(var i=0;i<arr.length;i++)void 0!==arr[i]&&ilib.extend(ilib.data.norm[toLoad[i]],arr[i]);options&&"function"==typeof options.onLoad&&options.onLoad(this)}):options&&"function"==typeof options.onLoad&&options.onLoad(this)})}):options&&"function"==typeof options.onLoad&&options.onLoad(this)},NormString._decomposeHangul=function(cp){var cp=cp-44032,result=String.fromCharCode(4352+cp/588)+String.fromCharCode(4449+cp%588/28),cp=cp%28;return 0!=cp&&(result+=String.fromCharCode(4519+cp)),result},NormString._expand=function(ch,canon,compat){var i,expansion="",n=ch.charCodeAt(0);if(GlyphString._isHangul(n))expansion=NormString._decomposeHangul(n);else{var result=canon[ch];if((result=!result&&compat?compat[ch]:result)&&result!==ch)for(i=0;i<result.length;i++)expansion+=NormString._expand(result[i],canon,compat);else expansion=ch}return expansion},NormString.prototype.normalize=function(form){var i;if("string"!=typeof form||0===this.str.length)return new IString(this.str);var nfc=!1,nfkd=!1;switch(form){case"nfc":nfc=!0;break;case"nfkd":nfkd=!0;break;case"nfkc":nfc=nfkd=!0}var ch,decomp="";if(nfkd)for(it=IString.prototype.charIterator.call(this);it.hasNext();)ch=it.next(),decomp+=NormString._expand(ch,ilib.data.norm.nfd,ilib.data.norm.nfkd);else for(it=IString.prototype.charIterator.call(this);it.hasNext();)ch=it.next(),decomp+=NormString._expand(ch,ilib.data.norm.nfd);function compareByCCC(left,right){return ilib.data.ccc[left]-ilib.data.ccc[right]}function ccc(c){return ilib.data.ccc[c]||0}for(var testChar,it=new IString(decomp).charIterator(),cpArray=[];it.hasNext();)cpArray.push(it.next());for(i=0;i<cpArray.length;){if(void 0!==ilib.data.ccc[cpArray[i]]&&0!==ccc(cpArray[i])){for(end=i+1;end<cpArray.length&&void 0!==ilib.data.ccc[cpArray[end]]&&0!==ccc(cpArray[end]);)end++;1<end-i&&(cpArray=cpArray.slice(0,i).concat(function sortChars(arr,comp){if("qt"!==ilib._getPlatform())return arr.sort(comp);for(var tmp,i=arr.length-1;0<i;i--)for(var j=0;j<i;j++)0<comp(arr[j],arr[j+1])&&(tmp=arr[j],arr[j]=arr[j+1],arr[j+1]=tmp);return arr}(cpArray.slice(i,end),compareByCCC),cpArray.slice(end)))}i++}if(nfc)for(i=0;i<cpArray.length;){if(void 0===ilib.data.ccc[cpArray[i]]||0===ilib.data.ccc[cpArray[i]])for(var end=i+1,notdone=!0;end<cpArray.length&¬done;)void 0!==ilib.data.ccc[cpArray[end]]&&0!==ilib.data.ccc[cpArray[end]]?(ccc(cpArray[end-1])<ccc(cpArray[end])&&void 0!==(testChar=GlyphString._compose(cpArray[i],cpArray[end]))&&(cpArray[i]=testChar,cpArray.splice(end,1),end=i),end++):(testChar=GlyphString._compose(cpArray[i],cpArray[end]),0===ccc(cpArray[end-1])&&void 0!==testChar?(cpArray[i]=testChar,cpArray.splice(end,1),end=i+1):notdone=!1);i++}return new IString(0<cpArray.length?cpArray.join(""):"")},NormString.prototype.charIterator=function(){var it=IString.prototype.charIterator.call(this);return new function _chiterator(istring){function ccc(c){return ilib.data.ccc[c]||0}this.index=0,this.hasNext=function(){return!!this.nextChar||it.hasNext()},this.next=function(){var nextCcc,ch=this.nextChar||it.next(),prevCcc=ccc(ch),composed=ch;if(this.nextChar=void 0,ilib.data.ccc&&(void 0===ilib.data.ccc[ch]||0===ccc(ch)))for(var testChar,notdone=!0;it.hasNext()&¬done;)this.nextChar=it.next(),nextCcc=ccc(this.nextChar),void 0!==ilib.data.ccc[this.nextChar]&&0!==nextCcc?(ch+=this.nextChar,this.nextChar=void 0):(testChar=GlyphString._compose(composed,this.nextChar),0===prevCcc&&void 0!==testChar?(composed=testChar,ch+=this.nextChar,this.nextChar=void 0):notdone=!1),prevCcc=nextCcc;return ch}}},module.exports=NormString;