nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

commit 48838f7faede19327ec352cf451cf9bb1cce2f23
parent 26f10cbd587371378d2d3e0d7440f1e00d549cbc
Author: Sergej Orlov <wladimirych@gmail.com>
Date:   Thu,  9 Feb 2017 11:40:31 +0300

moved functions for working with binary strings to separate package

Diffstat:
Asrc/lib/binary-strings/index.js | 30++++++++++++++++++++++++++++++
Msrc/lib/file-read/index.js | 16+---------------
Msrc/lib/xhr-promise/index.js | 16++--------------
3 files changed, 33 insertions(+), 29 deletions(-)

diff --git a/src/lib/binary-strings/index.js b/src/lib/binary-strings/index.js @@ -0,0 +1,29 @@ +function arrayBufferToString(arBuf) { + const + arr = new Uint8Array(arBuf), + s = []; + for (let i = 0; i < arr.length; i += 4096) { + let chunk = arr.subarray(i, i + 4096); + chunk = String.fromCharCode.apply(null, chunk); + s.push(chunk); + } + return s.join(''); +} + +function stringToArrayBuffer(s) { + const + length = s.length, + buf = new ArrayBuffer(length), + arr = new Uint8Array(buf); + for (let i = 0; i < length; i++) { + arr[i] = s.charCodeAt(i); + } + return buf; +} + +function blobFromString(s) { + const arr = stringToArrayBuffer(s); + return new Blob([arr]); +} + +export {arrayBufferToString, blobFromString, stringToArrayBuffer}; +\ No newline at end of file diff --git a/src/lib/file-read/index.js b/src/lib/file-read/index.js @@ -1,18 +1,4 @@ -function intArrayToString(arr) { - var s = []; - var chunk; - for (var i = 0; i < arr.length; i += 4096) { - chunk = arr.subarray(i, i + 4096); - chunk = String.fromCharCode.apply(null, chunk); - s.push(chunk); - } - return s.join(''); -} - -function arrayBufferToString(arBuf) { - var arr = new Uint8Array(arBuf); - return intArrayToString(arr); -} +import {arrayBufferToString} from 'lib/binary-strings'; const selectFiles = (() => { let fileInput; diff --git a/src/lib/xhr-promise/index.js b/src/lib/xhr-promise/index.js @@ -1,3 +1,5 @@ +import {arrayBufferToString} from 'lib/binary-strings'; + function successIfStatus200(xhr) { return xhr.status >= 200 && xhr.status <= 299; } @@ -6,20 +8,6 @@ function retryIfNetworkErrorOrServerError(xhr) { return (xhr.status === 0 || xhr.status >= 500); } - -function arrayBufferToString(arBuf) { - const result = []; - const arr = new Uint8Array(arBuf); - let chunk; - for (let i = 0; i < arr.length; i += 4096) { - chunk = arr.subarray(i, i + 4096); - chunk = String.fromCharCode.apply(null, chunk); - result.push(chunk); - } - return result.join(''); -} - - class XMLHttpRequestPromise { constructor( url, {method='GET', data=null, responseType='', timeout=30000, maxTries=3, retryTimeWait=1000,