diff --git a/README.adoc b/README.adoc index 9b1dac6..c8548f7 100644 --- a/README.adoc +++ b/README.adoc @@ -31,3 +31,11 @@ Add `mm.iban` as a dependency of your AngularJS module. ---- To use this directive the `ngModel` directive must also be used because this directive depends on it. + +=== filter +[source,html] +---- +IBAN: {{ string | iban:separator }} +---- + +The filter converts a given IBAN to it's human-friendly represantation. The separator defaults to a single space. diff --git a/src/ng-iban.coffee b/src/ng-iban.coffee index 9fb18bc..4e0975e 100644 --- a/src/ng-iban.coffee +++ b/src/ng-iban.coffee @@ -43,3 +43,18 @@ angular else modelValue return + + .filter 'iban', -> + (string, separator) -> + parseIban = (value) -> + if value? then value.toUpperCase().replace /\s/g, '' else undefined + + isValidIban = (value) -> + iban = parseIban(value) + IBAN.isValid iban + + valid = isValidIban string + if valid + IBAN.printFormat string, separator + else + string diff --git a/test/spec/ng-iban.coffee b/test/spec/ng-iban-directive.coffee similarity index 100% rename from test/spec/ng-iban.coffee rename to test/spec/ng-iban-directive.coffee diff --git a/test/spec/ng-iban-filter.coffee b/test/spec/ng-iban-filter.coffee new file mode 100644 index 0000000..fe22f20 --- /dev/null +++ b/test/spec/ng-iban-filter.coffee @@ -0,0 +1,22 @@ +'use strict' + +describe 'Filter: iban', -> + + # load the filter's module + beforeEach module 'mm.iban' + + filter = undefined + + beforeEach inject ($filter) -> + filter = $filter 'iban' + + it 'electronic IBAN should become print IBAN', -> + expect(filter('NL91ABNA0417164300')).toEqual 'NL91 ABNA 0417 1643 00' + + it 'invalid IBAN should stay untouched', -> + expect(filter('INVALIDIBAN')).toEqual 'INVALIDIBAN' + expect(filter('NL90ABNA0417164300')).toEqual 'NL90ABNA0417164300' + + it 'mind custom separator', -> + expect(filter('NL91ABNA0417164300', '-')).toEqual 'NL91-ABNA-0417-1643-00' + expect(filter('NL91ABNA0417164300', '')).toEqual 'NL91ABNA0417164300'