@blue-impact-engine/blue-impact-engine-client
Version:
Blue Impact Engine API Client
124 lines • 3.54 kB
JavaScript
/**
* Date and time utilities
*/
export const dateTime = {
/**
* Get current timestamp
* @returns number - Current timestamp in milliseconds
*/
now() {
return Date.now();
},
/**
* Get current ISO string
* @returns string - Current date in ISO format
*/
nowISO() {
return new Date().toISOString();
},
/**
* Add days to date
* @param date - Base date
* @param days - Number of days to add
* @returns Date - New date
*/
addDays(date, days) {
const result = new Date(date);
result.setDate(result.getDate() + days);
return result;
},
/**
* Subtract days from date
* @param date - Base date
* @param days - Number of days to subtract
* @returns Date - New date
*/
subtractDays(date, days) {
return this.addDays(date, -days);
},
/**
* Check if date is in the past
* @param date - Date to check
* @returns boolean - True if in past
*/
isPast(date) {
const checkDate = typeof date === "string" ? new Date(date) : date;
return checkDate < new Date();
},
/**
* Check if date is in the future
* @param date - Date to check
* @returns boolean - True if in future
*/
isFuture(date) {
const checkDate = typeof date === "string" ? new Date(date) : date;
return checkDate > new Date();
},
/**
* Get days between two dates
* @param start - Start date
* @param end - End date
* @returns number - Number of days
*/
daysBetween(start, end) {
const startDate = typeof start === "string" ? new Date(start) : start;
const endDate = typeof end === "string" ? new Date(end) : end;
const diffTime = Math.abs(endDate.getTime() - startDate.getTime());
return Math.ceil(diffTime / (1000 * 60 * 60 * 24));
},
};
/**
* String utilities
*/
export const strings = {
/**
* Capitalize first letter of string
* @param str - String to capitalize
* @returns string - Capitalized string
*/
capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
},
/**
* Convert string to title case
* @param str - String to convert
* @returns string - Title case string
*/
toTitleCase(str) {
return str.replace(/\w\S*/g, (txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase());
},
/**
* Generate random string
* @param length - Length of string
* @returns string - Random string
*/
random(length = 8) {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
},
/**
* Truncate string to specified length
* @param str - String to truncate
* @param length - Maximum length
* @param suffix - Suffix to add (default: '...')
* @returns string - Truncated string
*/
truncate(str, length, suffix = "...") {
if (str.length <= length)
return str;
return str.substring(0, length - suffix.length) + suffix;
},
/**
* Remove HTML tags from string
* @param html - HTML string
* @returns string - Clean text
*/
stripHtml(html) {
return html.replace(/<[^>]*>/g, "");
},
};
//# sourceMappingURL=typeUtils.js.map