ilib
Version:
iLib is a cross-engine library of internationalization (i18n) classes written in pure JS
2 lines • 1.28 kB
JavaScript
// !data charset/UTF-16 charset/UTF-16LE
var Charset=require("./Charset.js"),Charmap=require("./Charmap.js"),UTF16LE=function(options){"object"==typeof(options=options||{sync:!0}).charset&&options.charset instanceof Charset?(this.charset=options.charset,this._init(options)):new Charset({name:"UTF-16LE",sync:options.sync,loadParams:options.loadParams,onLoad:ilib.bind(this,function(cs){this.charset=cs,this._init(options)})})};UTF16LE.prototype=new Charmap({noinstance:!0}),UTF16LE.prototype.parent=Charmap,(UTF16LE.prototype.constructor=UTF16LE).prototype._init=function(options){this._calcExpansionFactor(),"function"==typeof options.onLoad&&options.onLoad(this)},UTF16LE.prototype.mapToUnicode=function(bytes){if("undefined"!=typeof Buffer)return Buffer.from(bytes).toString("utf16le");for(var ret="",i=0;i<bytes.length;i+=2)ret+=String.fromCharCode(bytes[i+1]<<8|bytes[i]);return ret},UTF16LE.prototype.mapToNative=function(str){var b;if("undefined"!=typeof Buffer)return b=Buffer.from(str,"utf16le"),new Uint8Array(b);for(var c,ret=new Uint8Array(2*str.length+2),i=0;i<str.length;i++)c=str.charCodeAt(i),ret[2*i]=255&c,ret[2*i+1]=c>>8&255;return ret[2*i+1]=0,ret[2*i+2]=0,ret},Charmap._algorithms["UTF-16"]=UTF16LE,Charmap._algorithms["UTF-16LE"]=UTF16LE,module.exports=UTF16LE;