55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
export function shorten(str) {
|
|
if (str.length < 10) return str;
|
|
return `${str.slice(0, 6)}...${str.slice(str.length - 4)}`;
|
|
}
|
|
|
|
export function capitalize(str) {
|
|
if (str.length === 0) return str;
|
|
if (str.length === 1) return str.toUpperCase();
|
|
return String(str[0]).toUpperCase() + String(str).slice(1);
|
|
}
|
|
|
|
export function shouldTriggerSafetyCheck() {
|
|
const _storage = window.sessionStorage;
|
|
const _safetyCheckKey = "-ghst-safety";
|
|
// check if sessionStorage item exists for SafetyCheck
|
|
if (!_storage.getItem(_safetyCheckKey)) {
|
|
_storage.setItem(_safetyCheckKey, "true");
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
export function formatCurrency(c, precision = 0, currency = "USD") {
|
|
const formatted = new Intl.NumberFormat("en-US", {
|
|
style: currency === "USD" || currency === "" ? "currency" : undefined,
|
|
currency: "USD",
|
|
maximumFractionDigits: precision,
|
|
minimumFractionDigits: precision,
|
|
}).format(c);
|
|
if (currency) return `${formatted} ${currency}`;
|
|
return formatted;
|
|
}
|
|
|
|
export const formatNumber = (number, precision = 0) => {
|
|
return new Intl.NumberFormat("en-US", {
|
|
minimumFractionDigits: precision,
|
|
maximumFractionDigits: precision,
|
|
}).format(number);
|
|
};
|
|
|
|
export const sortBondsByDiscount = (bonds) => {
|
|
return Array.from(bonds).filter((bond) => !bond.isSoldOut).sort((a, b) => (a.discount.gt(b.discount) ? -1 : 1));
|
|
};
|
|
|
|
export const timeConverter = (time, max = 7200, maxText = "long ago") => {
|
|
const seconds = Number(time);
|
|
const mins = Math.floor(seconds / 60);
|
|
const secs = seconds % 60;
|
|
if (mins > max) {
|
|
return maxText;
|
|
} else {
|
|
return `${mins}m ${secs < 10 ? '0' : ''}${secs}s`;
|
|
}
|
|
}
|