forked from GrosSacASac/utilsac
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypeCast.js
More file actions
33 lines (28 loc) · 838 Bytes
/
typeCast.js
File metadata and controls
33 lines (28 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
export {
stringFromArrayBuffer,
arrayBufferFromBlob,
stringFromBlob,
};
const stringFromArrayBuffer = function (arrayBuffer, encoding = `utf-8`) {
return (new TextDecoder(encoding)).decode(new DataView(arrayBuffer));
};
const xFromBlob = function (readAs) {
/**
* @param {Blob | File} blob
* @return {Promise}
*/
return function (blob) {
return new Promise(function (resolve, reject) {
const reader = new FileReader();
reader.onload = function () {
resolve(reader.result);
};
reader.onerror = function (error) {
reject(error);
};
reader[readAs](blob);
});
};
};
const arrayBufferFromBlob = xFromBlob(`readAsArrayBuffer`);
const stringFromBlob = xFromBlob(`readAsText`);