@base-framework/ui
Version:
This is a UI package that adds components and atoms that use Tailwind CSS and a theme based on Shadcn.
228 lines (227 loc) • 6.97 kB
JavaScript
import { DateTime as i } from "@base-framework/base";
const s = (t, r) => (typeof t == "string" && (t = [t]), Array.isArray(t) ? (t.push(r), t) : {
...t,
callBack: r
}), B = (t, r = "") => t != null && t !== "" ? t : r, p = {
/**
* Formats a number with commas.
*
* @param {string|number|object|array} watcher
* @param {string|null} defaultValue - Value if original is null or undefined.
* @returns {object|array}
*/
number(t, r = null) {
return s(t, (n) => {
if (!isNaN(n)) {
const l = /\B(?=(\d{3})+(?!\d))/g;
return n.toString().replace(l, ",");
}
return r || "";
});
},
/**
* Formats a boolean value as a yes/no string.
*
* @param {string|number|object|array} watcher
* @param {string} yes - Text for true values.
* @param {string} no - Text for false values.
* @returns {object|array}
*/
yesno(t, r = "Yes", c = "No") {
return s(t, (l) => l ? r : c);
},
/**
* Formats a value as money with two decimals.
*
* @param {string|number|object|array} watcher
* @param {string} currency - Currency symbol.
* @param {*} defaultValue - Value if original is invalid.
* @returns {object|array}
*/
money(t, r = "$", c = null) {
return s(t, (l) => {
const e = parseFloat(l);
if (isNaN(e))
return r + c || "";
const a = /\B(?=(\d{3})+(?!\d))/g;
return r + e.toFixed(2).toString().replace(a, ",");
});
},
/**
* Formats a value as a US phone number (10 digits).
*
* @param {string|object|array} watcher
* @param {*} defaultValue - Value if original is invalid.
* @returns {object|array}
*/
phone(t, r = null) {
return s(t, (n) => {
n = n || "";
const l = String(n.toString()).replace(/\D/g, "");
return l.length === 10 ? "(" + l.slice(0, 3) + ") " + l.slice(3, 6) + "-" + l.slice(6) : n || r;
});
},
/**
* Formats a value as an integer (rounds down).
*
* @param {string|number|object|array} watcher
* @param {*} defaultValue - Value if original is invalid.
* @returns {object|array}
*/
integer(t, r = null) {
return s(t, (n) => {
n = B(n, r);
const l = parseInt(n, 10);
return isNaN(l) ? r : l.toString();
});
},
/**
* Formats a date value to a standard date format.
*
* @param {string|number|object|array} watcher
* @param {*} defaultValue - Value if original is invalid.
* @returns {object|array}
*/
date(t, r = null) {
return s(t, (n) => n ? i.format("standard", n) : r || "");
},
/**
* Formats a date and time value to a standard date and time format.
*
* @param {string|number|object|array} watcher
* @param {*} defaultValue - Value if original is invalid.
* @returns {object|array}
*/
dateTime(t, r = null) {
return s(t, (n) => n ? i.format("standard", n) + " " + i.formatTime(n, 12) : r || "");
},
/**
* Formats a time value to a standard time format.
*
* @param {string|number|object|array} watcher
* @param {*} defaultValue - Value if original is invalid.
* @returns {object|array}
*/
time(t, r = null) {
return s(t, (n) => n ? i.formatTime(n, 12) : r || "");
},
/**
* Formats a value with a default value if null or undefined.
*
* @param {string|number|object|array} watcher
* @param {string|null} defaultValue - Value if original is null or undefined.
* @returns {object|array}
*/
default(t, r = null) {
return s(t, (n) => B(n, r));
},
/**
* Formats a number as a percentage.
*
* @param {string|number|object|array} watcher
* @param {number} decimals - Number of decimal places (default: 0).
* @param {boolean} isDecimal - Whether input is decimal (0.85) or whole number (85).
* @param {*} defaultValue - Value if original is invalid.
* @returns {object|array}
*/
percentage(t, r = 0, c = !1, n = null) {
return s(t, (e) => {
const a = parseFloat(e);
return isNaN(a) ? n || "" : (c ? a * 100 : a).toFixed(r) + "%";
});
},
/**
* Capitalizes the first letter of each word.
*
* @param {string|object|array} watcher
* @param {boolean} allWords - Capitalize all words (true) or just first word (false).
* @param {*} defaultValue - Value if original is invalid.
* @returns {object|array}
*/
capitalize(t, r = !0, c = null) {
return s(t, (l) => {
if (!l)
return c || "";
const e = String(l);
return r ? e.replace(/\b\w/g, (a) => a.toUpperCase()) : e.charAt(0).toUpperCase() + e.slice(1);
});
},
/**
* Converts a string to uppercase.
*
* @param {string|object|array} watcher
* @param {*} defaultValue - Value if original is invalid.
* @returns {object|array}
*/
uppercase(t, r = null) {
return s(t, (n) => n ? String(n).toUpperCase() : r || "");
},
/**
* Converts a string to lowercase.
*
* @param {string|object|array} watcher
* @param {*} defaultValue - Value if original is invalid.
* @returns {object|array}
*/
lowercase(t, r = null) {
return s(t, (n) => n ? String(n).toLowerCase() : r || "");
},
/**
* Truncates a string to a maximum length with ellipsis.
*
* @param {string|object|array} watcher
* @param {number} maxLength - Maximum length before truncation.
* @param {string} suffix - Suffix to append (default: '...').
* @param {*} defaultValue - Value if original is invalid.
* @returns {object|array}
*/
truncate(t, r = 50, c = "...", n = null) {
return s(t, (e) => {
if (!e)
return n || "";
const a = String(e);
return a.length <= r ? a : a.slice(0, r - c.length) + c;
});
},
/**
* Formats bytes to a human-readable file size.
*
* @param {string|number|object|array} watcher
* @param {number} decimals - Number of decimal places (default: 2).
* @param {*} defaultValue - Value if original is invalid.
* @returns {object|array}
*/
fileSize(t, r = 2, c = null) {
return s(t, (l) => {
const e = parseFloat(l);
if (isNaN(e) || e < 0)
return c || "";
if (e === 0)
return "0 Bytes";
const a = 1024, o = ["Bytes", "KB", "MB", "GB", "TB", "PB"], u = Math.floor(Math.log(e) / Math.log(a));
return parseFloat((e / Math.pow(a, u)).toFixed(r)) + " " + o[u];
});
},
/**
* Handles pluralization of words based on count.
*
* @param {string|number|object|array} watcher
* @param {string} singular - Singular form of the word.
* @param {string} plural - Plural form of the word (optional, adds 's' by default).
* @param {boolean} includeCount - Whether to include the count in output.
* @returns {object|array}
*/
plural(t, r, c = null, n = !0) {
return s(t, (e) => {
const a = parseInt(e, 10);
if (isNaN(a))
return "";
const o = a === 1 ? r : c || r + "s";
return n ? `${a} ${o}` : o;
});
}
};
export {
p as F,
s as c
};