Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/polygonplanet/lzbase62
Compress and encode strings into base62 [0-9a-zA-Z] using an original LZ-based algorithm in JavaScript.
https://github.com/polygonplanet/lzbase62
base62 base64 compress compression compression-algorithm ecmascript javascript lz77 lzss string zip
Last synced: about 1 month ago
JSON representation
Compress and encode strings into base62 [0-9a-zA-Z] using an original LZ-based algorithm in JavaScript.
- Host: GitHub
- URL: https://github.com/polygonplanet/lzbase62
- Owner: polygonplanet
- License: mit
- Created: 2014-10-15T07:59:23.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2024-09-09T13:43:58.000Z (3 months ago)
- Last Synced: 2024-11-08T04:03:00.548Z (about 1 month ago)
- Topics: base62, base64, compress, compression, compression-algorithm, ecmascript, javascript, lz77, lzss, string, zip
- Language: JavaScript
- Homepage:
- Size: 896 KB
- Stars: 63
- Watchers: 4
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-list - lzbase62
README
lzbase62
========[![NPM Version](https://img.shields.io/npm/v/lzbase62.svg)](https://www.npmjs.com/package/lzbase62)
[![GitHub Actions Build Status](https://github.com/polygonplanet/lzbase62/actions/workflows/ci.yml/badge.svg)](https://github.com/polygonplanet/lzbase62/actions)
[![Bundle Size (minified)](https://img.shields.io/github/size/polygonplanet/lzbase62/dist/lzbase62.min.js.svg)](https://github.com/polygonplanet/lzbase62/blob/master/dist/lzbase62.min.js)
[![GitHub License](https://img.shields.io/github/license/polygonplanet/lzbase62.svg)](https://github.com/polygonplanet/lzbase62/blob/master/LICENSE)lzbase62 is a JavaScript library that compresses strings into ASCII strings composed solely of base62 (`0-9, a-z, A-Z`) characters, using the LZ77 based original algorithm.
It can compress and decompress any Unicode string that JavaScript can handle.
This can be particularly useful when storing large amounts of data in storage with size limitations, such as localStorage or cookies.
And, since the compressed strings are composed solely of base62 characters, they can be used as string parameters, like GET parameters, without the risk of control characters or symbols.## Installation
### npm
```bash
npm install --save lzbase62
```#### Using ES6 `import`
```javascript
import lzbase62 from 'lzbase62';
```#### Using CommonJS `require`
```javascript
const lzbase62 = require('lzbase62');
```### Browser (standalone)
You can install the library via npm or download it from the [release list](https://github.com/polygonplanet/lzbase62/tags). Use the `lzbase62.js` or `lzbase62.min.js` files included in the package.
\*Please note that if you use `git clone`, even the *master* branch may be under development.```html
```
or the minified `lzbase62.min.js`:```html
```
When the script is loaded, the `lzbase62` object is defined in the global scope (i.e., `window.lzbase62`).
## Usage
Example of compressing and decompressing a string:
```javascript
const data = 'hello hello hello';
console.log(data.length); // 17const compressed = lzbase62.compress(data);
console.log(compressed); // 'tYVccfxGM'
console.log(compressed.length); // 9const decompressed = lzbase62.decompress(compressed);
console.log(decompressed); // 'hello hello hello'
console.log(decompressed === data); // true
```## Demo
* [lzbase62 compression demo](https://polygonplanet.github.io/lzbase62/demo/)
## API
* [compress](#lzbase62compressdata-options)
* [decompress](#lzbase62decompressdata-options)----
### lzbase62.compress(data, [options])
Compresses data into a base62 `[0-9a-zA-Z]` encoded string.
#### Parameters
* **data** *(string)* : Input data
* **[options]** *(object)* : Compression options
* **onData** *(function (chunk: string) {})* : Called when a data is chunked
* **onEnd** *(function () {})* : Called when process ends#### Return value
*(string)* : Compressed data
#### Examples
Example of compressing a string:
```javascript
const compressed = lzbase62.compress('abcabcabcabcabc');
console.log(compressed); // 'tRSTxDM'
```Compresses data using `onData` and `onEnd` events:
```javascript
const string = 'hello hello hello';
const compressedChunks = [];lzbase62.compress(string, {
onData: (chunk) => {
compressedChunks.push(chunk);
},
onEnd: () => {
console.log(compressedChunks.join('')); // 'tYVccfxGM'
}
});
```----
### lzbase62.decompress(data, [options])
Decompresses a string that has been compressed with [`lzbase62.compress()`](#lzbase62compressdata-options).
#### Parameters
* **data** *(string)* : Input data
* **[options]** *(object)* : Decompression options
* **onData** *(function (chunk: string) {})* : Called when a data is chunked
* **onEnd** *(function () {})* : Called when process ends#### Return value
*(string)* : Decompressed data
#### Examples
Example of decompressing a string that has been compressed with [`lzbase62.compress()`](#lzbase62compressdata-options):
```javascript
const decompressed = lzbase62.decompress('tRSTxDM');
console.log(decompressed); // 'abcabcabcabcabc'
```Decompress data using `onData` and `onEnd` events:
```javascript
const compressed = 'tYVccfxGM';
const decompressedChunks = [];lzbase62.decompress(compressed, {
onData: (chunk) => {
decompressedChunks.push(chunk);
},
onEnd: () => {
console.log(decompressedChunks.join('')); // 'hello hello hello'
}
});
```## Contributing
We welcome contributions from everyone.
For bug reports and feature requests, please [create an issue on GitHub](https://github.com/polygonplanet/lzbase62/issues).### Pull Requests
Before submitting a pull request, please run `$ npm run test` to ensure there are no errors.
We only accept pull requests that pass all tests.## License
This project is licensed under the terms of the MIT license.
See the [LICENSE](LICENSE) file for details.