|
| 1 | +<header> |
| 2 | + <h1 align="center"> |
| 3 | + <a href="https://github.com/guanghechen/algorithm.ts/tree/main/packages/huffman#readme">@algorithm.ts/huffman</a> |
| 4 | + </h1> |
| 5 | + <div align="center"> |
| 6 | + <a href="https://www.npmjs.com/package/@algorithm.ts/huffman"> |
| 7 | + <img |
| 8 | + alt="Npm Version" |
| 9 | + src="https://img.shields.io/npm/v/@algorithm.ts/huffman.svg" |
| 10 | + /> |
| 11 | + </a> |
| 12 | + <a href="https://www.npmjs.com/package/@algorithm.ts/huffman"> |
| 13 | + <img |
| 14 | + alt="Npm Download" |
| 15 | + src="https://img.shields.io/npm/dm/@algorithm.ts/huffman.svg" |
| 16 | + /> |
| 17 | + </a> |
| 18 | + <a href="https://www.npmjs.com/package/@algorithm.ts/huffman"> |
| 19 | + <img |
| 20 | + alt="Npm License" |
| 21 | + src="https://img.shields.io/npm/l/@algorithm.ts/huffman.svg" |
| 22 | + /> |
| 23 | + </a> |
| 24 | + <a href="#install"> |
| 25 | + <img |
| 26 | + alt="Module Formats: cjs, esm" |
| 27 | + src="https://img.shields.io/badge/module_formats-cjs%2C%20esm-green.svg" |
| 28 | + /> |
| 29 | + </a> |
| 30 | + <a href="https://github.com/nodejs/node"> |
| 31 | + <img |
| 32 | + alt="Node.js Version" |
| 33 | + src="https://img.shields.io/node/v/@algorithm.ts/huffman" |
| 34 | + /> |
| 35 | + </a> |
| 36 | + <a href="https://github.com/facebook/jest"> |
| 37 | + <img |
| 38 | + alt="Tested with Jest" |
| 39 | + src="https://img.shields.io/badge/tested_with-jest-9c465e.svg" |
| 40 | + /> |
| 41 | + </a> |
| 42 | + <a href="https://github.com/prettier/prettier"> |
| 43 | + <img |
| 44 | + alt="Code Style: prettier" |
| 45 | + src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square" |
| 46 | + /> |
| 47 | + </a> |
| 48 | + </div> |
| 49 | +</header> |
| 50 | +<br/> |
| 51 | + |
| 52 | + |
| 53 | +A typescript implementation of the **huffman** coding. |
| 54 | + |
| 55 | + |
| 56 | +## Install |
| 57 | + |
| 58 | +* npm |
| 59 | + |
| 60 | + ```bash |
| 61 | + npm install --save @algorithm.ts/huffman |
| 62 | + ``` |
| 63 | + |
| 64 | +* yarn |
| 65 | + |
| 66 | + ```bash |
| 67 | + yarn add @algorithm.ts/huffman |
| 68 | + ``` |
| 69 | + |
| 70 | + |
| 71 | +## Usage |
| 72 | + |
| 73 | +* `encode`: Encode text to Uint8Array and a huffman tree. |
| 74 | + |
| 75 | + ```typescript |
| 76 | + import { encode } from '@algorithm.ts/huffman' |
| 77 | + |
| 78 | + const { encodedData, encodingTable, tree } = encode('Hello, world!') |
| 79 | + ``` |
| 80 | + |
| 81 | +* `decode`: Decode a huffman encoded data to string. |
| 82 | + |
| 83 | + ```typescript |
| 84 | + import { decode } from '@algorithm.ts/huffman' |
| 85 | + |
| 86 | + const plaintext = decode(encodedData, tree) |
| 87 | + |
| 88 | + // Or build tree from encodingTable |
| 89 | + const tree2 = buildHuffmanTree(encodingTable) |
| 90 | + const plaintext2 = decode(encodedData, tree2) |
| 91 | + ``` |
| 92 | + |
| 93 | +* `compress`: Compress the encoded data. |
| 94 | + |
| 95 | + ```typescript |
| 96 | + // Array<0 | 1> ==> Uint8Array |
| 97 | + const compressedData = compress(encodedData) |
| 98 | + ``` |
| 99 | + |
| 100 | +* `decompress`: Decompress data. |
| 101 | + |
| 102 | + ```typescript |
| 103 | + // Uint8Array ==> Array<0 | 1> |
| 104 | + const encodedData2 = decompress(compressedData) |
| 105 | + ``` |
| 106 | + |
| 107 | + |
| 108 | +## Related |
| 109 | + |
| 110 | +* [Huffman coding | Wikipedia](https://en.wikipedia.org/wiki/Huffman_coding) |
| 111 | + |
| 112 | + |
| 113 | +[homepage]: https://github.com/guanghechen/algorithm.ts/tree/main/packages/huffman#readme |
0 commit comments