castium
Version:
A lightweight and chainable utility for effortless data type conversation in JavaScript and Node.js
181 lines (179 loc) • 5.06 kB
JavaScript
// src/log.ts
function log(error) {
console.warn("Castium: ", error);
}
// src/index.ts
var Castium = class _Castium {
value;
constructor(value) {
this.value = value;
}
number() {
let strValue = this.string().get();
strValue = strValue.replace(/[۰۱۲۳۴۵۶۷۸۹۰۱۲۳٤٥٦۷۸۹]/g, (d) => String(d.charCodeAt(0) & 15)).replace(/[^0-9.-]/g, "");
if (strValue.indexOf(".") !== strValue.lastIndexOf(".") || strValue === "" || strValue === null || strValue === void 0) {
return new _Castium(null);
}
const newValue = Number(strValue);
return new _Castium(isNaN(newValue) ? 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);
if (typeof this.value === "string") {
try {
const parsed = JSON.parse(this.value);
return new _Castium(Array.isArray(parsed) ? parsed : null);
} catch {
return new _Castium(null);
}
}
return new _Castium(null);
}
object() {
if (typeof this.value === "object" && !Array.isArray(this.value) && this.value !== null) {
return new _Castium(this.value);
}
if (typeof this.value === "string") {
try {
const parsed = JSON.parse(this.value);
return new _Castium(
typeof parsed === "object" && parsed !== null && !Array.isArray(parsed) ? parsed : null
);
} catch {
return new _Castium(null);
}
}
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 || value === void 0 ? 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);
}
shape(schema) {
const source = this.object().default({}).get();
const result = {};
for (const key in schema) {
const rule = schema[key];
const isFunction = typeof rule === "function";
if (isFunction) {
try {
result[key] = rule(source[key]);
} catch (error) {
log(error);
result[key] = null;
}
} else {
result[key] = rule;
}
}
return new _Castium(result);
}
when(predicate) {
try {
const result = typeof predicate === "function" ? predicate(this.value) : predicate;
if (result) return new _Castium(this.value);
else return new _Castium(null);
} catch (error) {
log(error);
return new _Castium(null);
}
}
get() {
return this.value;
}
};
var c = (value) => new Castium(value);
export {
c
};