UNPKG

date-limits

Version:

Check if a date is before a flexible limit.

53 lines (52 loc) 1.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkDateMatches = void 0; /** * Determines if a given date matches the criteria specified in the configuration. * * @param date - The date to check. * @param config - The configuration object defining the criteria. * @returns `true` if the date matches the criteria, otherwise `false`. */ function checkDateMatches(date, config) { if (!_checkValueMatches(date.getUTCFullYear(), config.year)) return false; if (!_checkValueMatches(date.getUTCMonth() + 1, config.month)) return false; if (!_checkValueMatches(date.getUTCDate(), config.day)) return false; const weekday = date.getUTCDay(); if (!_checkValueMatches(weekday, config.weekday)) { if (weekday !== 0) { return false; } if (!_checkValueMatches(7, config.weekday)) return false; } return true; } exports.checkDateMatches = checkDateMatches; /** * Helper function to determine if a single value matches a specific configuration part. * * @param value - The value to check. * @param configPart - The configuration part defining the criteria. * @returns `true` if the value matches the criteria, otherwise `false`. * @private */ function _checkValueMatches(value, configPart) { if (configPart === undefined) return true; if (typeof configPart === 'number') return value === configPart; if (configPart instanceof Set) return configPart.has(value); if (Array.isArray(configPart)) return new Set(configPart).has(value); if ('slope' in configPart) { value -= configPart.offset ?? 0; return value % configPart.slope === 0; } return ((configPart.from === undefined || value >= configPart.from) && (configPart.to === undefined || value <= configPart.to)); }