import comparePiecesToDate from '../helpers/comparePiecesToDate' ; import mapToPieces from '../helpers/mapToPieces' ; import parsePiecesToDate from '../helpers/parsePiecesToDate' ; /**
 * A `RegExp` that matches common brazilian date and, optionally, time formats.
 *
 * In date and time the seconds are optional too. So the valid formats are:
 * "DD/MM/YYYY", "DD/MM/YYYY HH:mm" and "DD/MM/YYYY HH:mm:ss".
 */ const DATE_TIME_PATTERN = /^\d{2}\/\d{2}\/\d{4}( \d{2}:\d{2}(:\d{2})?)?$/ ; example param value const parseToDate = ( value ) : Date => { if ( ! DATE_TIME_PATTERN . test ( value ) ) throw new Error ( `Value "${ value }" does not match format.` ) ; const pieces = mapToPieces ( value ) ; const instance = parsePiecesToDate ( pieces ) ; if ( ! comparePiecesToDate ( pieces , instance ) ) throw new Error ( `Value "${ value }" is an invalid date.` ) ; return instance ; } ; export default parseToDate ;