Skip to main content

@oxi/core@0.3.2

A set of common utilities and type-guards.

Works with
This package works with Bun
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Browsers
JSR Score
100%
Published
3 months ago (0.3.2)

Utility Functions Module

Overview

This module provides a collection of TypeScript utility functions designed to enhance type checking and validation in your applications. These utilities extend the basic capabilities of TypeScript's type system, allowing for more precise runtime type assertions and data manipulation.

Utilities Included:

  • assert(condition: any, message?: string, options?: ErrorOptions): asserts condition: Asserts that a condition is truthy, throwing an error with an optional message and options if not.

  • isDefined<T>(value: T): value is NonNullable<T>: Checks if a value is neither null nor undefined, ensuring it is defined.

  • isIterable(value: unknown): value is Iterable<unknown>: Determines whether a value is iterable, supporting checks for arrays, strings, Maps, Sets, and other iterable objects.

  • getTypeOf(value: unknown): string: Returns the type of the given value as a lowercase string, handling a wide range of JavaScript types and edge cases.

  • Type-checking utilities: A suite of functions like isString, isNumber, isBoolean, isBigInt, isSymbol, isObject, isArray, isPlainObject, isFunction, isPromise, isDate, isError, isMap, isSet, and isRegExp to validate various JavaScript types.

Usage

Import the utilities as needed into your TypeScript files to perform runtime checks, validate data, or assert conditions. These utilities are especially useful in scenarios where TypeScript's compile-time type system isn't sufficient, such as when dealing with data from external sources or user input.

Example:

import { assert, isString, getTypeOf } from '@oxi/core';

const data: unknown = fetchDataFromAPI();

assert(isString(data), 'Data must be a string');
console.log(`Data is of type: ${getTypeOf(data)}`);

Notes

While these utilities enhance runtime type safety and data validation, they complement rather than replace TypeScript's compile-time type checks. Use them judiciously to avoid redundant checks or impacting performance.

Built and signed on
GitHub Actions
View transparency log

Add Package

deno add @oxi/core

Import symbol

import * as mod from "@oxi/core";

Add Package

npx jsr add @oxi/core

Import symbol

import * as mod from "@oxi/core";

Add Package

yarn dlx jsr add @oxi/core

Import symbol

import * as mod from "@oxi/core";

Add Package

pnpm dlx jsr add @oxi/core

Import symbol

import * as mod from "@oxi/core";

Add Package

bunx jsr add @oxi/core

Import symbol

import * as mod from "@oxi/core";