Skip to content

Commit 8574d89

Browse files
author
Codeship Server
committed
feat(input): Allow user to clear input and be valid if not required
Update parsers, fomatter, and validator to handle empty values and treat them as valid.
1 parent ca3b2a2 commit 8574d89

File tree

8 files changed

+344
-343
lines changed

8 files changed

+344
-343
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ script:
1616
- npm run test
1717
after_success:
1818
- npm run semantic-release
19+
- npm run coverage:upload
1920
branches:
2021
except:
2122
- "/^v\\d+\\.\\d+\\.\\d+$/"

Gruntfile.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33

44
Native AngularJS directive that allows user input of a date/time value. Valid dates are displayed in specified format, but input may be in any supported format.
55

6+
[![MIT License][license-image]][license-url]
67
[![Build Status](https://travis-ci.org/dalelotts/angular-date-time-input.png?branch=master)](https://travis-ci.org/dalelotts/angular-date-time-input)
8+
[![Coverage Status](https://coveralls.io/repos/github/dalelotts/angular-date-time-input/badge.svg?branch=master)](https://coveralls.io/github/dalelotts/angular-date-time-input?branch=master)
9+
[![Dependency Status](https://david-dm.org/dalelotts/angular-date-time-input.svg)](https://david-dm.org/dalelotts/angular-date-time-input)
10+
[![devDependency Status](https://david-dm.org/dalelotts/angular-date-time-input/dev-status.svg)](https://david-dm.org/dalelotts/angular-date-time-input#info=devDependencies)
11+
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
12+
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
13+
714

815
#Dependencies
916

demo/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
Input format is 'YYYY/MM/DD hh:mm A', 'YYYY/MM/DD', or 'MMM DD YYYY' (and ISO 8601, which is always supported).
6969
<br> Input parsing is strict (meaning input must exactly match one of input formats).
7070
<br> Display format is 'YYYY/MM/DD hh:mm A'.
71-
<br>Stored as a date: {{ data.date1 }}
71+
<br>Stored as a date: {{ data.date2 }}
7272
</p>
7373
<div class="alert alert-info" role="alert">
7474
<strong>Try these</strong>
@@ -94,7 +94,7 @@
9494
<br> <strong>Input parsing is NOT strict</strong> (in this case, the moment parser is very, very forgiving - entering only
9595
'1' will likely result in a valid date).
9696
<br> Display format is 'YYYY/MMM/DD hh:mm A'.
97-
<br>Stored as: {{ data.date1 }}
97+
<br>Stored as: {{ data.date3 }}
9898
</p>
9999
<div class="alert alert-info" role="alert">
100100
<strong>Non-strict parsing</strong> is very forgiving and can result in some unexpected behavior.

karma.conf.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ module.exports = function (config) {
4343
// optionally, configure the reporter
4444
coverageReporter: {
4545
reporters: [
46-
{type: 'json', dir: 'build/coverage/'},
47-
{type: 'html', dir: 'build/coverage/'}
46+
{type: 'lcov', dir: 'build/coverage'},
47+
{type: 'json', dir: 'build/coverage'},
48+
{type: 'html', dir: 'build/coverage'}
4849
]
4950
},
5051

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
},
2121
"devDependencies": {
2222
"angular-mocks": "^1.x",
23+
"coveralls": "^2.11.6",
2324
"cz-conventional-changelog": "^1.1.5",
24-
"grunt": "^0.4.4",
25+
"eslint": "^2.2.0",
2526
"grunt-bump": "^0.7.0",
2627
"gulp": "^3.8.11",
2728
"gulp-htmlmin": "^1.3.0",
@@ -31,24 +32,24 @@
3132
"jquery": "^2.2.0",
3233
"jshint": "^2.9.1",
3334
"jshint-stylish": "^2.1.0",
34-
"karma": "^0.13.19",
35+
"karma": "^0.13.21",
3536
"karma-chrome-launcher": "^0.2.2",
3637
"karma-coverage": "^0.5.3",
3738
"karma-firefox-launcher": "^0.1.7",
38-
"karma-jasmine": "^0.3.2",
39+
"karma-jasmine": "^0.3.7",
3940
"karma-phantomjs-launcher": "^1.0.0",
4041
"karma-threshold-reporter": "^0.1.12",
41-
"lodash": "^4.1.0",
42-
"matchdep": "^1.0.0",
43-
"phantomjs-prebuilt": "^2.1.3",
42+
"lodash": "^4.5.0",
43+
"phantomjs-prebuilt": "^2.1.4",
4444
"plato": "^1.5.0",
4545
"run-browser": "^2.0.2",
4646
"semantic-release": "^6.2.0",
47-
"standard": "^5.4.1",
47+
"standard": "^6.0.7",
4848
"tape": "^4.4.0"
4949
},
5050
"scripts": {
5151
"test": "npm run test-browserify && gulp",
52+
"coverage:upload": "cat build/coverage/*/lcov.info | coveralls",
5253
"test-browserify": "run-browser test/commonjs/browserify.test.js -b",
5354
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
5455
},

src/dateTimeInput.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/*jslint vars:true */
33

44
/**
5-
* @license angular-date-time-input version: 0.1.0
5+
* @license angular-date-time-input
66
* (c) 2013-2015 Knight Rider Consulting, Inc. http://www.knightrider.com
77
* License: MIT
88
*
@@ -35,20 +35,30 @@
3535
// Behaviors
3636
switch (modelType) {
3737
case 'Date':
38-
result = dateParser;
38+
result = handleEmpty(dateParser);
3939
break;
4040
case 'moment':
41-
result = momentParser;
41+
result = handleEmpty(momentParser);
4242
break;
4343
case 'milliseconds':
44-
result = millisecondParser;
44+
result = handleEmpty(millisecondParser);
4545
break;
4646
default: // It is assumed that the modelType is a formatting string.
47-
result = stringParserFactory(modelType);
47+
result = handleEmpty(stringParserFactory(modelType));
4848
}
4949

5050
return result;
5151

52+
function handleEmpty(delegate) {
53+
return function (viewValue) {
54+
if (angular.isUndefined(viewValue) || viewValue === '' || viewValue === null) {
55+
return null;
56+
} else {
57+
return delegate(viewValue);
58+
}
59+
};
60+
}
61+
5262
function dateParser(viewValue) {
5363
return momentParser(viewValue).toDate();
5464
}
@@ -118,20 +128,23 @@
118128
}
119129

120130
function validator(modelValue, viewValue) {
121-
return angular.isDefined(viewValue) ? moment(viewValue, inputFormats, moment.locale(), dateParseStrict).isValid() : true;
131+
if (angular.isUndefined(viewValue) || viewValue === '' || viewValue === null) {
132+
return true;
133+
}
134+
return moment(viewValue, inputFormats, moment.locale(), dateParseStrict).isValid();
122135
}
123136

124137
function formatter(modelValue) {
138+
if (angular.isUndefined(modelValue) || modelValue === '' || modelValue === null) {
139+
return null;
140+
}
125141

126142
if (angular.isDate(modelValue)) {
127143
return moment(modelValue).format(displayFormat);
128144
} else if (angular.isNumber(modelValue)) {
129145
return moment.utc(modelValue).format(displayFormat);
130-
} else if (angular.isDefined(modelValue)) {
131-
return moment(modelValue, inputFormats, moment.locale(), dateParseStrict).format(displayFormat);
132146
}
133-
134-
return modelValue;
147+
return moment(modelValue, inputFormats, moment.locale(), dateParseStrict).format(displayFormat);
135148
}
136149

137150
function applyFormatters() {

0 commit comments

Comments
 (0)