@gatekeeper_technology/report-utils
Version:
Gatekeeper's pdf/email Utils - shared in NPM
188 lines (187 loc) • 8.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWeekNumber = exports.getEndOfWeek = exports.getStartOfWeek = exports.compareDates = exports.isSameDay = exports.displayDateTime = exports.displayTimeWithSeconds = exports.displayTime = exports.displayDate = exports.getOffsetDate = void 0;
const db_1 = require("@journeyapps/db");
/**
* Returns the date of the start of the week (Monday if iso = true, Sunday if iso = false).
* @param {Date} date The input date to calculate the start of the week from (default: current date).
* @param {boolean} iso Whether to use ISO 8601 standard (default: true).
* @returns {Date | null} The start of the week as a Date object, or null if the input date is invalid.
*/
function getStartOfWeek(date = new Date(), iso = true) {
if (!(date instanceof Date) && !(db_1.Day.isDay(date)))
return null;
// Calculate start of the week.
const day_of_week = new Date(+date).getDay();
const days_to_subtract = iso ? (!day_of_week ? 6 : day_of_week - 1) : day_of_week;
const start_of_week = new Date(+date);
start_of_week.setDate(start_of_week.getDate() - days_to_subtract);
start_of_week.setHours(2, 0, 0, 0);
return start_of_week;
}
exports.getStartOfWeek = getStartOfWeek;
/**
* Returns the date of the end of the week (Sunday if iso = true, Saturday if iso = false).
* @param {Date} date The input date to calculate the end of the week from (default: current date).
* @param {boolean} iso Whether to use ISO 8601 standard (default: true).
* @returns {Date | null} The end of the week as a Date object, or null if the input date is invalid.
*/
function getEndOfWeek(date = new Date(), iso = true) {
if (!(date instanceof Date) && !(db_1.Day.isDay(date)))
return null;
// Calculate end of the week.
const start_of_week = getStartOfWeek(date, iso);
if (!start_of_week)
return null;
const end_of_week = new Date(start_of_week);
end_of_week.setDate(start_of_week.getDate() + 6);
end_of_week.setUTCHours(23, 59, 59, 999);
return new Date(end_of_week);
}
exports.getEndOfWeek = getEndOfWeek;
/**
* This function returns the calendar week number of the given date.
* @param {Date} date The input date to calculate the week number for (default: current date).
* @param {boolean} iso Whether to use ISO 8601 standard (default: true).
* @returns {number | null} The week number as a number, or null if the input date is invalid.
*/
function getWeekNumber(date = new Date(), iso = true) {
var _a;
if (!(date instanceof Date) && !(db_1.Day.isDay(date)))
return null;
date = new Date(+date);
// Calculate start of the year and start of the week
//@ts-ignore — To cater for Day objects as well.
const year = (_a = date.getFullYear()) !== null && _a !== void 0 ? _a : date.getYear();
const start_of_year = new Date(year, 0, 1);
const start_of_week = getStartOfWeek(start_of_year, iso);
if (!start_of_week)
return null;
// Calculate number of days between start of the year and input date
const days = Math.floor((+date - +start_of_week) / (24 * 60 * 60 * 1000));
// Calculate week number
const week_number = Math.ceil(days / 7);
return week_number;
}
exports.getWeekNumber = getWeekNumber;
/**
* This function compares two dates and returns -1 if date_1 is before date_2,
* 0 if they are equal to the millisecond, and 1 if date_1 is after date_2.
* @param {Date} date_1
* @param {Date} [date_2 = new Date()] Defaults to the current date and time.
* @returns {-1 | 0 | 1 | null}
*/
function compareDates(date_1, date_2 = new Date()) {
if (!(date_1 instanceof Date || db_1.Day.isDay(date_1)))
return null;
if (!(date_2 instanceof Date || db_1.Day.isDay(date_2)))
return null;
// Compare the two dates
if (date_1 < date_2) {
return -1;
}
else if (date_1 > date_2) {
return 1;
}
else {
return 0;
}
}
exports.compareDates = compareDates;
/**
* This function checks if two given dates fall on the same calendar day.
* @param {Date} date_1 The first date to compare.
* @param {Date} [date_2 = new Date()] The second date to compare. Defaults to the current date and time.
* @returns {boolean} True if the two dates fall on the same day; otherwise, false.
*/
function isSameDay(date_1, date_2 = new Date()) {
if (!(date_1 instanceof Date || db_1.Day.isDay(date_1)))
return null;
if (!(date_2 instanceof Date || db_1.Day.isDay(date_2)))
return null;
// Compare the year, month, and day of the two dates
return new Date(+date_1).toDateString() === new Date(+date_2).toDateString();
}
exports.isSameDay = isSameDay;
/**
* Returns a new date object that is offset by the specified number of days.
* @param {Date} original_date The original date.
* @param {number} offset_days The number of days to offset the original date. Positive values add days, negative values subtract days.
* @returns {Date} A new date object that represents the original date plus the offset.
*/
function getOffsetDate(original_date, offset_days) {
if (!(original_date instanceof Date || db_1.Day.isDay(original_date)))
return null;
if (typeof offset_days !== 'number' && offset_days !== null)
return null;
// Calculate the new date by adding the offset to the original date
const new_date = new Date(+original_date + offset_days * 24 * 60 * 60 * 1000);
// Return the new date object
return new_date;
}
exports.getOffsetDate = getOffsetDate;
function displayDate(date = new Date(), month_length = 'short') {
if (!(date instanceof Date) && !(db_1.Day.isDay(date)))
return "-";
// Validate the month_length parameter
const valid_month_lengths = ['short', 'long', '2-digit', 'narrow', 'numeric'];
if (!valid_month_lengths.includes(month_length))
return '-';
return new Date(+date).toLocaleDateString('en-ZA', {
year: 'numeric',
month: month_length,
day: 'numeric'
});
}
exports.displayDate = displayDate;
/**
* This function takes a Date object, a timestamp, or a string representing a date and returns a string in the format HH:mm.
* Returns '-' if the input date is invalid.
* @param {Date} date - The date to display.
* @returns {string} The formatted time string or '-' if the input date is invalid.
*/
function displayTime(date = new Date()) {
if (!(date instanceof Date || db_1.Day.isDay(date)))
return "-";
return new Date(+date).toLocaleTimeString('en-ZA', {
hour: '2-digit',
minute: '2-digit'
});
}
exports.displayTime = displayTime;
/**
* This function takes a Date object, a timestamp, or a string representing a date and returns a string in the format HH:mm:ss.
* Returns '-' if the input date is invalid.
* @param {Date|number|string} date - The date to display.
* @returns {string} The formatted time string or '-' if the input date is invalid.
*/
function displayTimeWithSeconds(date = new Date()) {
if (!(date instanceof Date || db_1.Day.isDay(date)))
return "-";
return new Date(+date).toLocaleTimeString('en-ZA', {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
});
}
exports.displayTimeWithSeconds = displayTimeWithSeconds;
/**
* This function takes a Date object, a timestamp, or a string representing a date
* and returns a string in the format DD <Month name> YYYY, HH:mm.
* Returns '-' if the input date is invalid.
* @param {Date|number|string} date - The date to display.
* @param {'short' | 'long' | '2-digit' | 'narrow' | 'numeric'} monthFormat - The format to use for the month name.
* @returns {string} The formatted date and time string or '-' if the input date is invalid.
*/
function displayDateTime(date = new Date(), month_length = 'short') {
if (!(date instanceof Date || db_1.Day.isDay(date)))
return "-";
return new Date(+date).toLocaleDateString('en-ZA', {
year: 'numeric',
month: month_length,
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
});
}
exports.displayDateTime = displayDateTime;