@qntm-code/utils
Version:
A collection of useful utility functions with associated TypeScript types. All functions have been unit tested.
125 lines (124 loc) • 4.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.compareDates = void 0;
const index_js_1 = require("../convert-units/index.js");
const index_js_2 = require("../getters/index.js");
const date_comparator_enum_js_1 = require("./date-comparator.enum.js");
/**
* Determines if date a is before/before or same/same/after or same/or after to date b. If you want to limit the granularity to a unit other
* than milliseconds, pass it as the second parameter.
*
* When including a second parameter, it will match all units equal or larger. For example, if passing in month will check month and year,
* or if passing in day will check day, month, and year.
*/
function compareDates(a, comparator, b, unit) {
unit = singulariseUnit(unit);
const sameComparator = getSameComparator(comparator);
if (!compare(a.getFullYear(), b.getFullYear(), unit === index_js_1.TimeUnit.Year ? comparator : sameComparator)) {
return false;
}
if (unit === index_js_1.TimeUnit.Year) {
return true;
}
if (!compare(a.getMonth(), b.getMonth(), unit === index_js_1.TimeUnit.Month ? comparator : sameComparator)) {
return false;
}
if (unit === index_js_1.TimeUnit.Month) {
return true;
}
if (!compare((0, index_js_2.getWeekOfYear)(a), (0, index_js_2.getWeekOfYear)(b), unit === index_js_1.TimeUnit.Week ? comparator : sameComparator)) {
return false;
}
if (unit === index_js_1.TimeUnit.Week) {
return true;
}
if (!compare(a.getDate(), b.getDate(), unit === index_js_1.TimeUnit.Day ? comparator : sameComparator)) {
return false;
}
if (unit === index_js_1.TimeUnit.Day) {
return true;
}
if (!compare(a.getHours(), b.getHours(), unit === index_js_1.TimeUnit.Hour ? comparator : sameComparator)) {
return false;
}
if (unit === index_js_1.TimeUnit.Hour) {
return true;
}
if (!compare(a.getMinutes(), b.getMinutes(), unit === index_js_1.TimeUnit.Minute ? comparator : sameComparator)) {
return false;
}
if (unit === index_js_1.TimeUnit.Minute) {
return true;
}
if (!compare(a.getSeconds(), b.getSeconds(), unit === index_js_1.TimeUnit.Second ? comparator : sameComparator)) {
return false;
}
if (unit === index_js_1.TimeUnit.Second) {
return true;
}
return compare(a.getMilliseconds(), b.getMilliseconds(), comparator);
}
exports.compareDates = compareDates;
function compare(a, b, comparator) {
switch (comparator) {
case date_comparator_enum_js_1.DateComparator.Before: {
return a < b;
}
case date_comparator_enum_js_1.DateComparator.BeforeOrSame: {
return a <= b;
}
case date_comparator_enum_js_1.DateComparator.After: {
return a > b;
}
case date_comparator_enum_js_1.DateComparator.AfterOrSame: {
return a >= b;
}
default: {
return a === b;
}
}
}
function singulariseUnit(unit) {
switch (unit) {
case index_js_1.TimeUnit.Years: {
return index_js_1.TimeUnit.Year;
}
case index_js_1.TimeUnit.Months: {
return index_js_1.TimeUnit.Month;
}
case index_js_1.TimeUnit.Weeks: {
return index_js_1.TimeUnit.Week;
}
case index_js_1.TimeUnit.Days: {
return index_js_1.TimeUnit.Day;
}
case index_js_1.TimeUnit.Hours: {
return index_js_1.TimeUnit.Hour;
}
case index_js_1.TimeUnit.Minutes: {
return index_js_1.TimeUnit.Minute;
}
case index_js_1.TimeUnit.Seconds: {
return index_js_1.TimeUnit.Second;
}
case index_js_1.TimeUnit.Milliseconds: {
return index_js_1.TimeUnit.Millisecond;
}
default: {
return unit;
}
}
}
function getSameComparator(comparator) {
switch (comparator) {
case date_comparator_enum_js_1.DateComparator.Before: {
return date_comparator_enum_js_1.DateComparator.BeforeOrSame;
}
case date_comparator_enum_js_1.DateComparator.After: {
return date_comparator_enum_js_1.DateComparator.AfterOrSame;
}
default: {
return comparator;
}
}
}