Skip to content

Commit

Permalink
Merge pull request #35 from 9Y5/yisheng/add-typescript-declaration-file
Browse files Browse the repository at this point in the history
Add typescript declaration file.
  • Loading branch information
taylorhakes authored May 9, 2017
2 parents 3e397ff + 4953cff commit 623fa27
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 1 deletion.
47 changes: 47 additions & 0 deletions fecha.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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;
124 changes: 124 additions & 0 deletions fecha.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import fecha = require('./fecha.js');

// 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: function(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 yet.
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: function(D: number) {
return D + "th";
}
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
"files": [
"fecha.js",
"fecha.min.js"
]
],
"types": "./fecha.d.ts"
}

0 comments on commit 623fa27

Please sign in to comment.