ITF.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 _constants from './constants'
  19. import _Barcode3 from '../Barcode'
  20. function _classCallCheck(instance, Constructor) {
  21. if (!(instance instanceof Constructor)) {
  22. throw new TypeError("Cannot call a class as a function");
  23. }
  24. }
  25. function _possibleConstructorReturn(self, call) {
  26. if (!self) {
  27. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  28. }
  29. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  30. }
  31. function _inherits(subClass, superClass) {
  32. if (typeof superClass !== "function" && superClass !== null) {
  33. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  34. }
  35. subClass.prototype = Object.create(superClass && superClass.prototype, {
  36. constructor: {
  37. value: subClass,
  38. enumerable: false,
  39. writable: true,
  40. configurable: true
  41. }
  42. });
  43. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ =
  44. superClass;
  45. }
  46. var ITF = function(_Barcode) {
  47. _inherits(ITF, _Barcode);
  48. function ITF() {
  49. _classCallCheck(this, ITF);
  50. return _possibleConstructorReturn(this, (ITF.__proto__ || Object.getPrototypeOf(ITF)).apply(this,
  51. arguments));
  52. }
  53. _createClass(ITF, [{
  54. key: 'valid',
  55. value: function valid() {
  56. return this.data.search(/^([0-9]{2})+$/) !== -1;
  57. }
  58. }, {
  59. key: 'encode',
  60. value: function encode() {
  61. var _this2 = this;
  62. // Calculate all the digit pairs
  63. var encoded = this.data.match(/.{2}/g).map(function(pair) {
  64. return _this2.encodePair(pair);
  65. }).join('');
  66. return {
  67. data: _constants.START_BIN + encoded + _constants.END_BIN,
  68. text: this.text
  69. };
  70. }
  71. // Calculate the data of a number pair
  72. }, {
  73. key: 'encodePair',
  74. value: function encodePair(pair) {
  75. var second = _constants.BINARIES[pair[1]];
  76. return _constants.BINARIES[pair[0]].split('').map(function(first, idx) {
  77. return (first === '1' ? '111' : '1') + (second[idx] === '1' ? '000' : '0');
  78. }).join('');
  79. }
  80. }]);
  81. return ITF;
  82. }(_Barcode3);
  83. export default ITF;