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

castium

v1.0.25

Published

A lightweight and chainable utility for effortless data type conversation in JavaScript and Node.js

Downloads

221

Readme

Castium

Castium is a lightweight TypeScript utility for safe and chainable type conversion in JavaScript and TypeScript. It allows you to transform values between types with ease while handling edge cases gracefully.

Installation

npm install castium

or with yarn:

yarn add castium

Usage

Import the c function and use it to create a chainable conversion.

import { c } from "castium";

const result = c("0  ").string().number().boolean().get();
console.log(result); // false

API Methods

Below is a list of available methods in Castium, along with examples:


.number(defaultValue?)

Converts the value to a number.

c("42").number().get(); // 42
c("abc").number().get(); // null
c("abc").number(0).get(); // 0 (default value provided)
c("۱۲۳").number().get(); // 123
c("٣٤٥.٧").number().get(); // 345.7
c("1,200 تومان").number().get(); // 1200
c("text").number(0).get(); // 0 (default value provided)
c("  ٥٠٠٠  ").number().get(); // 5000
c("۴۲.۵۶").number().get(); // 42.56
c("12.34.56").number().get(); // null (default value provided)

.string()

Converts the value to a trimmed string.

c(42).string().get(); // "42"
c(null).string().get(); // ""
c({ key: "value" }).string().get(); // "{"key":"value"}"

.boolean()

Converts the value to a boolean.

c(1).boolean().get(); // true
c(0).boolean().get(); // false
c("true").boolean().get(); // true
c("false").boolean().get(); // true (non-empty string is truthy)

.booleanString()

Safely converts "true" or "false" string values to actual booleans.

c("true").booleanString().get(); // true
c("false").booleanString().get(); // false
c("random").booleanString().get(); // null

.date()

Converts the value to a valid JavaScript Date object.

c("2023-12-25").date().get(); // Date object (Dec 25, 2023)
c("invalid").date().get(); // null

.isoDate()

Converts the value to an ISO date string.

c("2023-12-25").isoDate().get(); // "2023-12-25T00:00:00.000Z"
c("invalid").isoDate().get(); // null

.fromDate()

Sets the time of a date object to 00:00:00 (start of the day).

c("2023-12-25").fromDate().get(); // Date object at 00:00:00

.toDate()

Sets the time of a date object to 23:59:59.999 (end of the day).

c("2023-12-25").toDate().get(); // Date object at 23:59:59.999

.dateTime()

Converts the date to a timestamp (getTime()).

c("2023-12-25").dateTime().get(); // 1703462400000

.array()

Parses a JSON string into an array.

c("[1,2,3]").array().get(); // [1, 2, 3]
c("invalid").array().get(); // null

.object()

Parses a JSON string into an object.

c('{"key":"value"}').object().get(); // { key: "value" }
c("invalid").object().get(); // null

.nullable()

Converts empty strings, null, and undefined to null.

c("").nullable().get(); // null
c("hello").nullable().get(); // "hello"

.undefined()

Converts empty strings and null to undefined.

c("").undefined().get(); // undefined

.default(defaultValue)

Provides a fallback value when the original value is null, undefined, or an empty string.

c(null).default("fallback").get(); // "fallback"

.transform(fn, defaultValue?)

Applies a transformation function to the value.

c("5")
  .transform((v) => Number(v) * 2)
  .get(); // 10

.json()

Parses a JSON string.

c('{"a": 1}').json().get(); // { a: 1 }
c("invalid").json().get(); // null

.match(regex)

Checks if a string matches a regular expression.

c("hello").match(/^h/).get(); // true
c("world").match(/^h/).get(); // false

.oneOf(...values)

Checks if the value is in a given set.

c("apple").oneOf("apple", "banana").get(); // true
c("orange").oneOf("apple", "banana").get(); // false

.clamp(min, max)

Restricts a number to be within a range.

c(5).clamp(1, 10).get(); // 5
c(-2).clamp(1, 10).get(); // 1
c(20).clamp(1, 10).get(); // 10

Conclusion

Castium simplifies type conversions in JavaScript and TypeScript while handling edge cases. It’s ideal for processing API responses, user inputs, and dynamic data sources efficiently.

Try it now and enjoy seamless data transformations! 🚀