UNPKG

@turnbuckle/aprs-calculator-services

Version:

Anchorpoint Risk Calculator Service.

96 lines (95 loc) 4.16 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.mapDates = exports.keyToDate = exports.dateToKey = exports.isDateInRange = exports.isDateOnOrBefore = exports.isDateBefore = exports.isDateOnOrAfter = exports.isDateAfter = exports.isDateTheSame = exports.monthsBetweenDates = exports.daysDiff = exports.addMonthsToDate = exports.addDaysToDate = exports.getEndOfMonth = exports.getStartOfMonth = exports.getStartOfDay = exports.getDistinctDates = void 0; var date_fns_1 = require("date-fns"); function getDistinctDates(source) { var output = []; for (var i = 0; i < source.length; i++) { var date = (0, date_fns_1.startOfDay)(new Date(source[i].date)).getTime(); if (output.indexOf(date) < 0) { output.push(date); } } return output.sort(function (a, b) { return a - b; }); } exports.getDistinctDates = getDistinctDates; function getStartOfDay(date) { return (0, date_fns_1.startOfDay)(new Date(date)); } exports.getStartOfDay = getStartOfDay; function getStartOfMonth(date) { return (0, date_fns_1.startOfDay)((0, date_fns_1.startOfMonth)(new Date(date))); } exports.getStartOfMonth = getStartOfMonth; function getEndOfMonth(date, monthOffset) { if (monthOffset === void 0) { monthOffset = 0; } return (0, date_fns_1.startOfDay)((0, date_fns_1.endOfMonth)((0, date_fns_1.addMonths)(new Date(date), monthOffset))); } exports.getEndOfMonth = getEndOfMonth; function addDaysToDate(date, days) { return (0, date_fns_1.addDays)(new Date(date), days); } exports.addDaysToDate = addDaysToDate; function addMonthsToDate(date, months) { return (0, date_fns_1.addMonths)(new Date(date), months); } exports.addMonthsToDate = addMonthsToDate; function daysDiff(startDate, endDate) { // differenceInDays takes DST into account // tslint:disable-next-line:no-bitwise return Math.floor((0, date_fns_1.differenceInHours)(new Date(endDate), new Date(startDate)) / 24) | 0; } exports.daysDiff = daysDiff; function monthsBetweenDates(startDate, endDate) { // the number of months between two dates (inclusive) // tslint:disable-next-line:no-bitwise return ((0, date_fns_1.differenceInMonths)(getEndOfMonth(endDate), getStartOfMonth(startDate)) | 0) + 1; } exports.monthsBetweenDates = monthsBetweenDates; function isDateTheSame(date, compareDate) { return (0, date_fns_1.isEqual)(new Date(date), new Date(compareDate)); } exports.isDateTheSame = isDateTheSame; function isDateAfter(date, compareDate) { return (0, date_fns_1.isAfter)(new Date(date), new Date(compareDate)); } exports.isDateAfter = isDateAfter; function isDateOnOrAfter(date, compareDate) { return isDateTheSame(date, compareDate) || isDateAfter(date, compareDate); } exports.isDateOnOrAfter = isDateOnOrAfter; function isDateBefore(date, compareDate) { return (0, date_fns_1.isBefore)(new Date(date), new Date(compareDate)); } exports.isDateBefore = isDateBefore; function isDateOnOrBefore(date, compareDate) { return isDateTheSame(date, compareDate) || isDateBefore(date, compareDate); } exports.isDateOnOrBefore = isDateOnOrBefore; function isDateInRange(date, startDate, endDate) { return isDateOnOrAfter(date, startDate) && isDateOnOrBefore(date, endDate); } exports.isDateInRange = isDateInRange; function dateToKey(date) { return getEndOfMonth(date).getTime().toString(); } exports.dateToKey = dateToKey; function keyToDate(key) { return new Date(+key); } exports.keyToDate = keyToDate; function mapDates(source) { return source.map(function (item) { return (__assign(__assign({}, item), { date: getStartOfDay(item.date) })); }); } exports.mapDates = mapDates;