|
| 1 | +/** Adapts type `D` to be usable as a date by cdk-based components that work with dates. */ |
| 2 | +export abstract class DateAdapter<D> { |
| 3 | + /** The locale to use for all dates. */ |
| 4 | + protected locale: any; |
| 5 | + |
| 6 | + /** |
| 7 | + * Gets the year component of the given date. |
| 8 | + * @param date The date to extract the year from. |
| 9 | + * @returns The year component. |
| 10 | + */ |
| 11 | + abstract getYear(date: D): number; |
| 12 | + |
| 13 | + /** |
| 14 | + * Gets the month component of the given date. |
| 15 | + * @param date The date to extract the month from. |
| 16 | + * @returns The month component (0-indexed, 0 = January). |
| 17 | + */ |
| 18 | + abstract getMonth(date: D): number; |
| 19 | + |
| 20 | + /** |
| 21 | + * Gets the date of the month component of the given date. |
| 22 | + * @param date The date to extract the date of the month from. |
| 23 | + * @returns The month component (1-indexed, 1 = first of month). |
| 24 | + */ |
| 25 | + abstract getDate(date: D): number; |
| 26 | + |
| 27 | + /** |
| 28 | + * Gets the day of the week component of the given date. |
| 29 | + * @param date The date to extract the day of the week from. |
| 30 | + * @returns The month component (0-indexed, 0 = Sunday). |
| 31 | + */ |
| 32 | + abstract getDayOfWeek(date: D): number; |
| 33 | + |
| 34 | + /** |
| 35 | + * Gets a list of names for the months. |
| 36 | + * @param style The naming style (e.g. long = 'January', short = 'Jan', narrow = 'J'). |
| 37 | + * @returns An ordered list of all month names, starting with January. |
| 38 | + */ |
| 39 | + abstract getMonthNames(style: 'long' | 'short' | 'narrow'): string[]; |
| 40 | + |
| 41 | + /** |
| 42 | + * Gets a list of names for the dates of the month. |
| 43 | + * @returns An ordered list of all date of the month names, starting with '1'. |
| 44 | + */ |
| 45 | + abstract getDateNames(): string[]; |
| 46 | + |
| 47 | + /** |
| 48 | + * Gets a list of names for the days of the week. |
| 49 | + * @param style The naming style (e.g. long = 'Sunday', short = 'Sun', narrow = 'S'). |
| 50 | + * @returns An ordered list of all weekday names, starting with Sunday. |
| 51 | + */ |
| 52 | + abstract getDayOfWeekNames(style: 'long' | 'short' | 'narrow'): string[]; |
| 53 | + |
| 54 | + /** |
| 55 | + * Gets the name for the year of the given date. |
| 56 | + * @param date The date to get the year name for. |
| 57 | + * @returns The name of the given year (e.g. '2017'). |
| 58 | + */ |
| 59 | + abstract getYearName(date: D): string; |
| 60 | + |
| 61 | + /** |
| 62 | + * Gets the first day of the week. |
| 63 | + * @returns The first day of the week (0-indexed, 0 = Sunday). |
| 64 | + */ |
| 65 | + abstract getFirstDayOfWeek(): number; |
| 66 | + |
| 67 | + /** |
| 68 | + * Gets the number of days in the month of the given date. |
| 69 | + * @param date The date whose month should be checked. |
| 70 | + * @returns The number of days in the month of the given date. |
| 71 | + */ |
| 72 | + abstract getNumDaysInMonth(date: D): number; |
| 73 | + |
| 74 | + /** |
| 75 | + * Clones the given date. |
| 76 | + * @param date The date to clone |
| 77 | + * @returns A new date equal to the given date. |
| 78 | + */ |
| 79 | + abstract clone(date: D): D; |
| 80 | + |
| 81 | + /** |
| 82 | + * Creates a date with the given year, month, and date. Does not allow over/under-flow of the |
| 83 | + * month and date. |
| 84 | + * @param year The full year of the date. (e.g. 89 means the year 89, not the year 1989). |
| 85 | + * @param month The month of the date (0-indexed, 0 = January). Must be an integer 0 - 11. |
| 86 | + * @param date The date of month of the date. Must be an integer 1 - length of the given month. |
| 87 | + * @returns The new date, or null if invalid. |
| 88 | + */ |
| 89 | + abstract createDate(year: number, month: number, date: number): D; |
| 90 | + |
| 91 | + /** |
| 92 | + * Gets today's date. |
| 93 | + * @returns Today's date. |
| 94 | + */ |
| 95 | + abstract today(): D; |
| 96 | + |
| 97 | + /** |
| 98 | + * Parses a date from a value. |
| 99 | + * @param value The value to parse. |
| 100 | + * @param parseFormat The expected format of the value being parsed |
| 101 | + * (type is implementation-dependent). |
| 102 | + * @returns The parsed date, or null if date could not be parsed. |
| 103 | + */ |
| 104 | + abstract parse(value: any, parseFormat: any): D | null; |
| 105 | + |
| 106 | + /** |
| 107 | + * Formats a date as a string. |
| 108 | + * @param date The value to parse. |
| 109 | + * @param displayFormat The format to use to display the date as a string. |
| 110 | + * @returns The parsed date, or null if date could not be parsed. |
| 111 | + */ |
| 112 | + abstract format(date: D, displayFormat: any): string; |
| 113 | + |
| 114 | + /** |
| 115 | + * Adds the given number of years to the date. Years are counted as if flipping 12 pages on the |
| 116 | + * calendar for each year and then finding the closest date in the new month. For example when |
| 117 | + * adding 1 year to Feb 29, 2016, the resulting date will be Feb 28, 2017. |
| 118 | + * @param date The date to add years to. |
| 119 | + * @param years The number of years to add (may be negative). |
| 120 | + * @returns A new date equal to the given one with the specified number of years added. |
| 121 | + */ |
| 122 | + abstract addCalendarYears(date: D, years: number): D; |
| 123 | + |
| 124 | + /** |
| 125 | + * Adds the given number of months to the date. Months are counted as if flipping a page on the |
| 126 | + * calendar for each month and then finding the closest date in the new month. For example when |
| 127 | + * adding 1 month to Jan 31, 2017, the resulting date will be Feb 28, 2017. |
| 128 | + * @param date The date to add months to. |
| 129 | + * @param months The number of months to add (may be negative). |
| 130 | + * @returns A new date equal to the given one with the specified number of months added. |
| 131 | + */ |
| 132 | + abstract addCalendarMonths(date: D, months: number): D; |
| 133 | + |
| 134 | + /** |
| 135 | + * Adds the given number of days to the date. Days are counted as if moving one cell on the |
| 136 | + * calendar for each day. |
| 137 | + * @param date The date to add days to. |
| 138 | + * @param days The number of days to add (may be negative). |
| 139 | + * @returns A new date equal to the given one with the specified number of days added. |
| 140 | + */ |
| 141 | + abstract addCalendarDays(date: D, days: number): D; |
| 142 | + |
| 143 | + /** |
| 144 | + * Gets the RFC 3339 compatible date string (https://tools.ietf.org/html/rfc3339) for the given |
| 145 | + * date. |
| 146 | + * @param date The date to get the ISO date string for. |
| 147 | + * @returns The ISO date string date string. |
| 148 | + */ |
| 149 | + abstract getISODateString(date: D): string; |
| 150 | + |
| 151 | + /** |
| 152 | + * Sets the locale used for all dates. |
| 153 | + * @param locale The new locale. |
| 154 | + */ |
| 155 | + setLocale(locale: any) { |
| 156 | + this.locale = locale; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Compares two dates. |
| 161 | + * @param first The first date to compare. |
| 162 | + * @param second The second date to compare. |
| 163 | + * @returns 0 if the dates are equal, a number less than 0 if the first date is earlier, |
| 164 | + * a number greater than 0 if the first date is later. |
| 165 | + */ |
| 166 | + compareDate(first: D, second: D): number { |
| 167 | + return this.getYear(first) - this.getYear(second) || |
| 168 | + this.getMonth(first) - this.getMonth(second) || |
| 169 | + this.getDate(first) - this.getDate(second); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Checks if two dates are equal. |
| 174 | + * @param first The first date to check. |
| 175 | + * @param second The second date to check. |
| 176 | + * @returns {boolean} Whether the two dates are equal. |
| 177 | + * Null dates are considered equal to other null dates. |
| 178 | + */ |
| 179 | + sameDate(first: D | null, second: D | null): boolean { |
| 180 | + return first && second ? !this.compareDate(first, second) : first == second; |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Clamp the given date between min and max dates. |
| 185 | + * @param date The date to clamp. |
| 186 | + * @param min The minimum value to allow. If null or omitted no min is enforced. |
| 187 | + * @param max The maximum value to allow. If null or omitted no max is enforced. |
| 188 | + * @returns `min` if `date` is less than `min`, `max` if date is greater than `max`, |
| 189 | + * otherwise `date`. |
| 190 | + */ |
| 191 | + clampDate(date: D, min?: D | null, max?: D | null): D { |
| 192 | + if (min && this.compareDate(date, min) < 0) { |
| 193 | + return min; |
| 194 | + } |
| 195 | + if (max && this.compareDate(date, max) > 0) { |
| 196 | + return max; |
| 197 | + } |
| 198 | + return date; |
| 199 | + } |
| 200 | +} |
0 commit comments