GoodbyeNJN's utility library for TypeScript and JavaScript, providing a collection of common utility functions and types.
- π Modern: Built with TypeScript, targeting ES modules
- π Type-safe: Full TypeScript support with comprehensive type definitions
- π¦ Modular: Import only what you need with tree-shakable exports
- π‘οΈ Result Type: Functional error handling with Result pattern
- π File System: Safe file system operations with Result types
- π§° Common Utils: String, math, promise, process, and other utility functions
- π Remeda Extensions: Extended utilities built on top of Remeda
npm install @goodbyenjn/utils
# or
pnpm add @goodbyenjn/utils
# or
yarn add @goodbyenjn/utils
import { sleep, template, unindent, debounce, $ } from "@goodbyenjn/utils";
// Promise utilities
await sleep(1000); // Sleep for 1 second
// String utilities
const text = template("Hello, {name}!", { name: "World" });
console.log(text); // "Hello, World!"
const code = unindent`
function example() {
return 'formatted';
}
`;
// Process utilities - Execute shell commands safely
const result = await $`ls -la`;
if (result.isOk()) {
console.log("stdout:", result.value.stdout);
console.log("stderr:", result.value.stderr);
} else {
console.error("Command failed:", result.error);
}
// Throttling and debouncing
const debouncedFn = debounce(() => console.log("Called!"), 300);
import { safeReadFile, safeWriteFile, safeExists } from "@goodbyenjn/utils/fs";
// Safe file operations that return Result types
const fileResult = await safeReadFile("config.json", "utf8");
if (fileResult.isOk()) {
console.log("File content:", fileResult.value);
} else {
console.error("Failed to read file:", fileResult.error);
}
// Check if file exists
const existsResult = await safeExists("path/to/file.txt");
if (existsResult.isOk() && existsResult.value) {
console.log("File exists!");
}
import { Ok, Err, Result } from "@goodbyenjn/utils/result";
// Create results
const success = Ok(42);
const failure = Err("Something went wrong");
// Handle results
if (success.isOk()) {
console.log("Value:", success.value); // 42
}
// Convert throwing functions to Result
const safeParseInt = Result.fromThrowable(parseInt);
const result = safeParseInt("not-a-number");
if (result.isErr()) {
console.log("Parse failed:", result.error);
}
import { hasOwnProperty, isPromiseLike } from "@goodbyenjn/utils/remeda";
// Type-safe property checking
const obj = { name: "John", age: 30 };
if (hasOwnProperty(obj, "name")) {
console.log(obj.name); // TypeScript knows this is safe
}
// Promise detection
if (isPromiseLike(someValue)) {
const result = await someValue;
}
import type { Nullable, YieldType } from "@goodbyenjn/utils/types";
// Nullable type for values that can be null or undefined
type User = {
id: string;
name: string;
email: Nullable<string>; // string | null | undefined
};
// Extract yield type from generators
function* numberGenerator() {
yield 1;
yield 2;
yield 3;
}
type NumberType = YieldType<typeof numberGenerator>; // number
- Main (
@goodbyenjn/utils
) - Common utilities (string, math, promise, process, error handling, etc.) - File System (
@goodbyenjn/utils/fs
) - Safe file system operations - Result (
@goodbyenjn/utils/result
) - Functional error handling - Remeda (
@goodbyenjn/utils/remeda
) - Extended Remeda utilities - Types (
@goodbyenjn/utils/types
) - Utility types for TypeScript
template(str, vars)
- Simple string templatingunindent(str)
- Remove common indentationaddPrefix/removePrefix
- Prefix manipulationaddSuffix/removeSuffix
- Suffix manipulationtoForwardSlash
- Convert backslashes to forward slashes
sleep(ms)
- Async sleep functioncreateLock()
- Create a mutex lockcreateSingleton(factory)
- Create singleton factoryPromiseWithResolvers
- Promise with external resolvers
$(command)
- Execute shell commands safely, returns ResultAsync with stdout/stderr
linear(value, range)
- Linear interpolationscale(value, inRange, outRange)
- Scale value between ranges
normalizeError(error)
- Normalize any value to ErrorgetErrorMessage(error)
- Extract error message safely
debounce(fn, delay, options)
- Debounce function callsthrottle(fn, delay, options)
- Throttle function calls
- Node.js >= 18
- TypeScript >= 4.5 (for TypeScript users)
# Install dependencies
pnpm install
# Build the library
pnpm run build
# Clean build artifacts
pnpm run clean
MIT Β© GoodbyeNJN