Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
A set of common utilities and type-guards.
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
, andisRegExp
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.
Add Package
deno add jsr:@oxi/core
Import symbol
import * as core from "@oxi/core";
Import directly with a jsr specifier
import * as core from "jsr:@oxi/core";
Add Package
pnpm i jsr:@oxi/core
pnpm dlx jsr add @oxi/core
Import symbol
import * as core from "@oxi/core";
Add Package
yarn add jsr:@oxi/core
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";