npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

castage

v1.1.0

Published

A type-safe library for dynamic object casting and ensuring type consistency in JavaScript/TypeScript.

Downloads

110

Readme

castage Coverage Status npm version npm downloads GitHub license

Castage is a TypeScript library for type-safe, dynamic object casting, ensuring runtime type validation while leveraging TypeScript's static type guarantees.

Installation

Run the following command to install:

npm install castage

Features

  • Type-safe casting: Validate and transform objects with strict type guarantees.
  • Runtime validation: Validate types beyond TypeScript's compile-time checks.
  • Flexible APIs: Support for primitive types, schemas, and custom validations.

API Documentation

Core Components

Caster<T>

The main interface for creating and managing type casters.

interface CasterFn<T> {
  (value: unknown, path?: string[]): Result<T, CastingError>;
}

interface UnpackCasterFn<T> {
  (value: unknown, path?: string[]): T;
}

interface Caster<T> extends CasterFn<T> {
  nullable: Caster<T | null>;
  optional: Caster<T | undefined>;
  unpack: UnwrapCasterFn<T>;
  validate<S extends T>(
    predicate: (value: T) => value is S, 
    name?: string, 
    error?: (value: T, path: string[]) => CastingError
  ): Caster<S>;
  match<S, E>(
    okMatcher: (data: T) => S, 
    errMatcher: (err: CastingError) => E
  ): UnpackCasterFn<S | E>;
  chain<S>(caster: Caster<S>): Caster<T & S>;
}

Where the Result type is imported from the resultage package.

CastingError

Describes an error encountered during the casting process.

interface CastingError extends Error {
  [$castingError]: true;
  code: CastingErrorCode;
  path: string[];
  extra: Extra;
}

Predefined Casters

Casters for commonly used types.

  • int: Ensures the value is an integer.
  • string: Ensures the value is a string.
  • boolean: Ensures the value is a boolean.
  • number: Ensures the value is a number.
  • struct: Ensures the value is a generic object.
  • array: Ensures the value is an array.
  • record: Ensures the value is a record.
  • tuple: Ensures the value is a tuple.
  • value: Ensures the value is a specific value.
  • values: Ensures the value is one of the specified values.
  • nill: Ensures the value is null.
  • undef: Ensures the value is undefined.

Example usage:

import { int, string } from 'castage';

const result = int(42);
if (result.isOk) {
  console.log(result.value); // 42
}

Advanced Utilities

array<T>(caster: CasterFn<T>, name?: string)

Validates an array of values using a specific caster.

import { array, int } from 'castage';

const intArrayCaster = array(int);
const result = intArrayCaster([1, 2, 3]);

if (result.isOk) {
  console.log(result.value); // [1, 2, 3]
}

struct<T>(casters: { [K in keyof T]: Caster<T[K]> }, name?: string)

Validates a structured object with specific type requirements.

import { struct, int, string } from 'castage';

const userCaster = struct({
  id: int,
  name: string
});

const result = userCaster({ id: 1, name: 'Alice' });

if (result.isOk) {
  console.log(result.value); // { id: 1, name: 'Alice' }
}

oneOf<T>(...casters: CasterFn<T>[])

Validates a value against multiple possible types.

import { oneOf, int, string } from 'castage';

const mixedTypeCaster = oneOf(int, string);

const result1 = mixedTypeCaster(42);
if (result1.isOk) {
  console.log(result1.value); // 42
}

const result2 = mixedTypeCaster('Alice');
if (result2.isOk) {
  console.log(result2.value); // 'Alice'
}

const result3 = mixedTypeCaster(true);

if (result3.isErr) {
  console.error(result3.error); // CastingError
}

Error Handling

Casting errors provide detailed information about failures:

interface CastingError {
  code: CastingErrorCode;
  path: string[];
  extra: Extra;
}

Use isCastingError to check for casting errors:

import { isCastingError } from 'castage';

if (isCastingError(error)) {
    console.error(error.path, error.code);
}