Skip to content

Commit

Permalink
Removed ability to leave out parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorhakes committed Sep 6, 2015
1 parent 66c66ac commit fe2e7db
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 47 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fecha",
"main": "fecha.js",
"version": "0.2.2",
"version": "1.0.0",
"homepage": "https://github.com/taylorhakes/fecha",
"authors": [
"Taylor Hakes"
Expand Down
66 changes: 29 additions & 37 deletions fecha.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,16 @@
/***
* Format a date
* @method format
* @param {Date|string} dateObj
* @param {Date|number} dateObj
* @param {string} mask Format of the date, i.e. 'mm-dd-yy' or 'shortDate'
*/
fecha.format = function (dateObj, mask) {
// Passing date through Date applies Date.parse, if necessary
if (typeof dateObj === 'string') {
dateObj = fecha.parse(dateObj);
} else if (!dateObj) {
dateObj = new Date();
if (typeof dateObj === 'number') {
dateObj = new Date(dateObj);
}
if (isNaN(dateObj)) {
throw new SyntaxError('invalid date');

if (!dateObj || typeof dateObj !== 'object' && typeof dateObj.getDate !== 'function') {
throw new Error('Invalid Date in fecha.format');
}

mask = fecha.masks[mask] || mask || fecha.masks['default'];
Expand Down Expand Up @@ -197,39 +195,33 @@
* @returns {Date|boolean}
*/
fecha.parse = function (dateStr, format) {
var time, isValid, dateInfo, today, date, info, index;

if (!format) {
time = Date.parse(dateStr.replace(/\-/g, '/'));
if (!isNaN(time)) {
return new Date(time);
} else {
return false;
}
var isValid, dateInfo, today, date, info, index;

} else {
format = fecha.masks[format] || format;
if (typeof format !== 'string') {
throw new Error('Invalid format in fecha.parse');
}

isValid = true;
dateInfo = {};
format.replace(token, function ($0) {
if (parseFlags[$0]) {
info = parseFlags[$0];
index = dateStr.search(info[0]);
if (!~index) {
isValid = false;
} else {
dateStr.replace(info[0], function (result) {
info[1](dateInfo, result);
dateStr = dateStr.substr(index + result.length);
return result;
});
}
format = fecha.masks[format] || format;

isValid = true;
dateInfo = {};
format.replace(token, function ($0) {
if (parseFlags[$0]) {
info = parseFlags[$0];
index = dateStr.search(info[0]);
if (!~index) {
isValid = false;
} else {
dateStr.replace(info[0], function (result) {
info[1](dateInfo, result);
dateStr = dateStr.substr(index + result.length);
return result;
});
}
}

return parseFlags[$0] ? '' : $0.slice(1, $0.length - 1);
});
}
return parseFlags[$0] ? '' : $0.slice(1, $0.length - 1);
});

if (!isValid) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion fecha.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions fecha.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,14 @@
expect(fecha.parse('hello', 'HH:mm:ss ZZ')).toEqual(false);
});
it('invalid date no format', function () {
expect(fecha.parse('hello')).toEqual(false);
expect(function() {
fecha.parse('hello')
}).toThrow();
});
it('no format specified', function () {
expect(fecha.parse('2014-11-05')).toEqual(new Date(2014, 10, 5));
});
it('another no format', function() {
expect(fecha.parse('2015-02-29')).toEqual(new Date(2015, 1, 29));
expect(function() {
fecha.parse('2014-11-05', false)
}).toThrow();
});
});
describe('format', function () {
Expand Down Expand Up @@ -162,10 +163,13 @@
}).toThrow();
});
it('Valid parse', function () {
expect(fecha.format('2011-10-01', 'MM-DD-YYYY')).toBe('10-01-2011');

expect(function() {
fecha.format('2011-10-01', 'MM-DD-YYYY')
}).toThrow();
});
it('Current Date', function () {
expect(fecha.format(null, 'YYYY')).toBe('' + (new Date()).getFullYear());
expect(fecha.format(new Date(), 'YYYY')).toBe('' + (new Date()).getFullYear());
});
it('Mask', function () {
expect(fecha.format(new Date(1999, 0, 2), 'mediumDate')).toBe('Jan 2, 1999');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fecha",
"version": "0.2.2",
"version": "1.0.0",
"description": "Date formatting and parsing",
"main": "fecha.js",
"scripts": {
Expand Down

0 comments on commit fe2e7db

Please sign in to comment.