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.
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)
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'
encode(text: string, codes: Map<string, string>): Array<string>
decode(text: Array<string>, codes: Map<string, string>):string
getCodesFromText(text: string): Map<string, string>
getFrequency(text: string): Array<any>
getTree(arr: Array<any>)
getRelativeFrequency(arr: Array<any>): Array<any>
getEntropyOfText(text: string): number