Package detail

lzbase62

polygonplanet11.8kMIT2.0.0

LZ77(LZSS) based compression algorithm in base62 for JavaScript

compression, decompression, algorithm, compress

readme

lzbase62

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

NPM Version Build Status Bundle Size (minified) GitHub License

The compressed result will be a string in base 62 (0-9A-Za-z) characters.
This is useful when storing large amounts of data in size-limited storage such as localStorage or cookies.

Demo

Installation

npm

$ npm install --save lzbase62

Usage

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

node

const lzbase62 = require('lzbase62');
const compressed = lzbase62.compress('hello hello hello');

webpack etc.

import lzbase62 from 'lzbase62';
const compressed = lzbase62.compress('hello hello hello');

browser (standalone)

<script src="lzbase62.min.js"></script>
<script>
var compressed = lzbase62.compress('hello hello hello');
</script>

Object lzbase62 is defined in the global scope if running in the browser window. ( window.lzbase62 )

API


lzbase62.compress(data, [options])

Compress data to a base62 [0-9a-zA-Z] encoded string.

Arguments

  • data (string) : Input data
  • [options] (object) : Compress options
    • onData (function (data) {}) : Called when a data is chunked
    • onEnd (function () {}) : Called when process is finished

Returns

(string) : Compressed data

Example

Compress string

var compressed = lzbase62.compress('abcabcabcabcabc');
console.log(compressed); // 'tRSTxDM'

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('')); // 'tYVccfrgxGL'
  }
});

lzbase62.decompress(data, [options])

Decompress data from a base62 [0-9a-zA-Z] encoded string.

Arguments

  • data (string) : Input data
  • [options] (object) : Decompress options
    • onData (function (data) {}) : Called when a data is chunked
    • onEnd (function () {}) : Called when process is finished

Returns

(string) : Decompressed data

Example

Decompress string

var decompressed = lzbase62.decompress('tRSTxDM');
console.log(decompressed); // 'abcabcabcabcabc'

Decompress data using onData events

var compressed = 'tYVccfrgxGL';
var decompressed = [];

lzbase62.decompress(compressed, {
  onData: function(data) {
    decompressed.push(data);
  },
  onEnd: function() {
    console.log(decompressed.join('')); // 'hello hello hello'
  }
});

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file.

2.0.0 - 2020-04-09

Changed

  • Changed the algorithm for creating sliding window.
    Note: The compression results have changed and are incompatible with previous versions.

Removed

  • Drop bower support.

1.4.9 - 2018-05-10

Fixed

  • Fix bug for can't find module.