ITF14.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. var _createClass = function() {
  3. function defineProperties(target, props) {
  4. for (var i = 0; i < props.length; i++) {
  5. var descriptor = props[i];
  6. descriptor.enumerable = descriptor.enumerable || false;
  7. descriptor.configurable = true;
  8. if ("value" in descriptor) descriptor.writable = true;
  9. Object.defineProperty(target, descriptor.key, descriptor);
  10. }
  11. }
  12. return function(Constructor, protoProps, staticProps) {
  13. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  14. if (staticProps) defineProperties(Constructor, staticProps);
  15. return Constructor;
  16. };
  17. }();
  18. import _ITF3 from './ITF'
  19. function _interopRequireDefault(obj) {
  20. return obj && obj.__esModule ? obj : {
  21. default: obj
  22. };
  23. }
  24. function _classCallCheck(instance, Constructor) {
  25. if (!(instance instanceof Constructor)) {
  26. throw new TypeError("Cannot call a class as a function");
  27. }
  28. }
  29. function _possibleConstructorReturn(self, call) {
  30. if (!self) {
  31. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  32. }
  33. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  34. }
  35. function _inherits(subClass, superClass) {
  36. if (typeof superClass !== "function" && superClass !== null) {
  37. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  38. }
  39. subClass.prototype = Object.create(superClass && superClass.prototype, {
  40. constructor: {
  41. value: subClass,
  42. enumerable: false,
  43. writable: true,
  44. configurable: true
  45. }
  46. });
  47. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ =
  48. superClass;
  49. }
  50. // Calculate the checksum digit
  51. var checksum = function checksum(data) {
  52. var res = data.substr(0, 13).split('').map(function(num) {
  53. return parseInt(num, 10);
  54. }).reduce(function(sum, n, idx) {
  55. return sum + n * (3 - idx % 2 * 2);
  56. }, 0);
  57. return Math.ceil(res / 10) * 10 - res;
  58. };
  59. var ITF14 = function(_ITF) {
  60. _inherits(ITF14, _ITF);
  61. function ITF14(data, options) {
  62. _classCallCheck(this, ITF14);
  63. // Add checksum if it does not exist
  64. if (data.search(/^[0-9]{13}$/) !== -1) {
  65. data += checksum(data);
  66. }
  67. return _possibleConstructorReturn(this, (ITF14.__proto__ || Object.getPrototypeOf(ITF14)).call(this, data,
  68. options));
  69. }
  70. _createClass(ITF14, [{
  71. key: 'valid',
  72. value: function valid() {
  73. return this.data.search(/^[0-9]{14}$/) !== -1 && +this.data[13] === checksum(this.data);
  74. }
  75. }]);
  76. return ITF14;
  77. }(_ITF3);
  78. export default ITF14;