ilib
Version:
iLib is a cross-engine library of internationalization (i18n) classes written in pure JS
2 lines • 8.68 kB
JavaScript
// !data nfc nfkd
var ilib=require("./ilib.js"),Utils=require("./Utils.js"),Locale=require("./Locale.js"),CType=require("./CType.js"),IString=require("./IString.js"),isIdeo=require("./isIdeo.js"),isAscii=require("./isAscii.js"),isDigit=require("./isDigit.js"),Collator=require("./Collator.js"),NormString=require("./NormString.js"),AlphabeticIndex=function(options){this.sync=!0,this.loadParams={},this.caseSensitive=!1,this.accentSensitive=!1,this.overflowLabel="#",this.underflowLabel="*",this.style="standard",this.index=[],options&&(options.locale&&(this.locale="string"==typeof options.locale?new Locale(options.locale):options.locale),void 0!==options.style&&(this.style=options.style),void 0!==options.overflowLabel&&(this.overflowLabel=options.overflowLabel),void 0!==options.underflowLabel&&(this.underflowLabel=options.underflowLabel),void 0!==options.sync&&(this.sync=!!options.sync),options.loadParams&&(this.loadParams=options.loadParams)),this.locale=this.locale||new Locale(),isAscii._init(this.sync,this.loadParams,ilib.bind(this,function(){isIdeo._init(this.sync,this.loadParams,ilib.bind(this,function(){isDigit._init(this.sync,this.loadParams,ilib.bind(this,function(){NormString.init({sync:this.sync,loadParam:this.loadParams,onLoad:ilib.bind(this,function(){new Collator({locale:this.locale,useNative:!1,sensitivity:"primary",usage:"search",sync:this.sync,loadParam:this.loadParams,onLoad:ilib.bind(this,function(collation){this.collationObj=collation,this._init(),options&&"function"==typeof options.onLoad&&options.onLoad(this)})})})})}))}))}))};AlphabeticIndex.prototype._updateCollationMap=function(){this.mixedCollationMap=new Array(),Utils.loadData({object:"Collator",locale:this.locale,name:"collation.json",sync:!0,loadParams:this.loadParams,callback:ilib.bind(this,function(collations){for(var i=0;i<this.inherit.length;i++){var collationData={};"this"===this.inherit[i]?(collationData.style=this.style,collationData.flowBoundaries=this.flowBoundaries,collationData.indexUnits=this.indexUnits,collationData.map=this.collationMap):(collationData.style=this.inherit[i],collationData.flowBoundaries=collations[this.inherit[i]].flowBoundaries,collationData.indexUnits=collations[this.inherit[i]].indexUnits,collationData.map=collations[this.inherit[i]].map),this.mixedCollationMap.push(collationData)}})})},AlphabeticIndex.prototype._init=function(){this.flowBoundaries=new Array(),"standard"===this.style&&(this.style=this.collationObj.defaultRule),this.collationMap=this.collationObj.collation.map,this.flowBoundaries=this.collationObj.collation.flowBoundaries,this.indexUnits=this.collationObj.collation.indexUnits,this.inherit=this.collationObj.collation.inherit,void 0!==this.inherit&&this._updateCollationMap()},AlphabeticIndex.prototype._getKeyByValue=function(value,validMapNum){var i,label;if(!value||!ilib.isArray(value))return"";for(this.inherit&&-1<validMapNum&&(this.collationMap=this.mixedCollationMap[validMapNum].map,this.indexUnits=this.mixedCollationMap[validMapNum].indexUnits),i=0;i<this.indexUnits.length;i++)if(this.collationMap[this.indexUnits[i]][0]===value[0]){label=this.indexUnits[i];break}return label},AlphabeticIndex.prototype._getFirstChar=function(element){if(!element)return"";var firstChar,source=new NormString(element);return firstChar=source.normalize("nfc").charIterator().next(),CType.withinRange(firstChar,"hangul")&&(firstChar=source.normalize("nfkd").charIterator().next()),firstChar},AlphabeticIndex.prototype._getLabelIndex=function(label){if(label){var i,indexNum;for(i=0;i<this.index.length;i++)if(this.index[i].label===label){indexNum=i;break}return indexNum}},AlphabeticIndex.prototype.getLocale=function(){return this.locale},AlphabeticIndex.prototype.addElement=function(element){if("string"==typeof element){var i,label=this.getBucket(element),newItem=!0,itemSet={};for(itemSet.label=label,itemSet.elements=[],i=0;i<this.index.length;i++)if(this.index[i].label===label){-1===this.index[i].elements.indexOf(element)&&this.index[i].elements.push(element),newItem=!1;break}return newItem&&(itemSet.elements.push(element),this.index.push(itemSet)),label}},AlphabeticIndex.prototype.addLabels=function(labels,start){var allBucketLabels=[];if(labels||ilib.isArray(labels)){if(allBucketLabels=this.getAllBucketLabels(),!start||start>allBucketLabels.length)allBucketLabels=allBucketLabels.concat(labels);else if("string"==typeof labels)allBucketLabels.splice(start,0,labels);else if("object"==typeof labels)for(var j=labels.length-1;0<=j;j--)allBucketLabels.splice(start,0,labels[j]);this.allBucketLabels=allBucketLabels}},AlphabeticIndex.prototype.clear=function(){for(var prop in this.index)this.index.hasOwnProperty(prop)&&(this.index[prop]="")},AlphabeticIndex.prototype.getAllBuckets=function(){var temp,i,itemIndex,underflowIndex=-1,overflowIndex=-1,mixedScriptEndIndex=-1,count=0,tempArr=[],tempIndex=[],tempBucket={};for(i=0;i<this.index.length;i++)tempArr.push(this.index[i].label);for(tempArr.sort(ilib.bind(this.collationObj,this.collationObj.compare)),i=0;i<tempArr.length;i++)(tempBucket={}).label=tempArr[i],itemIndex=this._getLabelIndex(tempArr[i]),this.index[itemIndex].elements.sort(ilib.bind(this.collationObj,this.collationObj.compare)),tempBucket.elements=this.index[itemIndex].elements,tempIndex[i]=tempBucket,tempBucket={};for(this.index=tempIndex,i=0;i<this.index.length;i++)this.inherit&&-1===this.mixedCollationMap[0].indexUnits.indexOf(this.index[i].label)&&(mixedScriptEndIndex=i,count++);for(this.inherit&&0<count&&(temp=this.index.splice(mixedScriptEndIndex-count+1,count),this.index=this.index.concat(temp)),i=0;i<this.index.length;i++)if(this.index[i].label===this.underflowLabel){underflowIndex=i;break}for(0<underflowIndex&&(temp=this.index.splice(underflowIndex,1)[0],this.index.unshift(temp)),i=0;i<this.index.length;i++)if(this.index[i].label===this.overflowLabel){overflowIndex=i;break}return-1<overflowIndex&&(temp=this.index.splice(overflowIndex,1)[0],this.index.push(temp)),this.index},AlphabeticIndex.prototype.getBucket=function(element){var label,firstChar,collationValue,charNum,firstBoundaryChar,endBoundaryChar,firstCharNum,i,validMapNum=-1;if(element){if(firstChar=this._getFirstChar(element),this.inherit){for(i=0;i<this.mixedCollationMap.length;i++)if(this.mixedCollationMap[i].map[firstChar]){collationValue=this.mixedCollationMap[i].map[firstChar],validMapNum=i,this.flowBoundaries=this.mixedCollationMap[validMapNum].flowBoundaries,this.indexUnits=this.mixedCollationMap[validMapNum].indexUnits;break}}else collationValue=this.collationMap[firstChar];if(collationValue)"number"==typeof collationValue[0]?label=collationValue[0]<this.flowBoundaries[0]?this.underflowLabel:collationValue[0]>this.flowBoundaries[1]?this.overflowLabel:this._getKeyByValue(collationValue,validMapNum):"object"==typeof collationValue[0]&&(label=this._getKeyByValue(collationValue[0],validMapNum));else if(charNum=IString.toCodePoint(firstChar,0),this.inherit){for(i=0;i<this.inherit.length&&(firstBoundaryChar=this._getKeyByValue([this.mixedCollationMap[i].flowBoundaries[0]],i),charNum<(firstCharNum=IString.toCodePoint(firstBoundaryChar,0)));i++);label=i===this.inherit.length?this.underflowLabel:this.overflowLabel}else firstBoundaryChar=this._getKeyByValue([this.flowBoundaries[0]],0),endBoundaryChar=this._getKeyByValue([this.flowBoundaries[1]],0),firstCharNum=IString.toCodePoint(firstBoundaryChar,0),IString.toCodePoint(endBoundaryChar,0),label=charNum<firstCharNum?this.underflowLabel:this.overflowLabel;return label}},AlphabeticIndex.prototype.getIndexStyle=function(){return this.style},AlphabeticIndex.prototype.getBucketCount=function(){return Object.keys(this.index).length},AlphabeticIndex.prototype.getBucketLabels=function(){return this.getAllBuckets().map(function(bucket){return bucket.label})},AlphabeticIndex.prototype.getAllBucketLabels=function(){return this.allBucketLabels||(this.allBucketLabels=new Array(),this.allBucketLabels=[this.underflowLabel].concat(this.indexUnits,this.overflowLabel)),this.allBucketLabels},AlphabeticIndex.prototype.getCollator=function(){return this.collationObj},AlphabeticIndex.prototype.getOverflowLabel=function(){return this.overflowLabel},AlphabeticIndex.prototype.getElementCount=function(){var i,buckets=this.index,count=0;for(i=0;i<buckets.length;i++)count+=buckets[i].elements.length;return count},AlphabeticIndex.prototype.getUnderflowLabel=function(){return this.underflowLabel},AlphabeticIndex.prototype.setOverflowLabel=function(overflowLabel){this.overflowLabel=overflowLabel},AlphabeticIndex.prototype.setUnderflowLabel=function(underflowLabel){this.underflowLabel=underflowLabel},module.exports=AlphabeticIndex;