Skip to content

Commit

Permalink
Merge branch 'release/0.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
mmjmanders committed Aug 21, 2014
2 parents c18c735 + 1bdc8cb commit 5c02ef8
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 48 deletions.
3 changes: 2 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,14 @@ module.exports = function (grunt) {
});

grunt.registerTask('test', [
'clean',
'clean:tmp',
'coffee',
'connect:test',
'karma'
]);

grunt.registerTask('default', [
'clean:dist',
'test',
'ngAnnotate',
'uglify'
Expand Down
6 changes: 3 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-iban",
"version": "0.1.0",
"version": "0.1.1",
"authors": [
"Mark Manders <[email protected]>"
],
Expand All @@ -22,9 +22,9 @@
"Gruntfile.js"
],
"dependencies": {
"angular": "~1.2.0"
"angular": ">=1.0.0"
},
"devDependencies": {
"angular-mocks": "~1.2.0"
"angular-mocks": ">=1.0.0"
}
}
57 changes: 35 additions & 22 deletions dist/ng-iban.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,43 @@ angular.module('mm.iban', ['ng']).directive('ngIban', function() {
restrict: 'A',
require: 'ngModel',
link: function(scope, elem, attrs, ctrl) {
var A, Z;
A = 'A'.charCodeAt(0);
Z = 'Z'.charCodeAt(0);
return scope.$watch(attrs.ngModel, function() {
var block, iban, remainder;
ctrl.$setValidity('iban', (!ctrl.$error.hasOwnProperty('required') || !!!ctrl.$error.required) && (!ctrl.$modelValue || ctrl.$modelValue === ''));
if (ctrl.$modelValue && ctrl.$modelValue !== '') {
iban = ctrl.$modelValue.toUpperCase().replace(/\s/g, '');
iban = iban.substr(4) + iban.substr(0, 4);
remainder = iban.split('').map(function(n) {
var code;
code = n.charCodeAt(0);
if (code >= A && code <= Z) {
return code - A + 10;
} else {
return n;
}
}).join('');
while (remainder.length > 2) {
block = remainder.slice(0, 9);
remainder = __modulo(parseInt(block, 10), 97) + remainder.slice(block.length);
var isValidIban;
isValidIban = function(value) {
var A, Z, block, iban, remainder;
A = 'A'.charCodeAt(0);
Z = 'Z'.charCodeAt(0);
iban = value.toUpperCase().replace(/\s/g, '');
iban = iban.substr(4) + iban.substr(0, 4);
remainder = iban.split('').map(function(n) {
var code;
code = n.charCodeAt(0);
if (code >= A && code <= Z) {
return code - A + 10;
} else {
return n;
}
return ctrl.$setValidity('iban', __modulo(parseInt(remainder, 10), 97) === 1);
}).join('');
while (remainder.length > 2) {
block = remainder.slice(0, 9);
remainder = __modulo(parseInt(block, 10), 97) + remainder.slice(block.length);
}
return __modulo(parseInt(remainder, 10), 97) === 1;
};
ctrl.$parsers.unshift(function(value) {
var valid;
valid = value != null ? isValidIban(value) : true;
ctrl.$setValidity('iban', valid);
if (valid) {
return value;
} else {
return void 0;
}
});
return ctrl.$formatters.unshift(function(value) {
var valid;
valid = value != null ? isValidIban(value) : true;
ctrl.$setValidity('iban', valid);
return value;
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion dist/ng-iban.min.js

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-iban",
"version": "0.1.0",
"version": "0.1.1",
"description": "IBAN directive for AngularJS",
"main": "./dist/ng-iban.js",
"scripts": {
Expand Down
38 changes: 23 additions & 15 deletions src/ng-iban.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@ angular
restrict: 'A'
require: 'ngModel'
link: (scope, elem, attrs, ctrl) ->
A = 'A'.charCodeAt 0
Z = 'Z'.charCodeAt 0
scope.$watch attrs.ngModel, ->
ctrl.$setValidity 'iban', (not ctrl.$error.hasOwnProperty('required') or not !!ctrl.$error.required) and (not ctrl.$modelValue or ctrl.$modelValue is '')
isValidIban = (value) ->
A = 'A'.charCodeAt 0
Z = 'Z'.charCodeAt 0

if ctrl.$modelValue and ctrl.$modelValue isnt ''
iban = ctrl.$modelValue.toUpperCase().replace /\s/g, ''
iban = iban.substr(4) + iban.substr 0, 4
iban = value.toUpperCase().replace /\s/g, ''
iban = iban.substr(4) + iban.substr 0, 4

remainder = iban.split('').map (n) ->
code = n.charCodeAt 0
if code >= A and code <= Z then code - A + 10 else n
.join ''
remainder = iban.split('').map (n) ->
code = n.charCodeAt 0
if code >= A and code <= Z then code - A + 10 else n
.join ''

while remainder.length > 2
block = remainder.slice 0, 9
remainder = parseInt(block, 10) %% 97 + remainder.slice block.length
while remainder.length > 2
block = remainder.slice 0, 9
remainder = parseInt(block, 10) %% 97 + remainder.slice block.length

ctrl.$setValidity 'iban', parseInt(remainder, 10) %% 97 is 1
parseInt(remainder, 10) %% 97 is 1

ctrl.$parsers.unshift (value) ->
valid = if value? then isValidIban value else true
ctrl.$setValidity 'iban', valid
if valid then value else undefined

ctrl.$formatters.unshift (value) ->
valid = if value? then isValidIban value else true
ctrl.$setValidity 'iban', valid
value
10 changes: 5 additions & 5 deletions test/spec/ng-iban.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ describe 'Directive: iban', ->
form = scope.form

it 'optional should pass with empty IBAN', ->
form.optional.$setViewValue ''
form.optional.$setViewValue undefined
scope.$digest()
expect(scope.optional).toEqual ''
expect(scope.optional).toEqual undefined
expect(form.optional.$valid).toBe true

it 'optional should pass with valid IBAN', ->
Expand All @@ -31,11 +31,11 @@ describe 'Directive: iban', ->
it 'optional should fail with invalid IBAN', ->
form.optional.$setViewValue 'NL87 INGB 0002 4455 88'
scope.$digest()
expect(scope.optional).toEqual 'NL87 INGB 0002 4455 88'
expect(scope.optional).toEqual undefined
expect(form.optional.$valid).toBe false

it 'required should fail with empty IBAN', ->
form.iban.$setViewValue ''
form.iban.$setViewValue undefined
scope.$digest()
expect(scope.iban).toEqual undefined
expect(form.iban.$valid).toBe false
Expand All @@ -49,5 +49,5 @@ describe 'Directive: iban', ->
it 'required should fail with invalid IBAN', ->
form.iban.$setViewValue 'NL87 INGB 0002 4455 88'
scope.$digest()
expect(scope.iban).toEqual 'NL87 INGB 0002 4455 88'
expect(scope.iban).toEqual undefined
expect(form.iban.$valid).toBe false

0 comments on commit 5c02ef8

Please sign in to comment.