import { padNumber, toInteger, isNumber } from '../util/util'; import { Ng2vDateStruct } from './ng2v-date-struct'; /** * Abstract type serving as a DI token for the service parsing and formatting dates for the Ng2vInputDatepicker * directive. A default implementation using the ISO 8601 format is provided, but you can provide another implementation * to use an alternative format. */ export abstract class Ng2vDateParserFormatter { /** * Parses the given value to an Ng2vDateStruct. Implementations should try their best to provide a result, even * partial. They must return null if the value can't be parsed. * @param value the value to parse */ abstract parse(value: string): Ng2vDateStruct; /** * Formats the given date to a string. Implementations should return an empty string if the given date is null, * and try their best to provide a partial result if the given date is incomplete or invalid. * @param date the date to format as a string */ abstract format(date: Ng2vDateStruct, formatType: string): string; } export class Ng2vDateISOParserFormatter extends Ng2vDateParserFormatter { parse(value: string): Ng2vDateStruct { if (value) { const dateParts = value.trim().split('-'); if (dateParts.length === 1 && isNumber(dateParts[0])) { return { year: toInteger(dateParts[0]), month: null, day: null }; } else if (dateParts.length === 2 && isNumber(dateParts[0]) && isNumber(dateParts[1])) { return { year: toInteger(dateParts[0]), month: toInteger(dateParts[1]), day: null }; } else if (dateParts.length === 3 && isNumber(dateParts[0]) && isNumber(dateParts[1]) && isNumber(dateParts[2])) { return { year: toInteger(dateParts[0]), month: toInteger(dateParts[1]), day: toInteger(dateParts[2]) }; } } return null; } format(date: Ng2vDateStruct, formatType: string): string { var month_names_short = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; if (formatType.toLocaleLowerCase() === "yyyy-mm-dd") return date ? `${date.year}-${isNumber(date.month) ? month_names_short[(date.month - 1)] : ''}-${isNumber(date.day) ? padNumber(date.day) : ''}` : ''; else return date ? `${isNumber(date.day) ? padNumber(date.day) : ''} - ${isNumber(date.month) ? month_names_short[(date.month - 1)] : ''} - ${date.year}` : ''; } }