JSFiddle - React, Tailwind, and code Playground
Generate 1000-byte file with random data
by skibulk
JavaScript
console.clear();
var byte;
var data = new Uint8Array(400);
var dataStr = "";
for(var i = 0; i < data.length; i++){
byte = Math.floor(Math.random() * 192) + 64;
data[i] = byte;
dataStr += byte.toString(2).padStart(8, "0");
}
// data.sort(function(a,b){return a-b});
console.log(data, dataStr.length / 8, dataStr);
var saveByteArray = (function () {
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
return function (data, name) {
var blob = new Blob(data, {type: "octet/stream"}),
url = window.URL.createObjectURL(blob);
a.href = url;
a.download = name;
a.click();
window.URL.revokeObjectURL(url);
};
}());
// saveByteArray([data], 'test.txt');
// UTILITIES ------------------------------------------
//LZW Compression/Decompression for Strings
// http://rosettacode.org/wiki/LZW_compression#JavaScript
var LZW = {
compress: function (uncompressed) {
"use strict";
// Build the dictionary.
var i,
dictionary = {},
c,
wc,
w = "",
result = [],
dictSize = 256;
for (i = 0; i < 256; i += 1) {
dictionary[String.fromCharCode(i)] = i;
}
for (i = 0; i < uncompressed.length; i += 1) {
c = uncompressed.charAt(i);
wc = w + c;
//Do not use dictionary[wc] because javascript arrays
//will return values for array['pop'], array['push'] etc
// if (dictionary[wc]) {
if (dictionary.hasOwnProperty(wc)) {
w = wc;
} else {
result.push(dictionary[w]);
// Add wc to the dictionary.
dictionary[wc] = dictSize++;
w = String(c);
}
}
// Output the code for w.
if (w !== "") {
result.push(dictionary[w]);
}
return result;
...