ilib
Version:
iLib is a cross-engine library of internationalization (i18n) classes written in pure JS
2 lines • 2.76 kB
JavaScript
// !data charmaps/ISO-8859-15 charset/ISO-8859-15
var ilib=require("../index.js"),Utils=require("./Utils.js"),Charset=require("./Charset.js"),Charmap=require("./Charmap.js"),IString=require("./IString.js"),CharmapTable=function(options){this.parent.call(this,options),this.charsetName="ISO-8859-15",options?"object"==typeof options.charset?(this.charset=options.charset,this.charsetName=this.charset.getName()):void 0!==options.name&&(this.charsetName=options.name):options={sync:!0},this.charset?this._init(options):new Charset({name:this.charsetName,sync:!0,loadParams:options.loadParams,onLoad:ilib.bind(this,function(cs){this.charset=cs,this._init(options)})})};CharmapTable.prototype=new Charmap({noinstance:!0}),CharmapTable.prototype.parent=Charmap,(CharmapTable.prototype.constructor=CharmapTable).prototype._init=function(options){this._calcExpansionFactor(),Utils.loadData({object:"Charmap",locale:"-",nonlocale:!0,name:"charmaps/"+this.charset.getName()+".json",sync:options.sync,loadParams:options.loadParams,callback:ilib.bind(this,function(mapping){var ret=this;if(!mapping){if(options.sync)throw"No mapping found for "+this.charset.getName();ret=void 0}this.map=mapping,"function"==typeof options.onLoad&&options.onLoad(ret)})})},CharmapTable.prototype._trieWalk=function(trie,array,start){for(var lastLeaf=void 0,i=start,trienode=trie;i<array.length;){if(void 0!==trienode.__leaf&&(lastLeaf={consumed:i-start+1,value:trienode.__leaf}),0===array[i])return{consumed:1,value:0};if(void 0===trienode[array[i]])return lastLeaf;if(function isValue(node){return"string"==typeof node||"number"==typeof node||"object"==typeof node&&ilib.isArray(node)}(trienode[array[i]]))return{consumed:i-start+1,value:trienode[array[i]]};trienode=trienode[array[i++]]}},CharmapTable.prototype.mapToNative=function(string){if(!string)return new Uint8Array(0);for(var str=string instanceof IString?string:new IString(string),ret=new Uint8Array(str.length*this.expansionFactor),i=0,j=0;i<string.length;){var result=this._trieWalk(this.map.from,string,i);result?result.value?(i+=result.consumed,j+=this.writeNative(ret,j,result.value)):(i=string.length,this.writeNative(ret,j,[result.value])):j+=this.writeNativeString(ret,j,this.dealWithMissingChar(string[i++]))}return ret.subarray(0,j)},CharmapTable.prototype.mapToUnicode=function(bytes){for(var ret="",i=0;i<bytes.length;){var result=this._trieWalk(this.map.to,bytes,i);if(result)if(result.value){if(i+=result.consumed,"string"==typeof result.value)ret+=result.value;else if(ilib.isArray(result.value))for(var j=0;j<result.value.length;j++)ret+=result.value[j]}else i=bytes.length;else ret+=this.dealWithMissingChar(bytes[i++])}return ret},Charmap._algorithms.CharmapTable=CharmapTable,module.exports=CharmapTable;