npm

Join us for the "JavaScript Supply Chain Security" tech talk, presented by VP of Security, Adam Baldwin. 6/20 at 10am PT.Sign up here »

lzbase62

1.4.9 • Public • Published

lzbase62

Build Status

LZ77(LZSS) based compression algorithm in base62 for JavaScript.

The compressed result will be a string in base 62 (0-9A-Za-z) characters.
This is useful when storing the large data in a size limited storage (e.g., localStorage, cookie etc.).

Installation

In a browser:

<script src="lzbase62.js"></script>

or

<script src="lzbase62.min.js"></script>

The object named "lzbase62" will defined in the global scope.

In Node.js:

npm install lzbase62
var lzbase62 = require('lzbase62');

bower:

bower install lzbase62

Usage

  • {string} lzbase62.compress ( data [, options ] )
    Compress data to a base 62(0-9a-zA-Z) encoded string.
    @param {string|Buffer} data Input data
    @param {Object=} [options] Options
    @return {string} Compressed data

  • {string} lzbase62.decompress ( data [, options ] )
    Decompress data from a base 62(0-9a-zA-Z) encoded string.
    @param {string} data Input data
    @param {Object=} [options] Options
    @return {string} Decompressed data

var data = 'hello hello hello';
console.log(data.length); // 17
 
var compressed = lzbase62.compress(data);
console.log(compressed); // 'tYVccfrgxGL'
console.log(compressed.length); // 11
console.log(compressed.length < data.length); // true
 
var decompressed = lzbase62.decompress(compressed);
console.log(decompressed); // 'hello hello hello'
console.log(decompressed === data); // true

Options

onData

Called when a data is chunked.

Receive a chunked string data.

onEnd

Called when process is finished.

Compress data using onData events
var string = 'hello hello hello';
var compressed = [];
lzbase62.compress(string, {
  onData: function(data) {
    compressed.push(data);
  },
  onEnd: function() {
    console.log(compressed.join(''));
  }
});
Decompress data using onData events
var decompressed = [];
lzbase62.decompress(compressed, {
  onData: function(data) {
    decompressed.push(data);
  },
  onEnd: function() {
    console.log(decompressed.join(''));
  }
});

Demo

License

MIT

install

npm i lzbase62

Downloadsweekly downloads

292

version

1.4.9

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability