Skip to content

Commit fe2e7db

Browse files
author
taylorhakes
committed
Removed ability to leave out parameter
1 parent 66c66ac commit fe2e7db

File tree

5 files changed

+43
-47
lines changed

5 files changed

+43
-47
lines changed

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "fecha",
33
"main": "fecha.js",
4-
"version": "0.2.2",
4+
"version": "1.0.0",
55
"homepage": "https://github.com/taylorhakes/fecha",
66
"authors": [
77
"Taylor Hakes"

fecha.js

+29-37
Original file line numberDiff line numberDiff line change
@@ -129,18 +129,16 @@
129129
/***
130130
* Format a date
131131
* @method format
132-
* @param {Date|string} dateObj
132+
* @param {Date|number} dateObj
133133
* @param {string} mask Format of the date, i.e. 'mm-dd-yy' or 'shortDate'
134134
*/
135135
fecha.format = function (dateObj, mask) {
136-
// Passing date through Date applies Date.parse, if necessary
137-
if (typeof dateObj === 'string') {
138-
dateObj = fecha.parse(dateObj);
139-
} else if (!dateObj) {
140-
dateObj = new Date();
136+
if (typeof dateObj === 'number') {
137+
dateObj = new Date(dateObj);
141138
}
142-
if (isNaN(dateObj)) {
143-
throw new SyntaxError('invalid date');
139+
140+
if (!dateObj || typeof dateObj !== 'object' && typeof dateObj.getDate !== 'function') {
141+
throw new Error('Invalid Date in fecha.format');
144142
}
145143

146144
mask = fecha.masks[mask] || mask || fecha.masks['default'];
@@ -197,39 +195,33 @@
197195
* @returns {Date|boolean}
198196
*/
199197
fecha.parse = function (dateStr, format) {
200-
var time, isValid, dateInfo, today, date, info, index;
201-
202-
if (!format) {
203-
time = Date.parse(dateStr.replace(/\-/g, '/'));
204-
if (!isNaN(time)) {
205-
return new Date(time);
206-
} else {
207-
return false;
208-
}
198+
var isValid, dateInfo, today, date, info, index;
209199

210-
} else {
211-
format = fecha.masks[format] || format;
200+
if (typeof format !== 'string') {
201+
throw new Error('Invalid format in fecha.parse');
202+
}
212203

213-
isValid = true;
214-
dateInfo = {};
215-
format.replace(token, function ($0) {
216-
if (parseFlags[$0]) {
217-
info = parseFlags[$0];
218-
index = dateStr.search(info[0]);
219-
if (!~index) {
220-
isValid = false;
221-
} else {
222-
dateStr.replace(info[0], function (result) {
223-
info[1](dateInfo, result);
224-
dateStr = dateStr.substr(index + result.length);
225-
return result;
226-
});
227-
}
204+
format = fecha.masks[format] || format;
205+
206+
isValid = true;
207+
dateInfo = {};
208+
format.replace(token, function ($0) {
209+
if (parseFlags[$0]) {
210+
info = parseFlags[$0];
211+
index = dateStr.search(info[0]);
212+
if (!~index) {
213+
isValid = false;
214+
} else {
215+
dateStr.replace(info[0], function (result) {
216+
info[1](dateInfo, result);
217+
dateStr = dateStr.substr(index + result.length);
218+
return result;
219+
});
228220
}
221+
}
229222

230-
return parseFlags[$0] ? '' : $0.slice(1, $0.length - 1);
231-
});
232-
}
223+
return parseFlags[$0] ? '' : $0.slice(1, $0.length - 1);
224+
});
233225

234226
if (!isValid) {
235227
return false;

fecha.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fecha.spec.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,14 @@
7474
expect(fecha.parse('hello', 'HH:mm:ss ZZ')).toEqual(false);
7575
});
7676
it('invalid date no format', function () {
77-
expect(fecha.parse('hello')).toEqual(false);
77+
expect(function() {
78+
fecha.parse('hello')
79+
}).toThrow();
7880
});
7981
it('no format specified', function () {
80-
expect(fecha.parse('2014-11-05')).toEqual(new Date(2014, 10, 5));
81-
});
82-
it('another no format', function() {
83-
expect(fecha.parse('2015-02-29')).toEqual(new Date(2015, 1, 29));
82+
expect(function() {
83+
fecha.parse('2014-11-05', false)
84+
}).toThrow();
8485
});
8586
});
8687
describe('format', function () {
@@ -162,10 +163,13 @@
162163
}).toThrow();
163164
});
164165
it('Valid parse', function () {
165-
expect(fecha.format('2011-10-01', 'MM-DD-YYYY')).toBe('10-01-2011');
166+
167+
expect(function() {
168+
fecha.format('2011-10-01', 'MM-DD-YYYY')
169+
}).toThrow();
166170
});
167171
it('Current Date', function () {
168-
expect(fecha.format(null, 'YYYY')).toBe('' + (new Date()).getFullYear());
172+
expect(fecha.format(new Date(), 'YYYY')).toBe('' + (new Date()).getFullYear());
169173
});
170174
it('Mask', function () {
171175
expect(fecha.format(new Date(1999, 0, 2), 'mediumDate')).toBe('Jan 2, 1999');

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fecha",
3-
"version": "0.2.2",
3+
"version": "1.0.0",
44
"description": "Date formatting and parsing",
55
"main": "fecha.js",
66
"scripts": {

0 commit comments

Comments
 (0)