UNPKG

temporal-fns

Version:

A utility library for working with JavaScript Temporal objects, providing helper functions for date and time manipulations.

188 lines (173 loc) 5.2 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { endOfDay: () => endOfDay, endOfMonth: () => endOfMonth, endOfWeek: () => endOfWeek, firstDayOfWeekOfMonth: () => firstDayOfWeekOfMonth, fromDate: () => fromDate, lastDayOfWeekOfMonth: () => lastDayOfWeekOfMonth, nextDayOfWeek: () => nextDayOfWeek, previousDayOfWeek: () => previousDayOfWeek, startOfDay: () => startOfDay, startOfMonth: () => startOfMonth, startOfWeek: () => startOfWeek, toDate: () => toDate }); module.exports = __toCommonJS(src_exports); // src/end-of-day.ts function endOfDay(temporal) { return temporal.with({ hour: 23, minute: 59, second: 59, millisecond: 999, microsecond: 999, nanosecond: 999 }); } // src/end-of-month.ts var import_temporal_polyfill = require("temporal-polyfill"); function endOfMonth(temporal, options) { const { preserveTime = false } = options ?? {}; const lastDay = temporal.with({ day: temporal.daysInMonth }); if (lastDay instanceof import_temporal_polyfill.Temporal.PlainDate) { return lastDay; } if (preserveTime) { return lastDay; } return endOfDay(lastDay); } // src/end-of-week.ts function endOfWeek(temporal) { return temporal.add({ days: 7 - temporal.dayOfWeek }); } // src/start-of-month.ts var import_temporal_polyfill2 = require("temporal-polyfill"); // src/start-of-day.ts function startOfDay(temporal) { return temporal.with({ hour: 0, minute: 0, second: 0, millisecond: 0, microsecond: 0, nanosecond: 0 }); } // src/start-of-month.ts function startOfMonth(temporal, options) { const { preserveTime = false } = options ?? {}; const firstDay = temporal.with({ day: 1 }); if (firstDay instanceof import_temporal_polyfill2.Temporal.PlainDate) { return firstDay; } if (preserveTime) { return firstDay; } return startOfDay(firstDay); } // src/first-day-of-week-of-month.ts function firstDayOfWeekOfMonth(temporal, dayOfWeek) { const firstDay = startOfMonth(temporal, { preserveTime: true }); const delta = (dayOfWeek - firstDay.dayOfWeek + 7) % 7; return firstDay.add({ days: delta }); } // src/from-date.ts var import_temporal_polyfill3 = require("temporal-polyfill"); function fromDate(date) { return import_temporal_polyfill3.Temporal.Instant.fromEpochMilliseconds(date.getTime()); } // src/last-day-of-week-of-month.ts function lastDayOfWeekOfMonth(temporal, dayOfWeek) { const lastDay = endOfMonth(temporal, { preserveTime: true }); const delta = (lastDay.dayOfWeek - dayOfWeek + 7) % 7; return lastDay.subtract({ days: delta }); } // src/next-day-of-week.ts function nextDayOfWeek(temporal, dayOfWeek) { let delta = dayOfWeek - temporal.dayOfWeek; if (delta <= 0) { delta += 7; } return temporal.add({ days: delta }); } // src/previous-day-of-week.ts function previousDayOfWeek(temporal, dayOfWeek) { let delta = temporal.dayOfWeek - dayOfWeek; if (delta <= 0) { delta += 7; } return temporal.subtract({ days: delta }); } // src/start-of-week.ts function startOfWeek(temporal) { const delta = temporal.dayOfWeek === 1 ? 0 : -(temporal.dayOfWeek - 1); return temporal.add({ days: delta }); } // src/to-date.ts var import_temporal_polyfill4 = require("temporal-polyfill"); function toDate(temporal) { if (temporal instanceof import_temporal_polyfill4.Temporal.Instant) { return new Date(temporal.epochMilliseconds); } if (temporal instanceof import_temporal_polyfill4.Temporal.PlainDate) { return new Date(temporal.year, temporal.month - 1, temporal.day); } if (temporal instanceof import_temporal_polyfill4.Temporal.PlainDateTime) { return new Date( temporal.year, temporal.month - 1, temporal.day, temporal.hour, temporal.minute, temporal.second, temporal.millisecond ); } if (temporal instanceof import_temporal_polyfill4.Temporal.ZonedDateTime) { const utc = temporal.withTimeZone("UTC"); return new Date( utc.year, utc.month - 1, utc.day, utc.hour, utc.minute, utc.second, utc.millisecond ); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { endOfDay, endOfMonth, endOfWeek, firstDayOfWeekOfMonth, fromDate, lastDayOfWeekOfMonth, nextDayOfWeek, previousDayOfWeek, startOfDay, startOfMonth, startOfWeek, toDate });