Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@ifty64bit/utils

A collection of daily reusable TypeScript utilities.

Installation

npm install @ifty64bit/utils

Usage

import { tryCatch, createEnum } from "@ifty64bit/utils";

API

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 install

Build

bun run build

Type check

bun run check

License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages