@decaf-ts/decorator-validation
Version:
simple decorator based validation engine
88 lines (87 loc) • 3.75 kB
TypeScript
import "reflect-metadata";
/**
* @summary Reverses the process from {@link formatDate}
*
* @param {string} date the date string to be converted back into date
* @param {string} format the date format
* @return {Date} the date from the format or the standard new Date({@prop date}) if the string couldn't be parsed (are you sure the format matches the string?)
*
* @function dateFromFormat
* @memberOf module:decorator-validation
* @category Model
*/
export declare function dateFromFormat(date: string, format: string): Date;
/**
* @description Binds a specific date format to a Date object's toString and toISOString methods using a Proxy
* @summary Wraps a Date object in a Proxy to return a formatted string when toString or toISOString is called.
* This function uses the Proxy API to intercept method calls and return the date formatted according
* to the specified format string, while maintaining all other Date functionality.
* The proxied Date maintains instanceof Date checks and behaves identically to a Date object.
* @param {Date} [date] The Date object to modify
* @param {string} [format] The format string to use for formatting the date
* @return {Date|undefined} A proxied Date object or undefined if no date was provided
* @function bindDateToString
* @memberOf module:decorator-validation
* @category Model
*/
export declare function bindDateToString(date: Date | undefined, format: string): Date | undefined;
/**
* @description Safely checks if a value is a valid Date object
* @summary A utility function that determines if a value is a valid Date object.
* This function is more reliable than using instanceof Date as it also checks
* that the date is not NaN, which can happen with invalid date strings.
* @param {any} date The value to check
* @return {boolean} True if the value is a valid Date object, false otherwise
* @function isValidDate
* @memberOf module:decorator-validation
* @category Validation
*/
export declare function isValidDate(date: any): boolean;
/**
* @summary Util function to pad numbers
* @param {number} num
*
* @return {string}
*
* @function twoDigitPad
* @memberOf module:decorator-validation
* @category Model
*/
export declare function twoDigitPad(num: number): string;
/**
* @summary Date Format Handling
* @description Code from {@link https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date}
*
* <pre>
* Using similar formatting as Moment.js, Class DateTimeFormatter (Java), and Class SimpleDateFormat (Java),
* I implemented a comprehensive solution formatDate(date, patternStr) where the code is easy to read and modify.
* You can display date, time, AM/PM, etc.
*
* Date and Time Patterns
* yy = 2-digit year; yyyy = full year
* M = digit month; MM = 2-digit month; MMM = short month name; MMMM = full month name
* EEEE = full weekday name; EEE = short weekday name
* d = digit day; dd = 2-digit day
* h = hours am/pm; hh = 2-digit hours am/pm; H = hours; HH = 2-digit hours
* m = minutes; mm = 2-digit minutes; aaa = AM/PM
* s = seconds; ss = 2-digit seconds
* S = miliseconds
* </pre>
*
* @param {Date} date
* @param {string} [patternStr] defaults to 'yyyy/MM/dd'
* @return {string} the formatted date
*
* @function formatDate
* @memberOf module:decorator-validation
* @category Model
*/
export declare function formatDate(date: Date, patternStr?: string): string;
/**
* @summary Parses a date from a specified format
* @param {string} format
* @param {string | Date | number} [v]
* @memberOf module:decorator-validation
* @category Model
*/
export declare function parseDate(format: string, v?: string | Date | number): Date | undefined;