AngularJS service exposing the fastest implementation of Luhn algorithm.
Usefult to check check credit card numbers validity or generally to verify card numbers generated via this algorithm.
Closurized, less than 500 bytes.
The luhn
service is a constructor with only a (privileged) method (i.e., check()
).
Using it is very simple.
-
Load AngularJS and this little library
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script> <script src="https://cdn.rawgit.com/leodido/ng-luhn/master/luhn.min.js"></script>
-
Declare your AngularJS application with
leodido.luhn
in the dependencies arrayvar app = angular.module('myModule', ['leodido.luhn']);
-
Inject the service into you AngularJS code and call its
check
methodapp.controller('NumberController', ['luhn', function(luhn) { var isValid = luhn.check('49927398716'); }]);
Do not forget to bootstrap your AngularJS application ...
Install it via bower
.
bower install ng-luhn
Otherwise you can grab *.luhn.js
file/s in the repository root or use rawgit.
- Fastest? Yes, check yourself: jsperf.