JSPM

  • Created
  • Published
  • Downloads 46
  • Score
    100M100P100Q59086F
  • License MIT

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

Package Exports

  • castium
  • castium/dist/index.js
  • castium/dist/index.mjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (castium) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Castium

Castium is a lightweight, chainable TypeScript/JavaScript utility for safely converting and transforming data types.

Installation

You can install Castium using npm:

npm install castium

Or using yarn:

yarn add castium

Usage

Import Castium and use it to safely convert values:

import { c } from "castium";

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

Features

Convert to Number

c("42").number().get(); // 42
c("invalid").number().get(); // null
c(null).number(0).get(); // 0 (default value)

Convert to String

c(123).string().get(); // "123"
c(null).string().get(); // ""

Convert to Boolean

c(1).boolean().get(); // true
c(0).boolean().get(); // false

Convert Boolean Strings

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

Convert to Date

c("2023-01-01").date().get(); // Date object
c("invalid").date().get(); // null

Convert to ISO Date String

c("2023-01-01").isoDate().get(); // "2023-01-01T00:00:00.000Z"

Convert to Start/End of Day

c("2023-01-01").fromDate().get(); // 2023-01-01T00:00:00.000Z
c("2023-01-01").toDate().get(); // 2023-01-01T23:59:59.999Z

Convert to Date Timestamp

c("2023-01-01").dateTime().get(); // 1672444800000

Convert to Array

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

Convert to Object

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

Handle Null or Undefined Values

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

Transform Value with a Custom Function

c(2)
  .transform((x) => x * 2)
  .get(); // 4

Compare Values

c(10).equal(10).get(); // true
c("hello").equal("world").get(); // false

Repository

Find the full source code and contribute on GitHub:

GitHub Repository

License

MIT License