cryptpad/www/common/common-util.js

114 lines
3.3 KiB
JavaScript
Raw Normal View History

define([], function () {
var Util = {};
2017-05-04 22:16:09 +08:00
Util.find = function (map, path) {
return (map && path.reduce(function (p, n) {
return typeof(p[n]) !== 'undefined' && p[n];
}, map));
};
2017-05-04 22:16:09 +08:00
Util.fixHTML = function (str) {
if (!str) { return ''; }
return str.replace(/[<>&"']/g, function (x) {
return ({ "<": "&lt;", ">": "&gt", "&": "&amp;", '"': "&#34;", "'": "&#39;" })[x];
});
};
2017-05-04 22:16:09 +08:00
Util.hexToBase64 = function (hex) {
var hexArray = hex
.replace(/\r|\n/g, "")
.replace(/([\da-fA-F]{2}) ?/g, "0x$1 ")
.replace(/ +$/, "")
.split(" ");
var byteString = String.fromCharCode.apply(null, hexArray);
return window.btoa(byteString).replace(/\//g, '-').replace(/=+$/, '');
};
2017-05-04 22:16:09 +08:00
Util.base64ToHex = function (b64String) {
var hexArray = [];
atob(b64String.replace(/-/g, '/')).split("").forEach(function(e){
var h = e.charCodeAt(0).toString(16);
if (h.length === 1) { h = "0"+h; }
hexArray.push(h);
});
return hexArray.join("");
};
2017-05-04 22:16:09 +08:00
Util.uint8ArrayToHex = function (a) {
// call slice so Uint8Arrays work as expected
2017-05-04 22:16:09 +08:00
return Array.prototype.slice.call(a).map(function (e) {
var n = Number(e & 0xff).toString(16);
if (n === 'NaN') {
throw new Error('invalid input resulted in NaN');
}
switch (n.length) {
case 0: return '00'; // just being careful, shouldn't happen
case 1: return '0' + n;
case 2: return n;
default: throw new Error('unexpected value');
}
}).join('');
};
2017-05-04 22:16:09 +08:00
Util.deduplicateString = function (array) {
var a = array.slice();
for(var i=0; i<a.length; i++) {
for(var j=i+1; j<a.length; j++) {
if(a[i] === a[j]) { a.splice(j--, 1); }
}
}
return a;
};
2017-05-04 22:16:09 +08:00
Util.getHash = function () {
return window.location.hash.slice(1);
};
2017-05-04 22:16:09 +08:00
Util.replaceHash = function (hash) {
if (window.history && window.history.replaceState) {
if (!/^#/.test(hash)) { hash = '#' + hash; }
return void window.history.replaceState({}, window.document.title, hash);
}
window.location.hash = hash;
};
/*
* Saving files
*/
2017-05-04 22:16:09 +08:00
Util.fixFileName = function (filename) {
return filename.replace(/ /g, '-').replace(/[\/\?]/g, '_')
.replace(/_+/g, '_');
};
2017-05-04 22:16:09 +08:00
Util.bytesToMegabytes = function (bytes) {
2017-04-18 21:44:15 +08:00
return Math.floor((bytes / (1024 * 1024) * 100)) / 100;
};
2017-05-04 22:16:09 +08:00
Util.bytesToKilobytes = function (bytes) {
2017-04-18 21:44:15 +08:00
return Math.floor(bytes / 1024 * 100) / 100;
};
2017-05-10 00:36:18 +08:00
Util.fetch = function (src, cb) {
2017-05-12 22:13:09 +08:00
var done = false;
var CB = function (err, res) {
if (done) { return; }
done = true;
cb(err, res);
};
2017-05-10 00:36:18 +08:00
var xhr = new XMLHttpRequest();
xhr.open("GET", src, true);
xhr.responseType = "arraybuffer";
xhr.onload = function () {
2017-05-12 22:13:09 +08:00
return void CB(void 0, new Uint8Array(xhr.response));
};
xhr.onerror = function () {
CB('XHR_ERROR');
2017-05-10 00:36:18 +08:00
};
xhr.send(null);
};
return Util;
});