index.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.pharmacode = undefined;
  6. 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; }; }();
  7. var _Barcode2 = require("../Barcode.js");
  8. var _Barcode3 = _interopRequireDefault(_Barcode2);
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  11. 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; }
  12. 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; } // Encoding documentation
  13. // http://www.gomaro.ch/ftproot/Laetus_PHARMA-CODE.pdf
  14. var pharmacode = function (_Barcode) {
  15. _inherits(pharmacode, _Barcode);
  16. function pharmacode(data, options) {
  17. _classCallCheck(this, pharmacode);
  18. var _this = _possibleConstructorReturn(this, (pharmacode.__proto__ || Object.getPrototypeOf(pharmacode)).call(this, data, options));
  19. _this.number = parseInt(data, 10);
  20. return _this;
  21. }
  22. _createClass(pharmacode, [{
  23. key: "encode",
  24. value: function encode() {
  25. var z = this.number;
  26. var result = "";
  27. // http://i.imgur.com/RMm4UDJ.png
  28. // (source: http://www.gomaro.ch/ftproot/Laetus_PHARMA-CODE.pdf, page: 34)
  29. while (!isNaN(z) && z != 0) {
  30. if (z % 2 === 0) {
  31. // Even
  32. result = "11100" + result;
  33. z = (z - 2) / 2;
  34. } else {
  35. // Odd
  36. result = "100" + result;
  37. z = (z - 1) / 2;
  38. }
  39. }
  40. // Remove the two last zeroes
  41. result = result.slice(0, -2);
  42. return {
  43. data: result,
  44. text: this.text
  45. };
  46. }
  47. }, {
  48. key: "valid",
  49. value: function valid() {
  50. return this.number >= 3 && this.number <= 131070;
  51. }
  52. }]);
  53. return pharmacode;
  54. }(_Barcode3.default);
  55. exports.pharmacode = pharmacode;