@carbon/utilities
Version:
Utilities and helpers to drive consistency across software products using the Carbon Design System
105 lines (104 loc) • 3.64 kB
JavaScript
//#region \0rolldown/runtime.js
var __defProp = Object.defineProperty;
var __exportAll = (all, no_symbols) => {
let target = {};
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
return target;
};
//#endregion
//#region src/dateTimeFormat/relative.ts
var relative_exports = /* @__PURE__ */ __exportAll({ format: () => format$1 });
/**
* Copyright IBM Corp. 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
function format$1(date, options) {
const rtf = new Intl.RelativeTimeFormat(options?.locale, { style: options?.style ?? "long" });
const d = typeof date === "number" ? new Date(date) : date;
const seconds = Math.floor((Date.now() - d.getTime()) / 1e3);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
const weeks = Math.floor(days / 7);
const months = Math.floor(weeks / 4);
const years = Math.floor(days / 365);
if (Math.abs(seconds) < 60) return new Intl.RelativeTimeFormat(options?.locale, {
numeric: "auto",
style: options?.style ?? "long"
}).format(0, "seconds");
if (Math.abs(minutes) < 60) return rtf.format(minutes * -1, "minutes");
if (Math.abs(hours) < 24) return rtf.format(hours * -1, "hours");
if (Math.abs(days) < 7) return rtf.format(days * -1, "days");
if (Math.abs(weeks) < 4) return rtf.format(weeks * -1, "weeks");
if (Math.abs(days) < 365) return rtf.format(months * -1, "months");
return rtf.format(years * -1, "years");
}
//#endregion
//#region src/dateTimeFormat/absolute.ts
var absolute_exports = /* @__PURE__ */ __exportAll({
format: () => format,
formatDate: () => formatDate,
formatRange: () => formatRange,
formatTime: () => formatTime
});
/**
* Copyright IBM Corp. 2024, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
function formatTime(date, options) {
return new Intl.DateTimeFormat(options?.locale, {
timeStyle: options?.style ?? "short",
timeZone: options?.timeZone
}).format(date);
}
function formatDate(date, options) {
return new Intl.DateTimeFormat(options?.locale, {
dateStyle: options?.style ?? "medium",
timeZone: options?.timeZone
}).format(date);
}
function format(date, options) {
const timeStyle = options?.timeStyle ?? (options?.style === "tooltip" ? "long" : options?.style) ?? "short";
const dateStyle = options?.dateStyle ?? (options?.style === "tooltip" ? "full" : options?.style) ?? "medium";
return new Intl.DateTimeFormat(options?.locale, {
timeStyle,
dateStyle,
timeZone: options?.timeZone
}).format(date);
}
function formatRange(startDate, endDate, options) {
const timeStyle = options?.timeStyle === null ? void 0 : options?.timeStyle ?? options?.style ?? "short";
const dateStyle = options?.dateStyle === null ? void 0 : options?.dateStyle ?? options?.style ?? "medium";
return new Intl.DateTimeFormat(options?.locale, {
timeStyle,
dateStyle,
timeZone: options?.timeZone
}).formatRange(startDate, endDate);
}
//#endregion
//#region src/dateTimeFormat/index.ts
/**
* Copyright IBM Corp. 2024
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const dateTimeFormat = {
relative: relative_exports,
absolute: absolute_exports
};
//#endregion
Object.defineProperty(exports, "dateTimeFormat", {
enumerable: true,
get: function() {
return dateTimeFormat;
}
});