npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@algorithm.ts/huffman

v4.0.5

Published

huffman + Euclidean algorithm

Readme

A typescript implementation of the huffman coding.

Install

  • npm

    npm install --save @algorithm.ts/huffman
  • pnpm

    pnpm add @algorithm.ts/huffman

Usage

  • encode: Encode text to Uint8Array and a huffman tree.

    import { encode } from '@algorithm.ts/huffman'
    const { encodedData, encodingTable, tree } = encode('Hello, world!')
  • decode: Decode a huffman encoded data to string.

    import { decode, fromEncodingTable } from '@algorithm.ts/huffman'
    
    const plaintext = decode(encodedData, tree)
    
    // Or build tree from encodingTable
    const tree2 = fromEncodingTable(encodingTable)
    const plaintext2 = decode(encodedData, tree2)
  • compress: Compress the encoded data.

    // Array<0 | 1> ==> Uint8Array
    const compressedData = compress(encodedData)
  • decompress: Decompress data.

    // Uint8Array ==> Array<0 | 1>
    const encodedData2 = decompress(compressedData)

Example

  • Compress uri data

    const data = { name: 'alice', age: 33, gender: 'female' }
    
    compress(JSON.stringify(data))
    // => 'WyIzIiwyMCwiLCIsMTYsIn0iLDM0LCJpIiwzNSwiZyIsMTgsIm4iLDE5LCJsIiwyMSwiciIsNDQsImMiLDkwLCJkIiw5MSwiOiIsMjMsIlwiIiw2LCJlIiwxNCwiYSIsMzAsInsiLDEyNCwiZiIsMTI1LCJtIiw2M10_-BvI)(p7lG1oLi06IEWNvMnvd(l0C'
    
    decompress('WyIzIiwyMCwiLCIsMTYsIn0iLDM0LCJpIiwzNSwiZyIsMTgsIm4iLDE5LCJsIiwyMSwiciIsNDQsImMiLDkwLCJkIiw5MSwiOiIsMjMsIlwiIiw2LCJlIiwxNCwiYSIsMzAsInsiLDEyNCwiZiIsMTI1LCJtIiw2M10_-BvI)(p7lG1oLi06IEWNvMnvd(l0C')
    // => '{"name":"alice","age":33,"gender":"female"}'
    
    function compress(text) {
      const { encodedData, encodingTable } = huffman.encode(text)
      const cipherBuffer = huffman.compress(encodedData)
      const cipherText = base64.encode(cipherBuffer)
    
      const textEncoder = new TextEncoder('utf-8')
      const encodingTableText = base64.encode(
        textEncoder.encode(JSON.stringify(compressEncodingTable(encodingTable))),
      )
      const result = (encodingTableText + '-' + cipherText)
        .replace(/\//g, '(')
        .replace(/\+/g, ')')
        .replace(/=/g, '_')
      return result
    }
    
    function decompress(text) {
      const [encodingTableText, cipherText] = text
        .replace(/\(/g, '/')
        .replace(/\)/g, '+')
        .replace(/_/g, '=')
        .split('-')
      const textDecoder = new TextDecoder('utf-8')
      const encodingTable = decompressEncodingTable(
        JSON.parse(textDecoder.decode(base64.decode(encodingTableText))),
      )
      const tree = huffman.fromEncodingTable(encodingTable)
    
      const cipherData = huffman.decompress(base64.decode(cipherText))
      const plaintext = huffman.decode(cipherData, tree)
      return plaintext
    }
    
    function compressEncodingTable(table) {
      const entries = Object.entries(table)
        .map(([value, path]) => {
          let p = 1
          for (const x of path) p = (p << 1) | x
          return [value, p]
        })
        .flat()
      return entries
    }
    
    function decompressEncodingTable(entries) {
      const table = {}
      for (let i = 0; i < entries.length; i += 2) {
        const value = entries[i]
        const p = entries[i + 1]
        const path = []
        for (let x = p; x > 1; x >>= 1) path.push(x & 1)
        table[value] = path.reverse()
      }
      return table
    }

Related