C-like Unsigned Integers
The `cuint` library provides functionality for handling C-like unsigned 32-bit and 64-bit integers in JavaScript, a capability not natively supported by the language. It represents unsigned integers as objects, splitting them into 16-bit segments (e.g., two for UINT32, four for UINT64) to emulate fixed-width arithmetic. Designed for performance, it aims to provide C-like behavior, including truncation on overflow. The current and only stable version is 0.2.2, last updated approximately 9 years ago (as of 2026). Due to its age and lack of updates, it is considered an abandoned project, meaning it likely does not receive bug fixes, security patches, or compatibility updates for newer JavaScript environments or Node.js versions. Its primary differentiator was bringing explicit unsigned integer arithmetic to JavaScript before `BigInt` was standardized.
Common errors
-
ReferenceError: UINT32 is not defined
cause Attempting to use `UINT32` in Node.js without `require('cuint')` or in a browser without including the build script.fixIn Node.js: `const { UINT32 } = require('cuint');`. In browser: ensure `<script src='/path/to/uint32.js'></script>` is present and loaded before usage. -
TypeError: Cannot read properties of undefined (reading 'add')
cause Trying to call a method like `.add()` on a variable that is not an instance of `UINT32` or `UINT64`, possibly due to incorrect instantiation or a lost `this` context.fixEnsure the variable is correctly instantiated with `UINT32(...)` or `UINT64(...)` and that method calls are chained correctly or called on valid instances. -
SyntaxError: Named export 'UINT32' not found. The requested module 'cuint' does not provide an export named 'UINT32'
cause Attempting to use ES Module `import { UINT32 } from 'cuint';` syntax with an abandoned CommonJS-only package in an ESM context.fixUse CommonJS `require()` syntax: `const { UINT32 } = require('cuint');`. -
TypeError: UINT32 is not a constructor
cause Trying to use `new UINT32(...)` instead of `UINT32(...)` as the library's functions are factory functions, not traditional constructors.fixCall `UINT32()` or `UINT64()` directly without the `new` keyword, e.g., `const value = UINT32(123);`.
Warnings
- breaking Methods modify the object in place, requiring explicit `.clone()` for immutable operations. This behavior is C-like but non-idiomatic for typical JavaScript object operations.
- gotcha The library is abandoned and has not been updated in over 9 years. This means it may have compatibility issues with newer Node.js versions, JavaScript engines, or security vulnerabilities that will not be patched.
- gotcha Overflows are handled by C-like truncation to the respective 32-bit or 64-bit limit. This differs significantly from JavaScript's standard number behavior or `BigInt`'s arbitrary precision, which might lead to unexpected results if not accounted for.
- gotcha The package does not provide native ES Module (ESM) exports. Direct `import ... from 'cuint'` syntax will not work in ESM-only Node.js environments or modern browser module contexts without a transpilation step.
Install
-
npm install cuint -
yarn add cuint -
pnpm add cuint
Imports
- UINT32
import { UINT32 } from 'cuint';const { UINT32 } = require('cuint'); - UINT64
import { UINT64 } from 'cuint';const { UINT64 } = require('cuint'); - UINT32 (Browser Global)
import { UINT32 } from 'cuint/build/uint32.js';// After including <script src="/your/path/to/uint32.js"></script> const val = UINT32('326648991');
Quickstart
// In Node.js environment
const { UINT32, UINT64 } = require('cuint');
// Instantiate a 32-bit unsigned integer
const thirtyTwoBitValue = UINT32('4294967290'); // Close to max 32-bit unsigned value
console.log(`Initial UINT32 value: ${thirtyTwoBitValue.toString()}`);
// Instantiate another 32-bit value
const smallThirtyTwoBitValue = UINT32(10);
// Perform an addition (modifies thirtyTwoBitValue in place)
thirtyTwoBitValue.add(smallThirtyTwoBitValue);
console.log(`UINT32 after adding 10: ${thirtyTwoBitValue.toString()}`); // Will demonstrate truncation if it overflows
// Instantiate a 64-bit unsigned integer
const sixtyFourBitValue = UINT64('18446744073709551600'); // Close to max 64-bit unsigned value
console.log(`Initial UINT64 value: ${sixtyFourBitValue.toString()}`);
// Instantiate another 64-bit value
const smallSixtyFourBitValue = UINT64(15);
// Perform an addition (modifies sixtyFourBitValue in place)
sixtyFourBitValue.add(smallSixtyFourBitValue);
console.log(`UINT64 after adding 15: ${sixtyFourBitValue.toString()}`);
// Demonstrate cloning for immutable operations
const originalValue = UINT32(100);
const addedValue = UINT32(50);
const resultValue = originalValue.clone().add(addedValue);
console.log(`Original value (immutable check): ${originalValue.toString()}`); // Should still be 100
console.log(`Result of clone().add(): ${resultValue.toString()}`); // Should be 150