@ifty64bit/utils
v0.2.0
Published
My daily reusable utilities
Readme
@ifty64bit/utils
A collection of daily reusable TypeScript utilities.
Installation
npm install @ifty64bit/utilsUsage
import { tryCatch, createEnum, formatCompact } from "@ifty64bit/utils";API
formatCompact(value: number, options?: FormatCompactOptions): string
Converts a number into a compact, human-readable string.
| Option | Type | Default | Description |
|---|---|---|---|
| decimals | number | 1 | Maximum decimal places shown |
| uppercase | boolean | false | Uppercase suffix (K vs k) |
Example:
formatCompact(1000) // "1k"
formatCompact(1500) // "1.5k"
formatCompact(10_000) // "10k"
formatCompact(1_000_000) // "1m"
formatCompact(1_500_000, { decimals: 2 }) // "1.5m"
formatCompact(10_000, { uppercase: true }) // "10K"tryCatch<T>(fn: () => T): TryCatchResult<T>
Wraps a function execution in a try-catch block and returns a result object instead of throwing errors.
Returns:
{ result: T, error: null }on success{ result: null, error: Error }on failure
Example:
const { result, error } = tryCatch(() => JSON.parse('{"valid": "json"}'));
if (error) {
console.error("Parsing failed:", error.message);
} else {
console.log("Parsed data:", result);
}createEnum<T>(obj: T): Frozen<T & { reverse: ReverseMap }>
Creates a frozen, enum-like object with reverse lookup capability.
Returns: A frozen object containing the original mapping and a reverse property for value-to-key lookups.
Example:
const Status = createEnum({
PENDING: 0,
ACTIVE: 1,
COMPLETED: 2,
});
console.log(Status.ACTIVE); // 1
console.log(Status.reverse[1]); // "ACTIVE"Development
Install dependencies
bun installBuild
bun run buildType check
bun run checkLicense
MIT
