Posted By

Pjotor on 05/03/16


Tagged

generator validator ABN VIN


Versions (?)

VIN and ABN validator & generator


 / Published in: JavaScript
 

URL: http://pjotor.com

VIN and ABN validator and generator for testing.

  1. var abn_weights = [10,1,3,5,7,9,11,13,15,17,19];
  2. var vin_weights = '8765432X098765432';
  3. var vin_map = '0123456789X';
  4. var vin_chars = '0123456789.ABCDEFGH..JKLMN.P.R..STUVWXYZ';
  5.  
  6. var transliterate = c => vin_chars.indexOf(c) % 10;
  7. var vin_chk = (n,i) => transliterate(n) * vin_map.indexOf(vin_weights[i]);
  8. var ran = (len, max) => Array(len).join().split(',').map(_ => ~~( Math.random() * (max||10) ));
  9.  
  10. //Validate ABN
  11. String.prototype.isAbn = function () {
  12. return this.length !== 11 ? !1 :
  13. !(this.split('')
  14. .map((n,i) => (i?n:n-1) * abn_weights[i])
  15. .reduce((a,b) => a+b) % 89)
  16. }
  17.  
  18. //Fake ABN
  19. var fake_abn = (abn) => (
  20. abn = ran(9),
  21. 99 - '00'.split('')
  22. .concat(abn)
  23. .map((n,i) => n * abn_weights[i])
  24. .reduce((a,b) => a+b) % 89 + abn.join('')
  25. );
  26.  
  27. //Validate VIN
  28. String.prototype.isVin = function () {
  29. return this.length !== 17 ? !1 :
  30. vin_map[this.split('')
  31. .map(vin_chk)
  32. .reduce((a,b) => a+b) % 11] == this[8]
  33. }
  34.  
  35. //Fake VIN
  36. var fake_vin = (vin,c) => (
  37. c = vin_chars.split('').filter(c => c!=='.'),
  38. vin = ran(17, c.length).map(n => c[n]),
  39. vin[8] = vin_map[vin.map(vin_chk).reduce((a,b) => a+b) % 11],
  40. vin.join('')
  41. );

Report this snippet  

You need to login to post a comment.