Skip to content

Instantly share code, notes, and snippets.

@fxck
Created May 25, 2017 09:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fxck/efe4ccff13d99ee9a2dcf82365909168 to your computer and use it in GitHub Desktop.
Save fxck/efe4ccff13d99ee9a2dcf82365909168 to your computer and use it in GitHub Desktop.
/* tslint:disable */
import { CptDateAdapter } from './date-adapter';
import {
getYear,
getMonth,
getDate,
getDay,
getDaysInMonth,
parse as dateFnsParse,
addYears,
addMonths,
addDays,
isSameMinute,
isValid,
setHours,
setMinutes,
getHours,
getMinutes
} from 'date-fns/esm';
// TODO(mmalerba): Remove when we no longer support safari 9.
/** Whether the browser supports the Intl API. */
const SUPPORTS_INTL_API = typeof Intl != 'undefined';
/** The default month names to use if Intl API is not available. */
const DEFAULT_MONTH_NAMES = {
'long': [
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September',
'October', 'November', 'December'
],
'short': ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
'narrow': ['J', 'F', 'M', 'A', 'M', 'J', 'J', 'A', 'S', 'O', 'N', 'D']
};
/** The default date names to use if Intl API is not available. */
const DEFAULT_DATE_NAMES = range(31, i => String(i + 1));
/** The default day of the week names to use if Intl API is not available. */
const DEFAULT_DAY_OF_WEEK_NAMES = {
'long': ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
'short': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
'narrow': ['S', 'M', 'T', 'W', 'T', 'F', 'S']
};
/** Creates an array and fills it with values. */
function range<T>(length: number, valueFunction: (index: number) => T): T[] {
return Array.apply(null, Array(length)).map((_: undefined, i: number) => valueFunction(i));
}
export class DateFnsDateAdapter extends CptDateAdapter<Date> {
getYear(date: Date): number {
return getYear(date);
}
getMonth(date: Date): number {
return getMonth(date);
}
getDate(date: Date): number {
return getDate(date);
}
getDayOfWeek(date: Date): number {
return getDay(date);
}
getMonthNames(style: 'long' | 'short' | 'narrow'): string[] {
if (SUPPORTS_INTL_API) {
let dtf = new Intl.DateTimeFormat(this.locale, {month: style});
return range(12, i => dtf.format(new Date(2017, i, 1)));
}
return DEFAULT_MONTH_NAMES[style];
}
getDateNames(): string[] {
if (SUPPORTS_INTL_API) {
let dtf = new Intl.DateTimeFormat(this.locale, {day: 'numeric'});
return range(31, i => dtf.format(new Date(2017, 0, i + 1)));
}
return DEFAULT_DATE_NAMES;
}
getDayOfWeekNames(style: 'long' | 'short' | 'narrow'): string[] {
if (SUPPORTS_INTL_API) {
let dtf = new Intl.DateTimeFormat(this.locale, {weekday: style});
return range(7, i => dtf.format(new Date(2017, 0, i + 1)));
}
return DEFAULT_DAY_OF_WEEK_NAMES[style];
}
getYearName(date: Date): string {
if (SUPPORTS_INTL_API) {
let dtf = new Intl.DateTimeFormat(this.locale, {year: 'numeric'});
return dtf.format(date);
}
return String(this.getYear(date));
}
getFirstDayOfWeek(): number {
// We can't tell using native JS Date what the first day of the week is, we default to Sunday.
return 0;
}
getNumDaysInMonth(date: Date): number {
return getDaysInMonth(date);
}
clone(date: Date): Date {
return this.createDate(this.getYear(date), this.getMonth(date), this.getDate(date));
}
createDate(year: number, month: number, date: number): Date {
return this.parse(new Date(year, month, date));
}
today(): Date {
return new Date();
}
parse(value: any, parseFormat?: string): Date | null {
if (value instanceof Date) {
return value;
}
const parsedDate = dateFnsParse(value, parseFormat, new Date());
const r = value && isValid(parsedDate) ? parsedDate : null;
return r;
}
format(date: Date, displayFormat: Object): string {
if (SUPPORTS_INTL_API) {
let dtf = new Intl.DateTimeFormat(this.locale, displayFormat);
return dtf.format(date);
}
return date.toDateString();
}
addCalendarYears(date: Date, years: number): Date {
return addYears(date, years);
}
addCalendarMonths(date: Date, months: number): Date {
return addMonths(date, months);
}
addCalendarDays(date: Date, days: number): Date {
return addDays(date, days);
}
getISODateString(date: Date): string {
return [
date.getUTCFullYear(),
this._2digit(date.getUTCMonth() + 1),
this._2digit(date.getUTCDate())
].join('-');
}
/**
* Pads a number to make it two digits.
* @param n The number to pad.
* @returns The padded number.
*/
private _2digit(n: number) {
return ('00' + n).slice(-2);
}
getHour(date: Date): number {
return getHours(date);
}
getMinute(date: Date): number {
return getMinutes(date);
}
setHour(date: Date, hour: number): Date {
return setHours(date, hour);
}
setMinute(date: Date, minute: number): Date {
return setMinutes(date, minute);
}
sameTime(first: Date | null, second: Date | null): boolean {
return isSameMinute(first, second);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment