Skip to content

Commit 2937184

Browse files
committed
feat(careless): Add careless matching
Closes #22
1 parent 81fc91e commit 2937184

File tree

7 files changed

+106
-9
lines changed

7 files changed

+106
-9
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ Password: <input ng-model="user.password" type="password" />
3939
Confirm: <input ng-model="user.passwordConfirm" type="password" match="user.password" />
4040
```
4141

42+
**Case insensitive (caseless) Example**
43+
44+
```html
45+
Password: <input ng-model="user.password" type="password" />
46+
Confirm: <input ng-model="user.passwordConfirm" type="password" match="user.password" match-caseless="true" />
47+
```
48+
<small>`match-caseless` can accept a scoped variable to allow the matching to be toggle-able between case insensitive and case sensitive.</small>
49+
4250
**Display Custom Error**<br>
4351
If your form and field both are named, you can access the validation result to show/hide messages.
4452

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-input-match",
3-
"version": "1.4.1",
3+
"version": "1.5.0",
44
"homepage": "https://github.com/TheSharpieOne/angular-input-match",
55
"authors": [
66
"TheSharpieOne <evan@lostonia.com>"

dist/angular-input-match.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
* angular-input-match
33
* Checks if one input matches another
4-
* @version v1.4.1
4+
* @version v1.5.0
55
* @link https://github.com/TheSharpieOne/angular-input-match
66
* @license MIT License, http://www.opensource.org/licenses/MIT
77
*/
@@ -24,13 +24,18 @@ function match ($parse) {
2424
}
2525

2626
var matchGetter = $parse(attrs.match);
27+
var caselessGetter = $parse(attrs.matchCaseless);
2728

2829
scope.$watch(getMatchValue, function(){
2930
ctrl.$$parseAndValidate();
3031
});
3132

3233
ctrl.$validators.match = function(){
33-
return ctrl.$viewValue === getMatchValue();
34+
var match = getMatchValue();
35+
if(caselessGetter(scope) && angular.isString(match) && angular.isString(ctrl.$viewValue)){
36+
return ctrl.$viewValue.toLowerCase() === match.toLowerCase();
37+
}
38+
return ctrl.$viewValue === match;
3439
};
3540

3641
function getMatchValue(){
@@ -44,4 +49,4 @@ function match ($parse) {
4449
};
4550
}
4651
match.$inject = ["$parse"];
47-
})(window, window.angular);
52+
})(window, window.angular);

dist/angular-input-match.min.js

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-input-match",
3-
"version": "1.4.1",
3+
"version": "1.5.0",
44
"homepage": "https://github.com/TheSharpieOne/angular-input-match",
55
"description": "Checks if one input matches another",
66
"main": "./dist/angular-input-match.min.js",

src/angular-input-match.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ function match ($parse) {
1717
}
1818

1919
var matchGetter = $parse(attrs.match);
20+
var caselessGetter = $parse(attrs.matchCaseless);
2021

2122
scope.$watch(getMatchValue, function(){
2223
ctrl.$$parseAndValidate();
2324
});
2425

2526
ctrl.$validators.match = function(){
26-
return ctrl.$viewValue === getMatchValue();
27+
var match = getMatchValue();
28+
if(caselessGetter(scope) && angular.isString(match) && angular.isString(ctrl.$viewValue)){
29+
return ctrl.$viewValue.toLowerCase() === match.toLowerCase();
30+
}
31+
return ctrl.$viewValue === match;
2732
};
2833

2934
function getMatchValue(){

test/angular-input-match.spec.js

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ describe('Directives: validation - match', function() {
5959
expect(compiled.hasClass('ng-valid')).to.be.true();
6060
});
6161

62-
6362
it('returns false if $modelValue are not identical', function() {
6463
$scope.confirmation = false;
6564
$scope.original = undefined;
@@ -108,6 +107,80 @@ describe('Directives: validation - match', function() {
108107
$scope.$digest();
109108
expect(form.testConfirm.$modelValue).to.be.undefined();
110109
});
111-
110+
111+
});
112+
113+
describe('caseless validation', function() {
114+
115+
describe('behavior:', function() {
116+
var validTemplate = '<input ng-model="confirmation" match="original" match-caseless="true"></input>';
117+
118+
it('returns true if no model value has been defined', function() {
119+
compiled = $compile(validTemplate)($scope);
120+
expect($scope.confirmation).to.be.undefined();
121+
$scope.$digest();
122+
expect(compiled.hasClass('ng-valid')).to.be.true();
123+
});
124+
125+
126+
it('returns true if $modelValue are identical', function() {
127+
$scope.confirmation = "value";
128+
compiled = $compile(validTemplate)($scope);
129+
$scope.original = "value";
130+
$scope.$digest();
131+
expect(compiled.hasClass('ng-valid')).to.be.true();
132+
});
133+
134+
it('returns true if $modelValue are not identical but match caselessly', function() {
135+
$scope.confirmation = "VALUE";
136+
compiled = $compile(validTemplate)($scope);
137+
$scope.original = "value";
138+
$scope.$digest();
139+
expect(compiled.hasClass('ng-valid')).to.be.true();
140+
});
141+
142+
it('returns false if $modelValue are not identical', function() {
143+
$scope.confirmation = false;
144+
$scope.original = undefined;
145+
compiled = $compile(validTemplate)($scope);
146+
$scope.$digest();
147+
expect(compiled.hasClass('ng-valid')).to.be.false();
148+
});
149+
150+
});
151+
152+
153+
describe('Form level validation', function() {
154+
var form,
155+
element,
156+
inputValue = 'testValue';
157+
158+
beforeEach(function() {
159+
element = angular.element(
160+
'<form name="form">' +
161+
'<input type="text" ng-model="test" name="test"></input>' +
162+
'<input type="text" match="form.test" match-caseless="true" ng-model="testConfirm" name="testConfirm"></input>' +
163+
'</form>'
164+
);
165+
$scope.test = inputValue;
166+
$compile(element)($scope);
167+
$scope.$digest();
168+
form = $scope.form;
169+
});
170+
171+
it('should check if $viewValues are identical', function() {
172+
form.testConfirm.$setViewValue(inputValue);
173+
$scope.$digest();
174+
expect(form.testConfirm.$error.match).to.be.undefined();
175+
});
176+
177+
it('should check if $viewValues are not identical but match caselessly', function() {
178+
form.testConfirm.$setViewValue(inputValue.toUpperCase());
179+
$scope.$digest();
180+
expect(form.testConfirm.$error.match).to.be.undefined();
181+
});
182+
183+
});
184+
112185
});
113186
});

0 commit comments

Comments
 (0)