huffman-javascript
v0.1.2
Published
Set of utilities for the huffman algorithm
Downloads
5
Maintainers
Readme
Huffman code is a particular type of optimal prefix code that is commonly used for lossless data compression. This is the implementation of the algorithm on TypeScript.
Installation
Clone this repository and install modules:
git clone https://github.com/kanitelk/huffman-javascript.git
cd huffman-javascript
npm install
npm run dev(or build)
Usage
The algorithm implementation is in the file /src/index.ts
Let's encode and decode plain text!
import { getCodesFromText, encode, decode } from './huffman';
/** ENCODING */
let text: string = 'abracadabra';
let encodedText: string = '';
let codes: Map<string, string> = getCodesFromText(text); // Symbols codes
let encodedArray: Array<any> = encode(text, codes); // Get array of encoded symbols
encodedText = encodedArray.join(''); // Encoded array to string. Equals 0101100...
/** DECODING */
text = decode(encodedArray, codes); // Equals 'abracadabra'
APIs
Encode text
encode(text: string, codes: Map<string, string>): Array<string>
Decode text
decode(text: Array<string>, codes: Map<string, string>):string
Get symbols codes from text
getCodesFromText(text: string): Map<string, string>
Get symbols frequency
getFrequency(text: string): Array<any>
Get Huffman Tree from frequency array
getTree(arr: Array<any>)
Get relative frequency array
getRelativeFrequency(arr: Array<any>): Array<any>
Get text entropy
getEntropyOfText(text: string): number