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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@webbuf/numbers

v3.0.26

Published

Fixed-sized numbers (unsigned, signed, float) optimized with Rust/WASM for the web, node.js, deno, and bun.

Downloads

96

Readme

@webbuf/numbers

@webbuf/numbers is a TypeScript/JavaScript library for working with fixed-size unsigned integers. This library provides support for several unsigned integer types (e.g., U8, U16, U32, U64, U128, U256) and allows users to perform common mathematical operations.

Signed integers and floating-point support are planned for future releases.

Installation

You can install the package from npm:

npm install @webbuf/numbers

Usage

The @webbuf/numbers package provides classes for fixed-size unsigned integers. Each class supports the following features:

  • Basic arithmetic operations: add, sub, mul, div
  • BigInt conversion: Convert to and from BigInt
  • Binary data encoding: Convert to and from big-endian and little-endian formats
  • Hexadecimal conversion: Convert to and from hexadecimal strings

Importing the Package

import { U8, U16, U32, U64, U128, U256 } from "@webbuf/numbers";

Creating Unsigned Integers

You can create instances from BigInt or number values using the fromBn and fromN static methods, respectively. Note that numbers are automatically converted to BigInt and may lose precision.

const uint8 = U8.fromN(255);
const uint64 = U64.fromBn(1024n);

Converting to BigInt

Use the toBn() method to retrieve the value as a BigInt:

const bigIntValue = uint64.toBn(); // 1024n

Arithmetic Operations

Each class supports basic arithmetic operations such as addition, subtraction, multiplication, and division.

const a = U16.fromN(1000);
const b = U16.fromN(500);
const result = a.add(b); // U16 instance with value 1500

Encoding to and from Buffers

For applications needing binary encoding, use the toBEBuf() and toLEBuf() methods to convert to big-endian and little-endian formats, respectively.

const buffer = uint16.toBEBuf(); // Big-endian representation
const uint16FromBuf = U16.fromBEBuf(buffer); // Reconstructed from buffer

Hexadecimal Representation

Each integer class can convert to and from hexadecimal strings:

const hexString = uint32.toHex(); // Hexadecimal string representation
const uint32FromHex = U32.fromHex(hexString); // Reconstruct from hex

API Documentation

FixedNum (Abstract Class)

The FixedNum abstract class provides a foundation for unsigned integer classes and defines the following core methods and properties:

  • Constructor: Initializes an instance with a buffer of fixed size.
  • toBn(): Converts the instance value to BigInt.
  • add(other: FixedNum): Adds another instance of the same type.
  • sub(other: FixedNum): Subtracts another instance of the same type.
  • mul(other: FixedNum): Multiplies by another instance of the same type.
  • div(other: FixedNum): Divides by another instance of the same type.
  • toBEBuf(): Returns a big-endian FixedBuf.
  • toLEBuf(): Returns a little-endian FixedBuf.
  • toHex(): Returns a hexadecimal string of the value.

U8, U16, U32, U64, U128, U256

Each of these classes extends FixedNum with specific byte lengths:

  • U8: 8-bit unsigned integer
  • U16: 16-bit unsigned integer
  • U32: 32-bit unsigned integer
  • U64: 64-bit unsigned integer
  • U128: 128-bit unsigned integer
  • U256: 256-bit unsigned integer

Example usage:

const uint8 = U8.fromN(42);
const uint16 = U16.fromBn(1000n);
console.log(uint8.toBn()); // 42n
console.log(uint16.toBn()); // 1000n

Future Enhancements

  • Signed Integer Support: Signed integer classes, allowing the representation of both positive and negative values, are planned for future versions.
  • Floating-Point Support: IEEE-754 floating-point support will be added to extend the range of numbers.

Contributing

Contributions are welcome! If you’d like to improve the library, feel free to open an issue or create a pull request on the GitHub repository.

License

This project is licensed under the MIT License.