Spaces:
Sleeping
Sleeping
File size: 7,435 Bytes
b64de39 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | /**
* General-purpose utility/helper functions.
*
* Pure, side-effect-free functions for common formatting,
* date operations, and data transformations used across the UI.
*/
// ---------------------------------------------------------------------------
// Date Formatting
// ---------------------------------------------------------------------------
/**
* Format an ISO date string to a human-readable short date.
* Example: "2026-02-07T10:30:00Z" -> "07 Feb 2026"
*
* @param {string|Date} dateInput - ISO string or Date object.
* @param {string} locale - BCP 47 locale tag.
* @returns {string} Formatted date string.
*/
export function formatDate(dateInput, locale = "en-IN") {
if (!dateInput) {
return "N/A";
}
const date = dateInput instanceof Date ? dateInput : new Date(dateInput);
if (Number.isNaN(date.getTime())) {
return "Invalid date";
}
return date.toLocaleDateString(locale, {
day: "2-digit",
month: "short",
year: "numeric",
});
}
/**
* Format an ISO date string to a short date + time.
* Example: "2026-02-07T10:30:00Z" -> "07 Feb 2026, 04:00 PM"
*
* @param {string|Date} dateInput - ISO string or Date object.
* @param {string} locale - BCP 47 locale tag.
* @returns {string} Formatted date-time string.
*/
export function formatDateTime(dateInput, locale = "en-IN") {
if (!dateInput) {
return "N/A";
}
const date = dateInput instanceof Date ? dateInput : new Date(dateInput);
if (Number.isNaN(date.getTime())) {
return "Invalid date";
}
return date.toLocaleString(locale, {
day: "2-digit",
month: "short",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
});
}
/**
* Return a relative time description (e.g. "2 hours ago").
*
* @param {string|Date} dateInput - ISO string or Date object.
* @returns {string} Relative time string.
*/
export function timeAgo(dateInput) {
if (!dateInput) {
return "N/A";
}
const date = dateInput instanceof Date ? dateInput : new Date(dateInput);
if (Number.isNaN(date.getTime())) {
return "Invalid date";
}
const seconds = Math.floor((Date.now() - date.getTime()) / 1000);
const intervals = [
{ label: "year", seconds: 31536000 },
{ label: "month", seconds: 2592000 },
{ label: "week", seconds: 604800 },
{ label: "day", seconds: 86400 },
{ label: "hour", seconds: 3600 },
{ label: "minute", seconds: 60 },
];
for (const interval of intervals) {
const count = Math.floor(seconds / interval.seconds);
if (count >= 1) {
return `${count} ${interval.label}${count > 1 ? "s" : ""} ago`;
}
}
return "Just now";
}
// ---------------------------------------------------------------------------
// Price / Currency Formatting
// ---------------------------------------------------------------------------
/**
* Format a number as Indian Rupee currency.
* Example: 2250.5 -> "Rs 2,250.50"
*
* @param {number} amount - Numeric value.
* @param {number} fractionDigits - Decimal places (default 2).
* @returns {string} Formatted currency string.
*/
export function formatPrice(amount, fractionDigits = 2) {
if (amount === null || amount === undefined || Number.isNaN(Number(amount))) {
return "N/A";
}
return `Rs ${Number(amount).toLocaleString("en-IN", {
minimumFractionDigits: fractionDigits,
maximumFractionDigits: fractionDigits,
})}`;
}
/**
* Format a price per quintal with unit label.
*
* @param {number} price - Price per quintal.
* @returns {string} Formatted string e.g. "Rs 2,250/qtl"
*/
export function formatPricePerQuintal(price) {
if (price === null || price === undefined) {
return "N/A";
}
return `${formatPrice(price, 0)}/qtl`;
}
// ---------------------------------------------------------------------------
// Temperature Formatting
// ---------------------------------------------------------------------------
/**
* Format a temperature value with the degree symbol.
*
* @param {number} temp - Temperature in Celsius.
* @param {number} decimals - Decimal places.
* @returns {string} e.g. "28.5 C"
*/
export function formatTemperature(temp, decimals = 1) {
if (temp === null || temp === undefined) {
return "N/A";
}
return `${Number(temp).toFixed(decimals)} C`;
}
// ---------------------------------------------------------------------------
// Number Formatting
// ---------------------------------------------------------------------------
/**
* Format a percentage value.
*
* @param {number} value - Decimal (0-1) or percentage (0-100).
* @param {boolean} isDecimal - If true, multiply by 100 first.
* @returns {string} e.g. "85.2%"
*/
export function formatPercentage(value, isDecimal = false) {
if (value === null || value === undefined) {
return "N/A";
}
const pct = isDecimal ? value * 100 : value;
return `${pct.toFixed(1)}%`;
}
// ---------------------------------------------------------------------------
// String Utilities
// ---------------------------------------------------------------------------
/**
* Capitalize the first letter of a string.
*
* @param {string} str - Input string.
* @returns {string} Capitalized string.
*/
export function capitalize(str) {
if (!str) {
return "";
}
return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
}
/**
* Convert a string to title case.
*
* @param {string} str - Input string.
* @returns {string} Title-cased string.
*/
export function titleCase(str) {
if (!str) {
return "";
}
return str
.split(" ")
.map((word) => capitalize(word))
.join(" ");
}
/**
* Truncate a string to a maximum length with an ellipsis.
*
* @param {string} str - Input string.
* @param {number} maxLength - Maximum character count.
* @returns {string} Truncated string.
*/
export function truncate(str, maxLength = 100) {
if (!str) {
return "";
}
if (str.length <= maxLength) {
return str;
}
return `${str.slice(0, maxLength).trimEnd()}...`;
}
// ---------------------------------------------------------------------------
// Miscellaneous
// ---------------------------------------------------------------------------
/**
* Generate a simple unique ID (for UI keys, not cryptographic).
*
* @param {string} prefix - Optional prefix.
* @returns {string} Unique identifier.
*/
export function generateId(prefix = "id") {
return `${prefix}_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`;
}
/**
* Debounce a function call.
*
* @param {Function} fn - Function to debounce.
* @param {number} delay - Delay in milliseconds.
* @returns {Function} Debounced function.
*/
export function debounce(fn, delay = 300) {
let timerId;
return function debounced(...args) {
clearTimeout(timerId);
timerId = setTimeout(() => fn.apply(this, args), delay);
};
}
/**
* Clamp a number within min and max bounds.
*
* @param {number} value - Input value.
* @param {number} min - Lower bound.
* @param {number} max - Upper bound.
* @returns {number} Clamped value.
*/
export function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
/**
* Deep-clone a plain object or array via structured cloning.
*
* @param {*} obj - Object to clone.
* @returns {*} Deep copy.
*/
export function deepClone(obj) {
if (obj === null || typeof obj !== "object") {
return obj;
}
return structuredClone(obj);
}
|