From da8970e975fd34c7a1ed601b63f8936026479e72 Mon Sep 17 00:00:00 2001 From: "Goh Yisheng (Andrew)" <9Y5@users.noreply.github.com> Date: Thu, 4 May 2017 02:35:11 +0800 Subject: [PATCH] Add types for fecha. (#16284) --- types/fecha/fecha-tests.ts | 124 +++++++++++++++++++++++++++++++++++++ types/fecha/index.d.ts | 52 ++++++++++++++++ types/fecha/tsconfig.json | 22 +++++++ types/fecha/tslint.json | 1 + 4 files changed, 199 insertions(+) create mode 100644 types/fecha/fecha-tests.ts create mode 100644 types/fecha/index.d.ts create mode 100644 types/fecha/tsconfig.json create mode 100644 types/fecha/tslint.json diff --git a/types/fecha/fecha-tests.ts b/types/fecha/fecha-tests.ts new file mode 100644 index 0000000000..0d0ce48638 --- /dev/null +++ b/types/fecha/fecha-tests.ts @@ -0,0 +1,124 @@ +import fecha = require('fecha'); + +// test fecha.parse +fecha.parse("February 3rd, 2014", "MMMM Do, YYYY"); +fecha.parse("5/3/98", "shortDate"); + +// test override fecha.i18n +fecha.i18n = { + dayNamesShort: ["Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"], + dayNames: [ + "Sunday", + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday" + ], + monthNamesShort: [ + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec" + ], + monthNames: [ + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December" + ], + amPm: ["am", "pm"], + DoFn(D: number) { + return D + "th"; + } +}; + +// just change one default mask +fecha.masks.shortDate = "M/D/YY"; + +// test override fecha.masks with an object. Must implement all keys. +// if you want to implement partially, use +// fecha.masks = Object.assign(fecha.masks, {shortDate: 'M/D/YY'}) for example. +fecha.masks = { + default: "ddd MMM DD YYYY HH:mm:ss", + shortDate: "M/D/YY", + mediumDate: "MMM D, YYYY", + longDate: "MMMM D, YYYY", + fullDate: "dddd, MMMM D, YYYY", + shortTime: "HH:mm", + mediumTime: "HH:mm:ss", + longTime: "HH:mm:ss.SSS" +}; + +// test add custom named mask. +// fecha.masks.myMask = "HH:mm:ss YY/MM/DD"; does not work for now. +fecha.masks['myMask'] = "HH:mm:ss YY/MM/DD"; + +// test fecha.format without i18nSettings, with Date object. +fecha.format(new Date(2014, 5, 6, 14, 10, 45), "myMask"); + +// test fecha.format without i18nSettings with number. +fecha.format(Date.now(), "myMask"); + +// test override i18nSettings with fecha.format +fecha.format(new Date(2014, 5, 6, 14, 10, 45), "myMask", { + dayNamesShort: ["Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"], + dayNames: [ + "Sunday", + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday" + ], + monthNamesShort: [ + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec" + ], + monthNames: [ + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December" + ], + amPm: ["am", "pm"], + DoFn(D: number) { + return D + "th"; + } +}); diff --git a/types/fecha/index.d.ts b/types/fecha/index.d.ts new file mode 100644 index 0000000000..f68d26a316 --- /dev/null +++ b/types/fecha/index.d.ts @@ -0,0 +1,52 @@ +// Type definitions for fecha 2.3 +// Project: https://github.com/taylorhakes/fecha +// Definitions by: Goh Yisheng +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export type Days = [string, string, string, string, string, string, string]; + +export type Months = [ + string, + string, + string, + string, + string, + string, + string, + string, + string, + string, + string, + string +]; + +export interface i18nSettings { + amPm: [string, string]; + dayNames: Days; + dayNamesShort: Days; + monthNames: Months; + monthNamesShort: Months; + DoFn(D: number): string; +} + +export interface Masks { + default: string; + fullDate: string; + longDate: string; + longTime: string; + mediumDate: string; + mediumTime: string; + shortDate: string; + shortTime: string; + [myMask: string]: string; +} + +export let masks: Masks; + +export let i18n: i18nSettings; + +export function format(dateObj: Date | number, mask: string, i18nSettings?: i18nSettings): string; + +export function parse(dateStr: string, format: string, i18nSettings?: i18nSettings): Date; + +export as namespace Fecha; diff --git a/types/fecha/tsconfig.json b/types/fecha/tsconfig.json new file mode 100644 index 0000000000..581630d1b6 --- /dev/null +++ b/types/fecha/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "fecha-tests.ts" + ] +} diff --git a/types/fecha/tslint.json b/types/fecha/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/fecha/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }