index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 _Barcode3 from '../Barcode.js'
  19. function _classCallCheck(instance, Constructor) {
  20. if (!(instance instanceof Constructor)) {
  21. throw new TypeError("Cannot call a class as a function");
  22. }
  23. }
  24. function _possibleConstructorReturn(self, call) {
  25. if (!self) {
  26. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  27. }
  28. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  29. }
  30. function _inherits(subClass, superClass) {
  31. if (typeof superClass !== "function" && superClass !== null) {
  32. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  33. }
  34. subClass.prototype = Object.create(superClass && superClass.prototype, {
  35. constructor: {
  36. value: subClass,
  37. enumerable: false,
  38. writable: true,
  39. configurable: true
  40. }
  41. });
  42. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ =
  43. superClass;
  44. } // Encoding documentation
  45. // http://www.gomaro.ch/ftproot/Laetus_PHARMA-CODE.pdf
  46. var pharmacode = function(_Barcode) {
  47. _inherits(pharmacode, _Barcode);
  48. function pharmacode(data, options) {
  49. _classCallCheck(this, pharmacode);
  50. var _this = _possibleConstructorReturn(this, (pharmacode.__proto__ || Object.getPrototypeOf(pharmacode))
  51. .call(this, data, options));
  52. _this.number = parseInt(data, 10);
  53. return _this;
  54. }
  55. _createClass(pharmacode, [{
  56. key: "encode",
  57. value: function encode() {
  58. var z = this.number;
  59. var result = "";
  60. // http://i.imgur.com/RMm4UDJ.png
  61. // (source: http://www.gomaro.ch/ftproot/Laetus_PHARMA-CODE.pdf, page: 34)
  62. while (!isNaN(z) && z != 0) {
  63. if (z % 2 === 0) {
  64. // Even
  65. result = "11100" + result;
  66. z = (z - 2) / 2;
  67. } else {
  68. // Odd
  69. result = "100" + result;
  70. z = (z - 1) / 2;
  71. }
  72. }
  73. // Remove the two last zeroes
  74. result = result.slice(0, -2);
  75. return {
  76. data: result,
  77. text: this.text
  78. };
  79. }
  80. }, {
  81. key: "valid",
  82. value: function valid() {
  83. return this.number >= 3 && this.number <= 131070;
  84. }
  85. }]);
  86. return pharmacode;
  87. }(_Barcode3);
  88. export default pharmacode;