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

@1-corp/fixed-size-numbers-ts

v1.1.0

Published

A functional implementation of fixed size integers in javascript with typing in TypeScript.

Downloads

8

Readme

Fixed Size Numbers

A functional implementation of fixed size integers in javascript with typing in TypeScript.

Note: this library is just a typed wrapper on BigNumber.js

Currently no Floating Points or Decimal values are supported!!!

API

Create a new signed or unsigned integer by passing a value in string, number, or BigNumber format to the constructor function

Access the value and the size with their respective attributes (_value, and _size)

Note: _value is always a BigNumber

Or preform safe math using the methods provided

import { Uint8, Int8 } from 'fixed-sized-numbers-ts'
let safe72 = Uint8(72)
let safeNegative54 = Int8(-54)

console.log(safe72)
/*
{
    _value: 72,
    _size: 8,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    _uint8: true
}
*/

console.log(safeNegative54)
/*
{
    _value: -54,
    _size: 8,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    _isPositive: false
    _int8: true,
}
*/

Upgrade any number simply by feeding it to a new constructor function

Note: This library freezes all objects, so you cannot simply change the _value of a constructed type. This is good and forces immutability.

import { Uint8, Uin64, Int8 } from 'fixed-sized-numbers-ts'
let safe72 = Uint8(72)

console.log(safe72)
/*
{
    _value: 72,
    _size: 8,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    _uint8: true
}
*/

let safeLarger72 = Uint64(safe72._value)

console.log(safeLarger72)
/*
{
    _value: 72,
    _size: 64,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    _uint64: true
}
*/

let signed72 = Int8(safeLarger72)

console.log(safeLarger72)
/*
{
    _value: 72,
    _size: 8,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    isPositive: true,
    _int8: true
}
*/

Note: Anything larger than 32bits needs to be constructed using strings because of imprecission in Javascript after 53 bits, this is enforced at the typing level

import { Uint64 } from 'fixed-sized-numbers-ts'
let bigSafeNumber = Uint64("18446744073709551616")

console.log(bigSafeNumber)
/*
{
    _value: "18446744073709551616",
    _size: 64,
    validateSize: [Function],
    add: [Function],
    sub: [Function],
    mul: [Function],
    div: [Function],
    _uint8: true
}
*/

For safe math the first value in the equation will be the one who's method is being called, the second number will be the number passed to it. i.e. Uint8(2).add() is a function that will add 2 to any other Uint8 value.

Note: Types must match for any arithmetic operation. The way the methods are generated at construction time is such that they require the same type as an argument. Additionally, the result of any of the arithmetic operations will the same type as its parent object. This is because the arithmetic operations generate a new number.

import { Uint8, Uint16, Int8 } from 'fixed-sized-numbers-ts'
let one = Uint8(1)
let two = Uint8(2)
let three = Uint8(3)
let threeHunnid = Uint16(300)
let negOne = Uint8(-1)
let oneSigned = Uint8(1)

one.add(one) // will yield a Uint8 with _value of 2
two.sub(one) // will yield a Uint8 with _value of 1
two.mul(three) // will yield a Uint8 with _value of 6
two.div(one) // will yield a Uint8 with _value of 2

// All of these will yield a type error
one.add(oneSigned) // No Uint8 -> Int8
negOne.add(one) // No Int8 -> Uint8
one.add(negOne) // No Uint8 -> Int8
threeHunnid.add(one) // No Uint16 -> Uint8
threeHunnid.add(negOne) // no Uint16 -> Int8