Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text compression in JavaScript

Are there any libraries/etc. that implement lightweight text compression in JavaScript?

My real goal is to slightly shorten some text and make it inconspicuous at a glance. (It needn't provide security -- the text will be displayed for the user.) Ideally the compression could be tweaked to output only the query characters

[-._~!$&'()*+,;=:@/?a-zA-Z0-9]

so it can be passed in a URL. (Actually, this is kind of important -- if other characters are used they'd have to be percent-encoded, which will probably make the text larger than the original.) Of course rare characters could be percent-encoded if needed.

Any ideas? Failing that, suggestions for making a simple one? The compression doesn't have to be great but shorter URLs would be nicer. The text to be compressed should be English sentences: mostly lower-case letters, spaces, and punctuation with the occasional upper-case letters, digits, and newlines.

like image 752
Charles Avatar asked Jul 22 '11 14:07

Charles


People also ask

What is compression in JavaScript?

What is Code Compression? To compress, minimize or minify code simply refers to removing unnecessary characters from the source code like white spaces, new line characters and a host of redundant data without actually affecting how the code or resource as a whole is processed by the browser.

What is text compression?

Definition. Text Compression involves changing the representation of a file so that the (binary) compressed output takes less space to store, or less time to transmit, but still the original file can be reconstructed exactly from its compressed representation.

What is gzip in JavaScript?

gzip-js is a pure JavaScript implementation of the GZIP file format. It uses the DEFLATE algorithm for compressing data. Please note that since this is a pure JavaScript implementation, it should NOT be used on the server for production code. It also does not comply 100% with the standard, yet.


1 Answers

I've found lz-string which is a perfect fit for my needs. It compresses and decompresses text rapidly and can target raw bits, valid Unicode characters, or Base64. Perhaps it will be useful for someone else?

like image 161
Charles Avatar answered Sep 18 '22 21:09

Charles