e: Modern TypeScript Utility Library
The 'e' library is a modern, universal utility collection specifically designed for TypeScript and Node.js environments. Currently at version 0.2.33, it emphasizes immutability, ensuring that none of its functions mutate or alter any passed arguments, promoting predictable side-effect-free code. Its design philosophy centers on simplicity, with each exported function serving a single, clear purpose. While currently in an early 0.x.x release cycle, it aims to provide a reliable, lightweight alternative to larger utility libraries, focusing on a clean API and strong TypeScript support from the ground up. The library is actively developed and geared towards contemporary JavaScript development practices.
Common errors
-
TypeError: require is not a function
cause Attempting to use `require()` to import `e` in an ES Module context, or in a Node.js project not configured for CommonJS compatibility.fixChange `const { symbol } = require('e');` to `import { symbol } from 'e';`. Ensure your `package.json` has `"type": "module"` if you are in an ESM project, or use a build tool to transpile. -
TS2305: Module ''e'' has no exported member 'MyFunction'.
cause Attempting to import a symbol (e.g., `MyFunction`) that either does not exist, is misspelled, or is not a named export from the 'e' package.fixVerify the symbol name against the 'e' documentation or source code. Ensure it is a named export, as 'e' does not use default exports. For example, use `import { correctFunctionName } from 'e';`. -
TypeError: Cannot read properties of undefined (reading 'length')
cause This error can occur if you pass `undefined` or `null` to a function expecting an array, or if you call a method on the result of an immutable operation without reassigning it, expecting the original to be modified.fixAlways ensure arguments passed to 'e' functions are of the expected type. Also, remember that 'e' functions are immutable; always capture their return value: `const newArray = shuffleArr(myArray);`
Warnings
- breaking As a 0.x.x version library, the API of 'e' is considered unstable and subject to frequent breaking changes with minor version updates. Always review the changelog before upgrading.
- gotcha All functions in 'e' are designed to be immutable, meaning they return new instances (e.g., a new array after shuffling) rather than modifying the input arguments. Failing to reassign the return value can lead to using stale data.
- gotcha The library is primarily built for ES Modules (ESM) environments. Using CommonJS `require()` in a Node.js project not configured for dual-package mode might result in import errors or unexpected behavior.
Install
-
npm install e -
yarn add e -
pnpm add e
Imports
- sleep
const { sleep } = require('e');import { sleep } from 'e'; - randFloat
import randFloat from 'e';
import { randFloat } from 'e'; - shuffleArr
import { shuffleArr } from 'e';
Quickstart
import { sleep, randFloat, shuffleArr } from 'e';
async function demonstrateE() {
console.log('Starting e library demonstration...');
// Demonstrate asynchronous delay
console.log('Pausing for 1000 milliseconds...');
await sleep(1000);
console.log('Resumed after sleep.');
// Generate a random floating-point number
const randomNumericValue = randFloat();
console.log(`Generated a random float between 0 and 1: ${randomNumericValue.toFixed(4)}`);
// Shuffle an array without mutating the original
const originalNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log('Original array:', originalNumbers);
const shuffledNumbers = shuffleArr(originalNumbers);
console.log('Shuffled array (new instance):', shuffledNumbers);
console.log('Original array remains unchanged:', originalNumbers);
// Verify non-mutation explicitly
if (originalNumbers === shuffledNumbers) {
console.error('CRITICAL ERROR: Array mutation detected! This should not happen in \'e\'.');
} else {
console.log('Non-mutation confirmed: original and shuffled arrays are distinct instances.');
}
console.log('e library demonstration complete.');
}
demonstrateE().catch(console.error);