encode32
encode32 is a utility for Base32 encoding 32-bit unsigned integers, drawing inspiration from Douglas Crockford's Base32 specification. Released as version 1.1.0, its release cadence is effectively abandoned, with the last publish being over 8 years ago and targeting Node.js versions as old as 0.4.7. Key differentiators include its design for human-friendliness and robustness, using 32 digits with case-insensitivity and aliases for easily confused characters (e.g., 'l' and 'I' for '1', 'o' for '0'). It notably excludes the character 'U' to avoid accidental obscenities. Instead of Crockford's suggested 'mod 37 checksum', this implementation incorporates a 3-bit parity checksum into the final character, enabling quick sanity checks without increasing length, but making it incompatible with other standard Base32 implementations. It is specifically designed for 32-bit numbers, though later additions (mentioned in the original README) hinted at support for up to 53-bit numbers with parity.
Common errors
-
TypeError: require is not a function
cause Attempting to use `require()` in an ES module context, or vice-versa, attempting `import` for a CommonJS-only package.fixIf in an ESM file, convert `import { encode32, decode32 } from 'encode32';` to `const { encode32, decode32 } = await import('encode32');` or, preferably, switch to a modern library that officially supports ESM. If in a CJS file, ensure `require()` is used correctly. -
NaN (Not a Number) returned from decode32
cause The input string to `decode32` is not a valid `encode32` string (e.g., incorrect format, invalid characters, or failed parity check).fixEnsure the input string is a correctly formatted 7-character Base32 string that was originally encoded by this library, and verify no characters are transposed or missing. The library's parity check is strict. -
RangeError: Value out of range for uint32
cause While not explicitly documented, attempting to encode a number larger than 2^32 - 1 (the maximum for a 32-bit unsigned integer) into `encode32` might lead to incorrect encoding or a runtime error, as the library is designed for 32-bit numbers.fixPre-validate numbers to ensure they are within the 32-bit unsigned integer range (0 to 4,294,967,295) before passing them to `encode32`. For larger numbers, use a `BigInt`-compatible or 64-bit encoding library.
Warnings
- breaking The package's custom 3-bit parity checksum makes it incompatible with standard Crockford Base32 implementations, which use an optional 'mod 37 checksum' or no checksum at all. Encoded strings from `encode32` cannot be reliably decoded by other Crockford Base32 libraries, and vice-versa, without custom adaptation.
- gotcha This package is unmaintained, with its last update over 8 years ago and targeting very old Node.js versions (0.4.7). The 'TODO' list in the README indicates critical features like performance improvements and compatibility modes were never implemented, suggesting a lack of ongoing development and potential for unaddressed bugs or performance issues.
- gotcha The library explicitly states it's for 32-bit numbers, encoding into 7 base-32 digits. While the README mentions fixed-length encoders for 32 and 41-bit numbers, and generators up to 53 bits, the primary exported functions (encode32/decode32) are likely optimized or limited to 32-bit unsigned integers. Attempting to encode larger numbers may lead to incorrect results, silent truncation, or unexpected errors due to JavaScript's number representation.
- gotcha The library is CommonJS-only (`require`). Attempting to import it using ES module syntax (`import`) in a modern Node.js or browser environment configured for ESM will result in an error.
Install
-
npm install encode32 -
yarn add encode32 -
pnpm add encode32
Imports
- encode32
import { encode32 } from 'encode32';const { encode32 } = require('encode32'); - decode32
import decode32 from 'encode32';
const { decode32 } = require('encode32'); - entire module
import enc from 'encode32';
const enc = require('encode32');
Quickstart
const { encode32, decode32 } = require('encode32');
const originalNumber = 123456772;
const encodedString = encode32(originalNumber);
console.log(`Original Number: ${originalNumber}`); // Expected: 123456772
console.log(`Encoded String: ${encodedString}`); // Expected: 0XDWT16
// Decoding various case/alias forms
const variations = [
"0xdwt16", // lower case
"oXDWTi6", // o for 0 and i for 1
"OxDwtL6" // O for 0 and L for 1
];
const decodedNumbers = variations.map(s => decode32(s));
console.log(`Decoded variations: ${decodedNumbers}`); // Expected: [123456772, 123456772, 123456772]
// Demonstrating parity check failure
const invalidInputs = [
"0XDWT18", // incorrect final digit
"X0DWT16", // transposed digits
"0XDT16" // missing digit
];
const invalidDecodes = invalidInputs.map(s => decode32(s));
console.log(`Invalid decodes (expect NaN): ${invalidDecodes}`); // Expected: [NaN, NaN, NaN]