QRCodeRaw.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  14. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  15. if (ar || !(i in from)) {
  16. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  17. ar[i] = from[i];
  18. }
  19. }
  20. return to.concat(ar || Array.prototype.slice.call(from));
  21. };
  22. Object.defineProperty(exports, "__esModule", { value: true });
  23. exports.ERROR_CORRECTION_LEVEL_HIGH = exports.ERROR_CORRECTION_LEVEL_QUARTILE = exports.ERROR_CORRECTION_LEVEL_MEDIUM = exports.ERROR_CORRECTION_LEVEL_LOW = void 0;
  24. var qr_1 = require("@akamfoad/qr");
  25. exports.ERROR_CORRECTION_LEVEL_LOW = 'L'; // Allows recovery of up to 7% data loss
  26. exports.ERROR_CORRECTION_LEVEL_MEDIUM = 'M'; // Allows recovery of up to 15% data loss
  27. exports.ERROR_CORRECTION_LEVEL_QUARTILE = 'Q'; // Allows recovery of up to 25% data loss
  28. exports.ERROR_CORRECTION_LEVEL_HIGH = 'H'; // Allows recovery of up to 30% data loss
  29. var DEFAULT_CONSTRUCTOR_PARAMS = {
  30. level: exports.ERROR_CORRECTION_LEVEL_LOW,
  31. padding: 1,
  32. invert: false,
  33. typeNumber: 0,
  34. errorsEnabled: false,
  35. };
  36. var QRCodeRaw = /** @class */ (function () {
  37. function QRCodeRaw(value, options) {
  38. if (options === void 0) { options = {}; }
  39. var params = __assign(__assign({}, DEFAULT_CONSTRUCTOR_PARAMS), options);
  40. this.value = value;
  41. this.level = params.level;
  42. this.typeNumber = params.typeNumber;
  43. this.padding = params.padding;
  44. this.invert = params.invert;
  45. this.errorsEnabled = params.errorsEnabled;
  46. }
  47. QRCodeRaw.prototype.setValue = function (value) {
  48. this.value = value;
  49. this._clearCache();
  50. };
  51. QRCodeRaw.prototype.getDataSize = function () {
  52. var data = this.getData();
  53. return data ? data.length : 0;
  54. };
  55. QRCodeRaw.prototype._clearCache = function () {
  56. this.qrCodeData = null;
  57. };
  58. QRCodeRaw.prototype._getQrCodeData = function (modules) {
  59. var qrCodeData = [];
  60. var padding = this.padding;
  61. var invert = this.invert;
  62. var rowPadding = Array(padding * 2 + modules.length).fill(invert);
  63. var rowsPadding = Array(padding).fill(rowPadding);
  64. var columnPadding = Array(padding).fill(invert);
  65. if (padding) {
  66. qrCodeData.push.apply(qrCodeData, rowsPadding);
  67. }
  68. modules.forEach(function (row) {
  69. var qrCodeRow = [];
  70. qrCodeRow.push.apply(qrCodeRow, __spreadArray(__spreadArray(__spreadArray([], columnPadding, false), row.map(function (isBlack) { return (invert ? !isBlack : isBlack); }), false), columnPadding, false));
  71. qrCodeData.push(qrCodeRow);
  72. });
  73. if (padding) {
  74. qrCodeData.push.apply(qrCodeData, rowsPadding);
  75. }
  76. return qrCodeData;
  77. };
  78. QRCodeRaw.prototype.getData = function () {
  79. if (!this.qrCodeData) {
  80. try {
  81. var qrcode = new qr_1.QRCode(this.typeNumber, qr_1.ErrorCorrectLevel[this.level]);
  82. qrcode.addData(this.value);
  83. qrcode.make();
  84. if (!qrcode.modules) {
  85. return null;
  86. }
  87. this.qrCodeData = this._getQrCodeData(qrcode.modules);
  88. Object.freeze(this.qrCodeData);
  89. }
  90. catch (error) {
  91. if (this.errorsEnabled) {
  92. throw error;
  93. }
  94. return null;
  95. }
  96. }
  97. return this.qrCodeData;
  98. };
  99. return QRCodeRaw;
  100. }());
  101. exports.default = QRCodeRaw;