date-vir
Version:
Easy and explicit dates and times.
33 lines (32 loc) • 801 B
JavaScript
import { DateTime } from 'luxon';
import { createCustomShape } from 'object-shape-tester';
/**
* A shape definition for {@link UtcIsoString}.
*
* @category ISO
*/
export const utcIsoStringShape = createCustomShape({
default: new Date().toISOString(),
name: 'UtcIsoString',
checkValue(value) {
return isValidIsoString(value);
},
});
/**
* Checks if the input is a valid UTC ISO string and type guards the input.
*
* @category ISO
* @category Assertion
* @example
*
* ```ts
* import {isValidIsoString} from 'date-vir';
*
* isValidIsoString('no'); // `false`
* isValidIsoString('2024-05-01T20:18:17.123Z'); // `true`
* ```
*/
export function isValidIsoString(input) {
const datetime = DateTime.fromISO(input);
return datetime.toUTC().toISO() === input;
}