utf-helpers
Version:
A zero-dependency tool to encode/decode UTF-8, UTF-16 and hex strings. For browser and Node.js. With typings.
2 lines • 3.17 kB
JavaScript
;var UtfHelpers=(()=>{var l=Object.defineProperty;var f=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var u=(r,t)=>{for(var n in t)l(r,n,{get:t[n],enumerable:!0})},c=(r,t,n,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of m(t))!A.call(r,e)&&e!==n&&l(r,e,{get:()=>t[e],enumerable:!(i=f(t,e))||i.enumerable});return r};var y=r=>c(l({},"__esModule",{value:!0}),r);var U={};u(U,{HexString:()=>g,Utf16:()=>x,Utf8:()=>a,UtfHelpers:()=>F});var g={fromArray:r=>{if(!(r instanceof Uint8Array)&&!Array.isArray(r))throw new Error(`HexString.fromArray: passed bytes obj is not an Array or Uint8Array: ${typeof r}, ${r}`);let t=r instanceof Uint8Array?Array.from(r):r;return"0x"+t.reduce((n,i)=>n+i.toString(16).padStart(2,"0"),"")},fromU8a:r=>g.fromArray(r),toArray(r){if(typeof r!="string")throw new Error(`HexString.toArray: passed string is not a string: ${typeof r}`);return((r.startsWith("0x")?r.slice(2):r).match(/.{1,2}/g)||[]).map(i=>parseInt(i,16))},toU8a:r=>Uint8Array.from(g.toArray(r))},a={stringToU8a(r){let t=new Uint8Array(a.lengthInBytes(r)),n=0,i=n,e=0,o=0,s=0;for(;s<r.length;)e=r.charCodeAt(s),e<128?t[n++]=e:e<2048?(t[n++]=e>>6|192,t[n++]=e&63|128):(e&64512)===55296&&((o=r.charCodeAt(s+1))&64512)===56320?(e=65536+((e&1023)<<10)+(o&1023),++s,t[n++]=e>>18|240,t[n++]=e>>12&63|128,t[n++]=e>>6&63|128,t[n++]=e&63|128):(t[n++]=e>>12|224,t[n++]=e>>6&63|128,t[n++]=e&63|128),s+=1;let h=n-i;return t},stringToNumberArray(r){return Array.from(a.stringToU8a(r))},u8aToString(r){let t=0,n=r.length;if(n-t<1)return"";let i="",e=t;for(;e<n;){let o=r[e++];if(o<=127)i+=String.fromCharCode(o);else if(o>=192&&o<224)i+=String.fromCharCode((o&31)<<6|r[e++]&63);else if(o>=224&&o<240)i+=String.fromCharCode((o&15)<<12|(r[e++]&63)<<6|r[e++]&63);else if(o>=240){let s=((o&7)<<18|(r[e++]&63)<<12|(r[e++]&63)<<6|r[e++]&63)-65536;i+=String.fromCharCode(55296+(s>>10)),i+=String.fromCharCode(56320+(s&1023))}}return i},numberArrayToString(r){return a.u8aToString(Uint8Array.from(r))},stringToHexString(r){return g.fromU8a(a.stringToU8a(r))},hexStringToString(r){return a.u8aToString(g.toU8a(r))},lengthInBytes(r){let t=0,n=0,i=0;for(;i<r.length;)n=r.charCodeAt(i),n<128?t+=1:n<2048?t+=2:(n&64512)===55296&&(r.charCodeAt(i+1)&64512)===56320?(++i,t+=4):t+=3,i+=1;return t}},x={stringToU16a(r){let t=new Uint16Array(x.lengthInBytes(r)),n=0;for(;n<r.length;){let i=r.codePointAt(n);i<=65535?t[n++]=i:(i-=65536,t[n++]=(i>>10)+55296,t[n++]=i%1024+56320)}return t},stringToNumberArray(r){return Array.from(x.stringToU16a(r))},numberArrayToString(r){let t=0,n=r.length,i="";for(;t<n-1;){let e=r[t],o=r[t+1];if(e>=55296&&e<=57343)if(o>=56320&&o<=57343)i+=String.fromCodePoint((e-55296)*1024+o-56320+65536),t+=2;else throw new Error(`invalid UTF16 sequence: first u16 is ${e}, second u16 is ${o}`);else i+=String.fromCodePoint(e),t+=1}return t<n&&(i+=String.fromCodePoint(r[n-1])),i},u16aToString(r){return x.numberArrayToString(r)},lengthInBytes(r){let t=0;for(;t<r.length;)t+=r.codePointAt(t)<=65535?1:2;return t}},F={HexString:g,Utf8:a,Utf16:x};return y(U);})();
//# sourceMappingURL=utf-helpers.min.js.map