Skip to content

Commit 623fa27

Browse files
authored
Merge pull request #35 from 9Y5/yisheng/add-typescript-declaration-file
Add typescript declaration file.
2 parents 3e397ff + 4953cff commit 623fa27

File tree

3 files changed

+173
-1
lines changed

3 files changed

+173
-1
lines changed

fecha.d.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
export type Days = [string, string, string, string, string, string, string];
2+
3+
export type Months = [
4+
string,
5+
string,
6+
string,
7+
string,
8+
string,
9+
string,
10+
string,
11+
string,
12+
string,
13+
string,
14+
string,
15+
string
16+
];
17+
18+
export interface i18nSettings {
19+
amPm: [string, string];
20+
dayNames: Days;
21+
dayNamesShort: Days;
22+
monthNames: Months;
23+
monthNamesShort: Months;
24+
DoFn(D: number): string;
25+
}
26+
27+
export interface Masks {
28+
default: string;
29+
fullDate: string;
30+
longDate: string;
31+
longTime: string;
32+
mediumDate: string;
33+
mediumTime: string;
34+
shortDate: string;
35+
shortTime: string;
36+
[myMask: string]: string;
37+
}
38+
39+
export let masks: Masks;
40+
41+
export let i18n: i18nSettings;
42+
43+
export function format(dateObj: Date | number, mask: string, i18nSettings?: i18nSettings): string;
44+
45+
export function parse(dateStr: string, format: string, i18nSettings?: i18nSettings): Date;
46+
47+
export as namespace Fecha;

fecha.test.ts

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import fecha = require('./fecha.js');
2+
3+
// test fecha.parse
4+
fecha.parse("February 3rd, 2014", "MMMM Do, YYYY");
5+
fecha.parse("5/3/98", "shortDate");
6+
7+
// test override fecha.i18n
8+
fecha.i18n = {
9+
dayNamesShort: ["Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"],
10+
dayNames: [
11+
"Sunday",
12+
"Monday",
13+
"Tuesday",
14+
"Wednesday",
15+
"Thursday",
16+
"Friday",
17+
"Saturday"
18+
],
19+
monthNamesShort: [
20+
"Jan",
21+
"Feb",
22+
"Mar",
23+
"Apr",
24+
"May",
25+
"Jun",
26+
"Jul",
27+
"Aug",
28+
"Sep",
29+
"Oct",
30+
"Nov",
31+
"Dec"
32+
],
33+
monthNames: [
34+
"January",
35+
"February",
36+
"March",
37+
"April",
38+
"May",
39+
"June",
40+
"July",
41+
"August",
42+
"September",
43+
"October",
44+
"November",
45+
"December"
46+
],
47+
amPm: ["am", "pm"],
48+
DoFn: function(D: number) {
49+
return D + "th";
50+
}
51+
};
52+
53+
// just change one default mask
54+
fecha.masks.shortDate = "M/D/YY";
55+
56+
// test override fecha.masks with an object. Must implement all keys.
57+
// if you want to implement partially, use
58+
// fecha.masks = Object.assign(fecha.masks, {shortDate: 'M/D/YY'}) for example.
59+
fecha.masks = {
60+
default: "ddd MMM DD YYYY HH:mm:ss",
61+
shortDate: "M/D/YY",
62+
mediumDate: "MMM D, YYYY",
63+
longDate: "MMMM D, YYYY",
64+
fullDate: "dddd, MMMM D, YYYY",
65+
shortTime: "HH:mm",
66+
mediumTime: "HH:mm:ss",
67+
longTime: "HH:mm:ss.SSS"
68+
};
69+
70+
// test add custom named mask.
71+
// fecha.masks.myMask = "HH:mm:ss YY/MM/DD"; does not work yet.
72+
fecha.masks["myMask"] = "HH:mm:ss YY/MM/DD";
73+
74+
// test fecha.format without i18nSettings, with Date object.
75+
fecha.format(new Date(2014, 5, 6, 14, 10, 45), "myMask");
76+
77+
// test fecha.format without i18nSettings with number.
78+
fecha.format(Date.now(), "myMask");
79+
80+
// test override i18nSettings with fecha.format
81+
fecha.format(new Date(2014, 5, 6, 14, 10, 45), "myMask", {
82+
dayNamesShort: ["Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"],
83+
dayNames: [
84+
"Sunday",
85+
"Monday",
86+
"Tuesday",
87+
"Wednesday",
88+
"Thursday",
89+
"Friday",
90+
"Saturday"
91+
],
92+
monthNamesShort: [
93+
"Jan",
94+
"Feb",
95+
"Mar",
96+
"Apr",
97+
"May",
98+
"Jun",
99+
"Jul",
100+
"Aug",
101+
"Sep",
102+
"Oct",
103+
"Nov",
104+
"Dec"
105+
],
106+
monthNames: [
107+
"January",
108+
"February",
109+
"March",
110+
"April",
111+
"May",
112+
"June",
113+
"July",
114+
"August",
115+
"September",
116+
"October",
117+
"November",
118+
"December"
119+
],
120+
amPm: ["am", "pm"],
121+
DoFn: function(D: number) {
122+
return D + "th";
123+
}
124+
});

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
"files": [
3535
"fecha.js",
3636
"fecha.min.js"
37-
]
37+
],
38+
"types": "./fecha.d.ts"
3839
}

0 commit comments

Comments
 (0)