LZW

node v14.18.3
version: master
endpointsharetweet
function compress(str) { let dict = {}, data = (str + "").split(""), out = [], currChar, phrase = data[0], code = 256; for (let i = 1; i < data.length; i++) { currChar = data[i]; if (dict[phrase + currChar] != null) phrase += currChar; else { out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); dict[phrase + currChar] = code; code++; phrase = currChar; } } out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); for (let i = 0; i < out.length; i++) out[i] = String.fromCharCode(out[i]); return out.join(""); } function decompress(str) { let dict = {}, data = (str + "").split(""), currChar = data[0], oldPhrase = currChar, out = [currChar], code = 256, phrase; for (let i = 1; i < data.length; i++) { let currCode = data[i].charCodeAt(0); if (currCode < 256) phrase = data[i]; else phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar); out.push(phrase); currChar = phrase.charAt(0); dict[code] = oldPhrase + currChar; code++; oldPhrase = phrase; } return out.join(""); }
const fetch = require('node-fetch'); fetch('https://gist.githubusercontent.com/SiddharthShyniben/29293ad446d94b7d972448943159ccae/raw/5a0fcfcd8052a27d6ae93954dbbf97b6ccfd61a4/image.txt') .then(res => res.text()) .then(data => console.log({ original: data, originalLength: data.length, compressed: compress(data), compressedLength: compress(data).length, diff: data.length - compress(data).length, diffPercentage: (((data.length - compress(data).length) / data.length) * 100) + "%", compressionWorks: data === decompress(compress(data)) }))
Loading…

no comments

    sign in to comment