Skip to main content
Home

@oxi/core@0.3.2
Built and signed on GitHub Actions

A set of common utilities and type-guards.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
a year 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

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@oxi/core

Import symbol

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

Import directly with a jsr specifier

import * as core from "jsr:@oxi/core";

Add Package

pnpm i jsr:@oxi/core
or (using pnpm 10.8 or older)
pnpm dlx jsr add @oxi/core

Import symbol

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

Add Package

yarn add jsr:@oxi/core
or (using Yarn 4.8 or older)
yarn dlx jsr add @oxi/core

Import symbol

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

Add Package

vlt install jsr:@oxi/core

Import symbol

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

Add Package

npx jsr add @oxi/core

Import symbol

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

Add Package

bunx jsr add @oxi/core

Import symbol

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