开发者

Text compression in JavaScript

开发者 https://www.devze.com 2023-03-22 19:17 出处:网络
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 securi

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 larg开发者_开发技巧er 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.


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?


I was thinking too of a huffman compression. This javascript library is really good: http://rumkin.com/tools/compression/compress_huff.php. It has a really space saving method to compress and decompress each character. Maybe you want to look for a Golomb Code, too. It has about the same compression rate like the huffman but IMO it's easier to implement. Don't ask me about an example.

0

精彩评论

暂无评论...
验证码 换一张
取 消