UNPKG

castium

Version:

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

145 lines (144 loc) 4.23 kB
// src/index.ts var Castium = class _Castium { value; constructor(value) { this.value = value; } number(defaultValue) { let strValue = this.string().get(); strValue = strValue.replace(/[٠١٢٣٤٥٦٧٨٩]/g, (d) => String(d.charCodeAt(0) - 1632)).replace(/[۰۱۲۳۴۵۶۷۸۹]/g, (d) => String(d.charCodeAt(0) - 1776)); strValue = strValue.replace(/[^0-9.]/g, ""); const dotCount = (strValue.match(/\./g) || []).length; if (dotCount > 1) return new _Castium(defaultValue ?? null); if (strValue === "" || strValue === null || strValue === void 0) return new _Castium(defaultValue ?? null); const newValue = Number(strValue); return new _Castium( isNaN(newValue) ? defaultValue ?? null : newValue ); } string() { if (this.value === null || this.value === void 0) return new _Castium(""); if (typeof this.value === "object") return new _Castium(JSON.stringify(this.value)); return new _Castium(String(this.value).trim()); } boolean() { return new _Castium(Boolean(this.value)); } isEqual(expected) { return new _Castium(this.value === expected); } booleanString() { const str = this.string().get().toLowerCase(); if (str === "true") return new _Castium(true); if (str === "false") return new _Castium(false); return new _Castium(null); } date() { if (this.value === "" || this.value === null || this.value === void 0) return new _Castium(null); const parsedDate = new Date(this.value); return new _Castium(isNaN(parsedDate.getTime()) ? null : parsedDate); } isoDate() { const dateValue = this.date().get(); return dateValue ? new _Castium(dateValue.toISOString()) : new _Castium(null); } fromDate() { const dateCaster = this.date(); if (dateCaster.get()) { dateCaster.get().setHours(0, 0, 0, 0); } return dateCaster; } toDate() { const dateCaster = this.date(); if (dateCaster.get()) { dateCaster.get().setHours(23, 59, 59, 999); } return dateCaster; } dateTime() { const dateCaster = this.date(); return dateCaster.get() ? new _Castium(dateCaster.get().getTime()) : new _Castium(null); } array() { if (Array.isArray(this.value)) return new _Castium(this.value); try { const parsed = JSON.parse(this.string().get()); return new _Castium(Array.isArray(parsed) ? parsed : null); } catch { return new _Castium(null); } } object() { if (typeof this.value === "object" && !Array.isArray(this.value) && this.value !== null) { return new _Castium(this.value); } try { const parsed = JSON.parse(this.string().get()); return new _Castium( typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed : null ); } catch { return new _Castium(null); } } nullable() { return new _Castium( this.value === "" || this.value === void 0 ? null : this.value ); } undefined() { return new _Castium( this.value === "" || this.value === null ? void 0 : this.value ); } default(defaultValue) { const value = this.get(); return new _Castium( value === null ? defaultValue : value ); } transform(fn, defaultValue) { try { return new _Castium(fn(this.value)); } catch { return new _Castium(defaultValue ?? null); } } json() { try { return new _Castium(JSON.parse(this.string().get())); } catch { return new _Castium(null); } } match(regex) { const str = this.string().get(); return new _Castium(regex.test(str)); } oneOf(...values) { const validValues = values.flat(); return new _Castium(validValues.includes(this.value)); } clamp(min, max) { const num = this.number().get(); if (num === null) return new _Castium(min); return new _Castium(Math.min(Math.max(num, min), max)); } enum(enumObj) { const values = Object.values(enumObj); const value = this.get(); const includes = values.includes(value); return new _Castium(includes ? value : null); } get() { return this.value; } }; var c = (value) => new Castium(value); export { c };