Skip to content

Commit

Permalink
feat(input): Allow user to clear input and be valid if not required
Browse files Browse the repository at this point in the history
Update parsers, fomatter, and validator to handle empty values and treat them as valid.
  • Loading branch information
Codeship Server committed Feb 23, 2016
1 parent ca3b2a2 commit 8574d89
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 343 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ script:
- npm run test
after_success:
- npm run semantic-release
- npm run coverage:upload
branches:
except:
- "/^v\\d+\\.\\d+\\.\\d+$/"
22 changes: 0 additions & 22 deletions Gruntfile.js

This file was deleted.

7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@

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.

[![MIT License][license-image]][license-url]
[![Build Status](https://travis-ci.org/dalelotts/angular-date-time-input.png?branch=master)](https://travis-ci.org/dalelotts/angular-date-time-input)
[![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)
[![Dependency Status](https://david-dm.org/dalelotts/angular-date-time-input.svg)](https://david-dm.org/dalelotts/angular-date-time-input)
[![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)
[![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)
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)


#Dependencies

Expand Down
4 changes: 2 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
Input format is 'YYYY/MM/DD hh:mm A', 'YYYY/MM/DD', or 'MMM DD YYYY' (and ISO 8601, which is always supported).
<br> Input parsing is strict (meaning input must exactly match one of input formats).
<br> Display format is 'YYYY/MM/DD hh:mm A'.
<br>Stored as a date: {{ data.date1 }}
<br>Stored as a date: {{ data.date2 }}
</p>
<div class="alert alert-info" role="alert">
<strong>Try these</strong>
Expand All @@ -94,7 +94,7 @@
<br> <strong>Input parsing is NOT strict</strong> (in this case, the moment parser is very, very forgiving - entering only
'1' will likely result in a valid date).
<br> Display format is 'YYYY/MMM/DD hh:mm A'.
<br>Stored as: {{ data.date1 }}
<br>Stored as: {{ data.date3 }}
</p>
<div class="alert alert-info" role="alert">
<strong>Non-strict parsing</strong> is very forgiving and can result in some unexpected behavior.
Expand Down
5 changes: 3 additions & 2 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ module.exports = function (config) {
// optionally, configure the reporter
coverageReporter: {
reporters: [
{type: 'json', dir: 'build/coverage/'},
{type: 'html', dir: 'build/coverage/'}
{type: 'lcov', dir: 'build/coverage'},
{type: 'json', dir: 'build/coverage'},
{type: 'html', dir: 'build/coverage'}
]
},

Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
},
"devDependencies": {
"angular-mocks": "^1.x",
"coveralls": "^2.11.6",
"cz-conventional-changelog": "^1.1.5",
"grunt": "^0.4.4",
"eslint": "^2.2.0",
"grunt-bump": "^0.7.0",
"gulp": "^3.8.11",
"gulp-htmlmin": "^1.3.0",
Expand All @@ -31,24 +32,24 @@
"jquery": "^2.2.0",
"jshint": "^2.9.1",
"jshint-stylish": "^2.1.0",
"karma": "^0.13.19",
"karma": "^0.13.21",
"karma-chrome-launcher": "^0.2.2",
"karma-coverage": "^0.5.3",
"karma-firefox-launcher": "^0.1.7",
"karma-jasmine": "^0.3.2",
"karma-jasmine": "^0.3.7",
"karma-phantomjs-launcher": "^1.0.0",
"karma-threshold-reporter": "^0.1.12",
"lodash": "^4.1.0",
"matchdep": "^1.0.0",
"phantomjs-prebuilt": "^2.1.3",
"lodash": "^4.5.0",
"phantomjs-prebuilt": "^2.1.4",
"plato": "^1.5.0",
"run-browser": "^2.0.2",
"semantic-release": "^6.2.0",
"standard": "^5.4.1",
"standard": "^6.0.7",
"tape": "^4.4.0"
},
"scripts": {
"test": "npm run test-browserify && gulp",
"coverage:upload": "cat build/coverage/*/lcov.info | coveralls",
"test-browserify": "run-browser test/commonjs/browserify.test.js -b",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
Expand Down
33 changes: 23 additions & 10 deletions src/dateTimeInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*jslint vars:true */

/**
* @license angular-date-time-input version: 0.1.0
* @license angular-date-time-input
* (c) 2013-2015 Knight Rider Consulting, Inc. http://www.knightrider.com
* License: MIT
*
Expand Down Expand Up @@ -35,20 +35,30 @@
// Behaviors
switch (modelType) {
case 'Date':
result = dateParser;
result = handleEmpty(dateParser);
break;
case 'moment':
result = momentParser;
result = handleEmpty(momentParser);
break;
case 'milliseconds':
result = millisecondParser;
result = handleEmpty(millisecondParser);
break;
default: // It is assumed that the modelType is a formatting string.
result = stringParserFactory(modelType);
result = handleEmpty(stringParserFactory(modelType));
}

return result;

function handleEmpty(delegate) {
return function (viewValue) {
if (angular.isUndefined(viewValue) || viewValue === '' || viewValue === null) {
return null;
} else {
return delegate(viewValue);
}
};
}

function dateParser(viewValue) {
return momentParser(viewValue).toDate();
}
Expand Down Expand Up @@ -118,20 +128,23 @@
}

function validator(modelValue, viewValue) {
return angular.isDefined(viewValue) ? moment(viewValue, inputFormats, moment.locale(), dateParseStrict).isValid() : true;
if (angular.isUndefined(viewValue) || viewValue === '' || viewValue === null) {
return true;
}
return moment(viewValue, inputFormats, moment.locale(), dateParseStrict).isValid();
}

function formatter(modelValue) {
if (angular.isUndefined(modelValue) || modelValue === '' || modelValue === null) {
return null;
}

if (angular.isDate(modelValue)) {
return moment(modelValue).format(displayFormat);
} else if (angular.isNumber(modelValue)) {
return moment.utc(modelValue).format(displayFormat);
} else if (angular.isDefined(modelValue)) {
return moment(modelValue, inputFormats, moment.locale(), dateParseStrict).format(displayFormat);
}

return modelValue;
return moment(modelValue, inputFormats, moment.locale(), dateParseStrict).format(displayFormat);
}

function applyFormatters() {
Expand Down
Loading

0 comments on commit 8574d89

Please sign in to comment.