date-vir
Version:
Easy and explicit dates and times.
47 lines (46 loc) • 1.13 kB
JavaScript
/**
* Checks that the input has all the requested {@link FullDate} keys.
*
* @category Util
* @example
*
* ```ts
* import {hasFullDateKeys} from 'date-vir';
*
* hasFullDateKeys({year: 2024}, ['year']); // `true`
* hasFullDateKeys({year: 2024}, ['day']); // `false`
* ```
*/
export function hasFullDateKeys(partialFullDate, keys) {
try {
assertHasFullDateKeys(partialFullDate, keys);
return true;
}
catch {
return false;
}
}
/**
* Asserts that the input has all the requested {@link FullDate} keys.
*
* @category Util
* @example
*
* ```ts
* import {assertHasFullDateKeys} from 'date-vir';
*
* assertHasFullDateKeys({year: 2024}, ['year']); // passes
* assertHasFullDateKeys({year: 2024}, ['day']); // fails
* ```
*/
export function assertHasFullDateKeys(partialFullDate, keys) {
const missingKeys = [];
keys.forEach((key) => {
if (partialFullDate[key] == undefined) {
missingKeys.push(key);
}
});
if (missingKeys.length) {
throw new Error(`Missing required FullDate key(s): ${missingKeys.join(', ')}`);
}
}