UNPKG

7.25 kBJavaScriptView Raw
1var painless = require('painless');
2var fecha = require('./fecha');
3var test = painless.createGroup();
4var assert = painless.assert;
5var today = new Date();
6var year = today.getFullYear();
7
8function testParse(name, str, format, date) {
9 test(name, function() {
10 assert.equal(+fecha.parse(str, format), +date);
11 });
12}
13
14function testFormat(name, dateObj, format, expected) {
15 test(name, function () {
16 assert.equal(fecha.format(dateObj, format), expected);
17 });
18}
19
20testParse('basic date parse', '2012/05/03', 'YYYY/MM/DD', new Date(2012, 4, 3));
21testParse('basic date parse with time', '2012/05/03 05:01:40', 'YYYY/MM/DD HH:mm:ss', new Date(2012, 4, 3, 5, 1, 40));
22testParse('date with different slashes', '2012-05-03 05:01:40', 'YYYY-MM-DD HH:mm:ss', new Date(2012, 4, 3, 5, 1, 40));
23testParse('date with different order', '11-7-97', 'D-M-YY', new Date(1997, 6, 11));
24testParse('date very short', '2-8-04', 'D-M-YY', new Date(2004, 7, 2));
25testParse('compact', '11081997', 'MMDDYYYY', new Date(1997, 10, 8));
26testParse('month names', 'March 3rd, 1999', 'MMMM Do, YYYY', new Date(1999, 2, 3));
27testParse('month names short', 'Jun 12, 2003', 'MMM D, YYYY', new Date(2003, 5, 12));
28testParse('day name', 'Wednesday Feb 03, 2100', 'dddd MMM DD, YYYY', new Date(2100, 1, 3));
29testParse('ampm 10PM', '2015-11-07 10PM', 'YYYY-MM-DD hhA', new Date(2015, 10, 7, 22));
30testParse('ampm 9AM', '2015-11-07 9AM', 'YYYY-MM-DD hhA', new Date(2015, 10, 7, 9));
31testParse('ampm 12am', '2000-01-01 12AM', 'YYYY-MM-DD hhA', new Date(2000, 0, 1, 0));
32testParse('ampm 3am', '2000-01-01 12AM', 'YYYY-MM-DD hhA', new Date(2000, 0, 1, 0));
33testParse('ampm am lowercase', '2000-01-01 11am', 'YYYY-MM-DD hha', new Date(2000, 0, 1, 11));
34testParse('noon pm lowercase', '2000-01-01 12pm', 'YYYY-MM-DD hha', new Date(2000, 0, 1, 12));
35testParse('24 hour time long', '2000-01-01 20', 'YYYY-MM-DD HH', new Date(2000, 0, 1, 20));
36testParse('24 hour time long 02', '2000-01-01 02', 'YYYY-MM-DD HH', new Date(2000, 0, 1, 2));
37testParse('24 hour time short', '2000-01-01 3', 'YYYY-MM-DD H', new Date(2000, 0, 1, 3));
38testParse('milliseconds time', '10:20:30.123', 'HH:mm:ss.SSS', new Date(year, 0, 1, 10, 20, 30, 123));
39testParse('milliseconds medium', '10:20:30.12', 'HH:mm:ss.SS', new Date(year, 0, 1, 10, 20, 30, 120));
40testParse('milliseconds short', '10:20:30.1', 'HH:mm:ss.S', new Date(year, 0, 1, 10, 20, 30, 100));
41testParse('timezone offset', '09:20:31 GMT-0500 (EST)', 'HH:mm:ss ZZ', new Date(Date.UTC(year, 0, 1, 14, 20, 31)));
42testParse('UTC timezone offset', '09:20:31 GMT-0000 (UTC)', 'HH:mm:ss ZZ', new Date(Date.UTC(year, 0, 1, 9,20, 31)));
43testParse('UTC timezone offset without GMT', '09:20:31 -0000 (UTC)', 'HH:mm:ss ZZ', new Date(Date.UTC(year, 0, 1, 9,20, 31)));
44testParse('invalid date', 'hello', 'HH:mm:ss ZZ', false);
45test('invalid date no format', function () {
46 assert.throws(function () {
47 fecha.parse('hello');
48 });
49});
50test('no format specified', function () {
51 assert.throws(function () {
52 fecha.parse('2014-11-05', false);
53 });
54});
55test('long input false', function () {
56 var input = '';
57 for (var i = 0; i < 1002; i++) {
58 input += '1';
59 }
60 assert.isFalse(fecha.parse(input, 'HH'));
61});
62
63// Day of the month
64testFormat('Day of the month', new Date(2014, 2, 5), 'D', '5');
65testFormat('Day of the month padded', new Date(2014, 2, 5), 'DD', '05');
66
67// Day of the week
68testFormat('Day of the week short', new Date(2015, 0, 8), 'd', '4');
69testFormat('Day of the week long', new Date(2015, 0, 10), 'dd', '06');
70testFormat('Day of the week short name', new Date(2014, 2, 5), 'ddd', 'Wed');
71testFormat('Day of the week long name', new Date(2014, 2, 5), 'dddd', 'Wednesday');
72
73// Month
74testFormat('Month', new Date(2014, 2, 5), 'M', '3');
75testFormat('Month padded', new Date(2014, 2, 5), 'MM', '03');
76testFormat('Month short name', new Date(2014, 2, 5), 'MMM', 'Mar');
77testFormat('Month full name mmmm', new Date(2014, 2, 5), 'MMMM', 'March');
78
79// Year
80testFormat('Year short', new Date(2001, 2, 5), 'YY', '01');
81testFormat('Year long', new Date(2001, 2, 5), 'YYYY', '2001');
82
83// Hour
84testFormat('Hour 12 hour short', new Date(2001, 2, 5, 6), 'h', '6');
85testFormat('Hour 12 hour padded', new Date(2001, 2, 5, 6), 'hh', '06');
86testFormat('Hour 12 hour short 2', new Date(2001, 2, 5, 14), 'h', '2');
87testFormat('Hour 12 hour padded 2', new Date(2001, 2, 5, 14), 'hh', '02');
88testFormat('Hour 24 hour short', new Date(2001, 2, 5, 13), 'H', '13');
89testFormat('Hour 24 hour padded', new Date(2001, 2, 5, 13), 'HH', '13');
90testFormat('Hour 24 hour short', new Date(2001, 2, 5, 3), 'H', '3');
91testFormat('Hour 24 hour padded', new Date(2001, 2, 5, 3), 'HH', '03');
92
93// Minute
94testFormat('Minutes short', new Date(2001, 2, 5, 6, 7), 'm', '7');
95testFormat('Minutes padded', new Date(2001, 2, 5, 6, 7), 'mm', '07');
96
97// Seconds
98testFormat('Seconds short', new Date(2001, 2, 5, 6, 7, 2), 's', '2');
99testFormat('Seconds padded', new Date(2001, 2, 5, 6, 7, 2), 'ss', '02');
100
101// Milliseconds
102testFormat('milliseconds short', new Date(2001, 2, 5, 6, 7, 2, 500), 'S', '5');
103testFormat('milliseconds short 2', new Date(2001, 2, 5, 6, 7, 2, 2), 'S', '0');
104testFormat('milliseconds medium', new Date(2001, 2, 5, 6, 7, 2, 300), 'SS', '30');
105testFormat('milliseconds medium 2', new Date(2001, 2, 5, 6, 7, 2, 10), 'SS', '01');
106testFormat('milliseconds long', new Date(2001, 2, 5, 6, 7, 2, 5), 'SSS', '005');
107
108// AM PM
109testFormat('ampm am', new Date(2001, 2, 5, 3, 7, 2, 5), 'a', 'am');
110testFormat('ampm pm', new Date(2001, 2, 5, 15, 7, 2, 5), 'a', 'pm');
111testFormat('ampm AM', new Date(2001, 2, 5, 3, 7, 2, 5), 'A', 'AM');
112testFormat('ampm PM', new Date(2001, 2, 5, 16, 7, 2, 5), 'A', 'PM');
113
114// th, st, nd, rd
115testFormat('th', new Date(2001, 2, 11), 'Do', '11th');
116testFormat('st', new Date(2001, 2, 21), 'Do', '21st');
117testFormat('nd', new Date(2001, 2, 2), 'Do', '2nd');
118testFormat('rd', new Date(2001, 2, 23), 'Do', '23rd');
119
120// Timezone offset
121test('timezone offset', function () {
122 assert(fecha.format(new Date(2001, 2, 11), 'ZZ').match(/^[\+\-]\d{4}$/));
123});
124
125// Random groupings
126testFormat('MM-DD-YYYY HH:mm:ss', new Date(2001, 2, 5, 6, 7, 2, 5), 'MM-DD-YYYY HH:mm:ss',
127 '03-05-2001 06:07:02');
128testFormat('MMMM D, YY', new Date(1987, 0, 8, 6, 7, 2, 5), 'MMMM D, YY', 'January 8, 87');
129testFormat('M MMMM MM YYYY, YY', new Date(1987, 0, 8, 6, 7, 2, 5), 'M MMMM MM YYYY, YY',
130 '1 January 01 1987, 87');
131testFormat('YYYY/MM/DD HH:mm:ss', new Date(2031, 10, 29, 2, 1, 9, 5), 'YYYY/MM/DD HH:mm:ss',
132 '2031/11/29 02:01:09');
133testFormat('D-M-YYYY', new Date(2043, 8, 18, 2, 1, 9, 5), 'D-M-YYYY', '18-9-2043');
134testFormat('current date', new Date(), 'YYYY', '' + (new Date()).getFullYear());
135testFormat('mask', new Date(1999, 0, 2), 'mediumDate', 'Jan 2, 1999');
136testFormat('number date', 1325376000000, 'YYY-MM-DD HH:mm:ss', fecha.format(new Date(Date.UTC(2012,0,1)), 'YYY-MM-DD HH:mm:ss'));
137
138test('Invalid date', function () {
139 assert.throws(function () {
140 fecha.format('hello', 'YYYY');
141 });
142});
143test('Invalid date number', function () {
144 assert.throws(function () {
145 fecha.format(89237983724982374, 'YYYY');
146 });
147});
148test('string date', function () {
149 assert.throws(function () {
150 fecha.format('2011-10-01', 'MM-DD-YYYY')
151 })
152});
153