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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@wopjs/cast

v0.1.12

Published

Type-safe utilities for filtering and coercing unknown values in TypeScript.

Readme

@wopjs/cast

Docs Build Status npm-version Coverage Status minified-size

Type-safe utilities for filtering and coercing unknown values in TypeScript.

Features

  • Three-tier API pattern (is/to/as) for flexible type filtering
  • Strong TypeScript type narrowing that preserves complex types (unions, tuples, readonly arrays)
  • Zero dependencies and tree-shakeable
  • Safe edge case handling (NaN, null, undefined, circular references)

Install

npm add @wopjs/cast

API Pattern

Every type has up to three functions following a consistent naming pattern:

| Pattern | Returns | Use Case | | ------- | ---------------- | --------------------------------------------- | | is* | boolean | Type guards for conditional narrowing | | to* | T \| undefined | Optional values, composable with Option types | | as* | T | Always returns valid value with fallback |

import { isNumber, toNumber, asNumber } from "@wopjs/cast";

// is* - Type guard for conditionals
if (isNumber(value)) {
  value; // type narrowed to number
}

// to* - Returns undefined for invalid input
const num = toNumber(input); // number | undefined

// as* - Always returns a number (0 as fallback)
const safeNum = asNumber(input); // number

Type Narrowing

The library preserves and correctly narrows complex TypeScript types:

import { isArray, toArray, isTruthy } from "@wopjs/cast";

// Preserves array element types
const arr: string[] = ["a", "b"];
if (isArray(arr)) {
  arr; // still string[], not unknown[]
}

// Extracts from unions
const value: string | string[] = getData();
if (isArray(value)) {
  value; // narrowed to string[]
}

// Preserves tuples and readonly arrays
const tuple: [string, number] = ["a", 1];
const result = toArray(tuple); // [string, number] | undefined

// Excludes falsy types
const val: string | null | undefined = "hello";
if (isTruthy(val)) {
  val; // narrowed to string
}

Available Functions

Booleans

| Function | Description | | ----------- | -------------------------------------------- | | isTrue | Returns true if value is exactly true | | toTrue | Returns true or undefined | | asTrue | Returns true or false | | isTruthy | Returns true if Boolean(x) is true | | toTruthy | Returns truthy value or undefined | | isFalsy | Returns true if Boolean(x) is false | | toFalsy | Returns falsy value or undefined | | isBoolean | Returns true if value is true or false | | toBoolean | Returns boolean or undefined |

Numbers

| Function | Description | | ---------- | ----------------------------------------------------- | | isNumber | Returns true if value is a number (excluding NaN) | | toNumber | Returns number or undefined | | asNumber | Returns number or 0 |

Strings

| Function | Description | | ------------------ | --------------------------------------------- | | isString | Returns true if value is a string | | toString | Returns string or undefined | | asString | Returns string or "" | | isNonEmptyString | Returns true if value is a non-empty string | | toNonEmptyString | Returns non-empty string or undefined |

Arrays

| Function | Description | | ----------------- | ----------------------------------------------- | | isArray | Type guard for arrays (preserves element types) | | toArray | Returns array or undefined | | asArray | Returns array or [] | | isNonEmptyArray | Type guard for non-empty arrays | | toNonEmptyArray | Returns non-empty array or undefined |

Objects

| Function | Description | | ----------------------- | --------------------------------------------------------------- | | isObject | Returns true for objects (including arrays, excluding null) | | toObject | Returns object or undefined | | asObject | Returns object or {} | | isPlainObject | Returns true for plain objects (excluding arrays) | | toPlainObject | Returns plain object or undefined | | asPlainObject | Returns plain object or {} | | isNonEmptyPlainObject | Returns true for objects with at least one key | | toNonEmptyPlainObject | Returns non-empty object or undefined | | isNonEmptyJSONObject | Returns true for objects with non-undefined values | | toNonEmptyJSONObject | Returns JSON object or undefined |

Utilities

| Function | Description | | --------------------- | ------------------------------------------ | | isDefined | Returns true if value is not undefined | | toPlainObjectOf | Filter object values by type predicate | | toPlainObjectOfTrue | Filter object to only true values | | print | Safely stringify any value for display |

Return Helpers

Constant functions useful for callbacks and default values:

| Function | Returns | | -------------------- | ----------- | | noop | void | | returnsUndefined | undefined | | returnsNull | null | | returnsFalse | false | | returnsTrue | true | | returnsEmptyString | "" |

Usage Example

import {
  asPlainObject,
  asString,
  toNumber,
  asArray,
  toNonEmptyPlainObject,
  toPlainObjectOfTrue,
  isNumber,
  asNumber,
} from "@wopjs/cast";

// Parsing unknown API response
function parseUser(data: unknown) {
  const obj = asPlainObject(data);
  return {
    name: asString(obj.name),
    age: toNumber(obj.age), // undefined if not a number
    tags: asArray(obj.tags),
    settings: toNonEmptyPlainObject(obj.settings),
  };
}

// Filtering object values
const config = { debug: true, verbose: false, enabled: true };
toPlainObjectOfTrue(config); // { debug: true, enabled: true }

// Safe number handling
isNumber(NaN); // false (NaN is excluded)
asNumber(NaN); // 0 (safe fallback)
asNumber("42"); // 0 (not coerced, use parseInt for that)