0.1.2 • Published 2 years ago

huffman-javascript v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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)

npm.io

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'

npm.io

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