Utilium: TypeScript Utilities
Utilium is a comprehensive TypeScript utility library offering a diverse set of functionalities for both compile-time and runtime operations. The current stable version is 3.1.1, with recent releases indicating active development, typically involving feature additions or minor bug fixes within patch versions, and significant refactorings in major versions like 3.0.0. Key differentiators include advanced compile-time mathematical types, utilities for HTTP range requests using `fetch`, robust `Buffer` extension capabilities, and convenience functions for strings and objects. It also features a unique `List` class that merges array and `Set` functionalities, `JSONFileMap` for file system interactions, and specialized tools for Xterm.js shell handling. The library targets modern Node.js environments, requiring Node.js >=22.0.0, and ships with full TypeScript support.
Common errors
-
Cannot find module 'utilium/sub.js'
cause Attempting to import a subpath with a `.js` extension after version 3.0.0.fixRemove the `.js` extension from the import path: `import { example } from 'utilium/sub'`. -
TypeError: (0 , utilium_1.List) is not a constructor
cause Attempting to use CommonJS `require` for ESM-only exports. Utilium is primarily an ESM library.fixMigrate your project to use ES Modules (ESM) and `import` statements, or configure your bundler/TypeScript compiler to correctly handle ESM interoperability. E.g., `import { List } from 'utilium'`. -
Property 'RequestError' does not exist on type 'typeof import("utilium")'cause Accessing a top-level export that was removed or moved under a namespace in v3.0.0.fixRefer to the v3.0.0 release notes. `RequestError` was replaced by `requests.Issue`. Update your code to `import { requests } from 'utilium'; const error: requests.Issue = { ... };`.
Warnings
- breaking Version 3.0.0 introduced significant breaking changes by removing numerous top-level exports and consolidating them under namespaces. For instance, `ResourceCache`, `RequestError`, and `Increment` were removed, with their functionalities moved to `cache.Resource`, `requests.Issue`, and `Add<..., 1>` respectively.
- breaking Since v3.0.0, direct subpath imports no longer require the `.js` file extension. Importing `utilium/sub.js` will fail.
- gotcha The `@memoize` decorator had several stability fixes between versions 2.8.2 and 2.8.8. Older versions might exhibit incorrect caching behavior, particularly with auto-accessors.
- gotcha Utilium requires Node.js version 22.0.0 or higher. Running in older Node.js environments will lead to errors.
Install
-
npm install utilium -
yarn add utilium -
pnpm add utilium
Imports
- List
const { List } = require('utilium')import { List } from 'utilium' - splitIntoArgs
import { splitIntoArgs } from 'utilium' - @memoize
import { memoize } from 'utilium'; class MyClass { @memoize myMethod() { ... } } - requests
import { requests } from 'utilium'
Quickstart
import { List, splitIntoArgs, requests } from 'utilium';
// Demonstrate the List class
const myList = new List([1, 2, 2, 3]);
console.log(`Initial List: ${myList.toArray()}`); // Output: Initial List: 1,2,3
myList.add(4);
myList.remove(2);
console.log(`Modified List: ${myList.toArray()}`); // Output: Modified List: 1,3,4
// Demonstrate splitIntoArgs
const args = splitIntoArgs('command --flag value -x "quoted arg"');
console.log('Parsed arguments:', args); // Output: Parsed arguments: [ 'command', '--flag', 'value', '-x', 'quoted arg' ]
// Demonstrate usage of nested exports (requests namespace)
const issue: requests.Issue = {
code: 'FETCH_ERROR',
message: 'Network request failed'
};
console.log('Request Issue:', issue);
// You can also demonstrate compile-time types, but they don't produce runtime output directly.
// Example of a compile-time type (cannot be run directly, but shows intent):
// import type { Add } from 'utilium/types';
// type Sum = Add<5, 3>; // Sum will be 8 at compile-time