ITF14.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  6. var _ITF2 = require('./ITF');
  7. var _ITF3 = _interopRequireDefault(_ITF2);
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  10. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  11. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  12. // Calculate the checksum digit
  13. var checksum = function checksum(data) {
  14. var res = data.substr(0, 13).split('').map(function (num) {
  15. return parseInt(num, 10);
  16. }).reduce(function (sum, n, idx) {
  17. return sum + n * (3 - idx % 2 * 2);
  18. }, 0);
  19. return Math.ceil(res / 10) * 10 - res;
  20. };
  21. var ITF14 = function (_ITF) {
  22. _inherits(ITF14, _ITF);
  23. function ITF14(data, options) {
  24. _classCallCheck(this, ITF14);
  25. // Add checksum if it does not exist
  26. if (data.search(/^[0-9]{13}$/) !== -1) {
  27. data += checksum(data);
  28. }
  29. return _possibleConstructorReturn(this, (ITF14.__proto__ || Object.getPrototypeOf(ITF14)).call(this, data, options));
  30. }
  31. _createClass(ITF14, [{
  32. key: 'valid',
  33. value: function valid() {
  34. return this.data.search(/^[0-9]{14}$/) !== -1 && +this.data[13] === checksum(this.data);
  35. }
  36. }]);
  37. return ITF14;
  38. }(_ITF3.default);
  39. exports.default = ITF14;