@dvcol/common-utils
Version:
Typescript library for common utility functions and constants
118 lines (103 loc) • 4.72 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; } var _class;// lib/common/utils/date.utils.ts
var elapsedTime = (endDate, startDate = /* @__PURE__ */ new Date()) => {
return (endDate.getTime() - startDate.getTime()) / 1e3;
};
var DateUtils = (_class = class {
static __initStatic() {this.clone = (date) => new Date(date)}
static next(days = 1, date, coefficient = 1) {
const _date = date ? this.clone(date) : /* @__PURE__ */ new Date();
_date.setDate(_date.getDate() + days * coefficient);
return _date;
}
static previous(days = 1, date, coefficient = 1) {
return this.next(days, date, -coefficient);
}
static __initStatic2() {this.weeks = {
previous: (weeks = 1, date) => this.previous(weeks, date, 7),
next: (weeks = 1, date) => this.next(weeks, date, 7)
}}
}, _class.__initStatic(), _class.__initStatic2(), _class);
var toDateObject = (record) => {
if (!record) return void 0;
return Object.fromEntries(
Object.entries(record).map(([key, value]) => {
if (typeof value === "string") return [key, new Date(value)];
if (typeof value === "object") return [key, toDateObject(value)];
return [key, value];
})
);
};
var isDate = (value) => value instanceof Date;
var compareDateObject = (a, b) => {
if (!a || !b) throw new Error("Cannot compare undefined objects");
return Object.fromEntries(
Object.keys(a).map((key) => {
const _key = key;
if (a && b) {
const aValue = a[_key];
const bValue = b[_key];
if (isDate(aValue) && isDate(bValue)) return [_key, aValue.getTime() !== bValue.getTime()];
if (!isDate(aValue) && !isDate(bValue)) return [_key, compareDateObject(aValue, bValue)];
}
return [_key, a !== b];
})
);
};
var shortTime = (date, locale, options) => _optionalChain([date, 'optionalAccess', _ => _.toLocaleTimeString, 'call', _2 => _2(locale, { hour: "2-digit", minute: "2-digit", ...options })]);
var DayOfWeek = {
Sunday: "Sunday",
Monday: "Monday",
Tuesday: "Tuesday",
Wednesday: "Wednesday",
Thursday: "Thursday",
Friday: "Friday",
Saturday: "Saturday"
};
var DayOfWeekToNumber = {
[DayOfWeek.Sunday]: 0,
[DayOfWeek.Monday]: 1,
[DayOfWeek.Tuesday]: 2,
[DayOfWeek.Wednesday]: 3,
[DayOfWeek.Thursday]: 4,
[DayOfWeek.Friday]: 5,
[DayOfWeek.Saturday]: 6
};
var NumberToDayOfWeek = {
0: DayOfWeek.Sunday,
1: DayOfWeek.Monday,
2: DayOfWeek.Tuesday,
3: DayOfWeek.Wednesday,
4: DayOfWeek.Thursday,
5: DayOfWeek.Friday,
6: DayOfWeek.Saturday
};
var dayOfTheWeek = (date) => {
const day = new Date(date).getDay();
return NumberToDayOfWeek[day];
};
var getTodayISOLocal = () => new Date(Date.now() - (/* @__PURE__ */ new Date()).getTimezoneOffset() * 60 * 1e3).toISOString();
function timeSince(date) {
const now = /* @__PURE__ */ new Date();
const past = new Date(date);
const diffMs = now.getTime() - past.getTime();
const seconds = Math.floor(diffMs / 1e3);
const minutes = Math.floor(diffMs / (1e3 * 60));
const hours = Math.floor(diffMs / (1e3 * 60 * 60));
const days = Math.floor(diffMs / (1e3 * 60 * 60 * 24));
const months = Math.floor(diffMs / (1e3 * 60 * 60 * 24 * 30));
const years = Math.floor(diffMs / (1e3 * 60 * 60 * 24 * 365));
return { seconds, minutes, hours, days, months, years };
}
function timeAgo(date, {
short = false,
units = ["years", "months", "days", "hours", "minutes", "seconds"]
} = {}) {
const diff = timeSince(date);
const found = units.find((u) => diff[u] > 0);
if (!found) return "now";
const value = diff[found];
if (short) return `${value}${found.slice(0, 1)}`;
const unit = found.slice(0, -1);
return `${value} ${unit}${value > 1 ? "s" : ""} ago`;
}
exports.elapsedTime = elapsedTime; exports.DateUtils = DateUtils; exports.toDateObject = toDateObject; exports.compareDateObject = compareDateObject; exports.shortTime = shortTime; exports.DayOfWeek = DayOfWeek; exports.DayOfWeekToNumber = DayOfWeekToNumber; exports.NumberToDayOfWeek = NumberToDayOfWeek; exports.dayOfTheWeek = dayOfTheWeek; exports.getTodayISOLocal = getTodayISOLocal; exports.timeSince = timeSince; exports.timeAgo = timeAgo;