Skip to content

Commit

Permalink
100% test coverage. Throw error on invalid date. Updated to 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorhakes committed Feb 13, 2016
1 parent 04dce44 commit d0a3d15
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 78 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
coverage
coverage
.nyc_output
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## 2.0.0
Fecha now throws errors on invalid dates in `fecha.format` and is stricter about what dates it accepts. Dates must pass `Object.prototype.toString.call(dateObj) !== '[object Date]'`.
12 changes: 0 additions & 12 deletions Gulpfile.js

This file was deleted.

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": "1.2.0",
"version": "2.0.0",
"homepage": "https://github.com/taylorhakes/fecha",
"authors": [
"Taylor Hakes"
Expand Down
3 changes: 2 additions & 1 deletion fecha.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
dateObj = new Date(dateObj);
}

if (!dateObj || typeof dateObj !== 'object' && typeof dateObj.getDate !== 'function') {
if (Object.prototype.toString.call(dateObj) !== '[object Date]' || isNaN(dateObj.getTime())) {
throw new Error('Invalid Date in fecha.format');
}

Expand Down Expand Up @@ -256,6 +256,7 @@
return date;
};

/* istanbul ignore next */
if (typeof module !== 'undefined' && module.exports) {
module.exports = fecha;
} else if (typeof define === 'function' && define.amd) {
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.

56 changes: 0 additions & 56 deletions karma.conf.js

This file was deleted.

12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "fecha",
"version": "1.2.2",
"version": "2.0.0",
"description": "Date formatting and parsing",
"main": "fecha.js",
"scripts": {
"test": "node test.js"
"test": "nyc --cache --reporter=text node test.js",
"build": "uglifyjs fecha.js -m -o fecha.min.js"
},
"repository": {
"type": "git",
Expand All @@ -25,9 +26,8 @@
},
"homepage": "https://github.com/taylorhakes/fecha",
"devDependencies": {
"gulp": "^3.8.10",
"gulp-rename": "^1.2.0",
"gulp-uglify": "^1.0.2",
"painless": "^0.6.1"
"nyc": "^5.6.0",
"painless": "^0.6.1",
"uglify-js": "^2.6.1"
}
}
7 changes: 7 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ testParse('milliseconds medium', '10:20:30.12', 'HH:mm:ss.SS', new Date(year, 0,
testParse('milliseconds short', '10:20:30.1', 'HH:mm:ss.S', new Date(year, 0, 1, 10, 20, 30, 100));
testParse('timezone offset', '09:20:31 GMT-0500 (EST)', 'HH:mm:ss ZZ', new Date(Date.UTC(year, 0, 1, 14, 20, 31)));
testParse('UTC timezone offset', '09:20:31 GMT-0000 (UTC)', 'HH:mm:ss ZZ', new Date(Date.UTC(year, 0, 1, 9,20, 31)));
testParse('UTC timezone offset without GMT', '09:20:31 -0000 (UTC)', 'HH:mm:ss ZZ', new Date(Date.UTC(year, 0, 1, 9,20, 31)));
testParse('invalid date', 'hello', 'HH:mm:ss ZZ', false);
test('invalid date no format', function () {
assert.throws(function () {
Expand Down Expand Up @@ -132,12 +133,18 @@ testFormat('YYYY/MM/DD HH:mm:ss', new Date(2031, 10, 29, 2, 1, 9, 5), 'YYYY/MM/D
testFormat('D-M-YYYY', new Date(2043, 8, 18, 2, 1, 9, 5), 'D-M-YYYY', '18-9-2043');
testFormat('current date', new Date(), 'YYYY', '' + (new Date()).getFullYear());
testFormat('mask', new Date(1999, 0, 2), 'mediumDate', 'Jan 2, 1999');
testFormat('number date', 1325376000000, 'YYY-MM-DD HH:mm:ss', fecha.format(new Date(Date.UTC(2012,0,1)), 'YYY-MM-DD HH:mm:ss'));

test('Invalid date', function () {
assert.throws(function () {
fecha.format('hello', 'YYYY');
});
});
test('Invalid date number', function () {
assert.throws(function () {
fecha.format(89237983724982374, 'YYYY');
});
});
test('string date', function () {
assert.throws(function () {
fecha.format('2011-10-01', 'MM-DD-YYYY')
Expand Down

0 comments on commit d0a3d15

Please sign in to comment.