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 castiumOr using yarn:
yarn add castiumUsage
Import Castium and use it to safely convert values:
import { c } from "castium";
const result = c("123").number().get(); // 123Features
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(); // falseConvert Boolean Strings
c("true").booleanString().get(); // true
c("false").booleanString().get(); // false
c("random").booleanString().get(); // nullConvert to Date
c("2023-01-01").date().get(); // Date object
c("invalid").date().get(); // nullConvert 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.999ZConvert to Date Timestamp
c("2023-01-01").dateTime().get(); // 1672444800000Convert to Array
c("[1,2,3]").array().get(); // [1, 2, 3]
c("invalid").array().get(); // nullConvert to Object
c('{"key": "value"}').object().get(); // { key: "value" }
c("invalid").object().get(); // nullHandle 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(); // 4Compare Values
c(10).equal(10).get(); // true
c("hello").equal("world").get(); // falseRepository
Find the full source code and contribute on GitHub:
License
MIT License