mo-arraybuffer-tools
v0.0.2
Published
[ ] write proper tests [ ] create package.json browser/react-native fields
Downloads
5
Readme
[ ] write proper tests [ ] create package.json browser/react-native fields
one package but distinct include (import x from 'package/feature') with code removal? browser, electron, react-native keys?
package keywords
"test": "xo && ava && tsd-check"
travis? https://github.com/sindresorhus/delay/blob/master/test.js
arraybuffer-hex -> node way, TextEncoder way, fallback
arraybuffer-base64 -> same
arraybuffer-binarystring
arraybuffer-utf8
arraybuffer-nodebuffer
arraybuffer-sha256|sha512|sha1|md5 arraybuffer-hmac
var uint8array = new TextEncoder().encode(string); var string = new TextDecoder(encoding).decode(uint8array);
export function encode(source: ArrayBufferLike): string { let buffer = new Uint8Array(source); let result: string[] = []; for (let i=0; i<buffer.length; i++) { result.push(String.fromCharCode(buffer[i])); } return result.join(''); }
export function decode(source: string): ArrayBufferLike { let result = new Uint8Array(source.length); for (let i=0; i<source.length; i++) result[i] = source.charCodeAt(i); return result.buffer; }
function str2ab(str) { var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char var bufView = new Uint16Array(buf); for (var i=0, strLen=str.length; i<strLen; i++) { bufView[i] = str.charCodeAt(i); } return buf; }
return String.fromCharCode.apply(null, new Uint16Array(buf));
function arrayBufferToString(buffer){
var bufView = new Uint16Array(buffer);
var length = bufView.length;
var result = '';
var addition = Math.pow(2,16)-1;
for(var i = 0;i<length;i+=addition){
if(i + addition > length){
addition = length - i;
}
result += String.fromCharCode.apply(null, bufView.subarray(i,i+addition));
}
return result;
}