uqr 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -59,9 +59,9 @@ class QrCode {
59
59
  // error correction level, data codeword bytes, and mask number.
60
60
  // This is a low-level API that most users should not use directly.
61
61
  // A mid-level API is the encodeSegments() function.
62
- constructor(version, errorCorrectionLevel, dataCodewords, msk) {
62
+ constructor(version, ecc, dataCodewords, msk) {
63
63
  this.version = version;
64
- this.errorCorrectionLevel = errorCorrectionLevel;
64
+ this.ecc = ecc;
65
65
  // The modules of this QR Code (false = light, true = dark).
66
66
  // Immutable after constructor finishes. Accessed through getModule().
67
67
  this.modules = [];
@@ -92,7 +92,6 @@ class QrCode {
92
92
  this.applyMask(i);
93
93
  }
94
94
  }
95
- assert(msk >= 0 && msk <= 7);
96
95
  this.mask = msk;
97
96
  this.applyMask(msk);
98
97
  this.drawFormatBits(msk);
@@ -128,12 +127,11 @@ class QrCode {
128
127
  // Draws two copies of the format bits (with its own error correction code)
129
128
  // based on the given mask and this object's error correction level field.
130
129
  drawFormatBits(mask) {
131
- const data = this.errorCorrectionLevel[1] << 3 | mask;
130
+ const data = this.ecc[1] << 3 | mask;
132
131
  let rem = data;
133
132
  for (let i = 0; i < 10; i++)
134
133
  rem = rem << 1 ^ (rem >>> 9) * 1335;
135
134
  const bits = (data << 10 | rem) ^ 21522;
136
- assert(bits >>> 15 === 0);
137
135
  for (let i = 0; i <= 5; i++)
138
136
  this.setFunctionModule(8, i, getBit(bits, i));
139
137
  this.setFunctionModule(8, 7, getBit(bits, 6));
@@ -156,7 +154,6 @@ class QrCode {
156
154
  for (let i = 0; i < 12; i++)
157
155
  rem = rem << 1 ^ (rem >>> 11) * 7973;
158
156
  const bits = this.version << 12 | rem;
159
- assert(bits >>> 18 === 0);
160
157
  for (let i = 0; i < 18; i++) {
161
158
  const color = getBit(bits, i);
162
159
  const a = this.size - 11 + i % 3;
@@ -203,7 +200,7 @@ class QrCode {
203
200
  // codewords appended to it, based on this object's version and error correction level.
204
201
  addEccAndInterleave(data) {
205
202
  const ver = this.version;
206
- const ecl = this.errorCorrectionLevel;
203
+ const ecl = this.ecc;
207
204
  if (data.length !== getNumDataCodewords(ver, ecl))
208
205
  throw new RangeError("Invalid argument");
209
206
  const numBlocks = NUM_ERROR_CORRECTION_BLOCKS[ecl[0]][ver];
@@ -228,7 +225,6 @@ class QrCode {
228
225
  result.push(block[i]);
229
226
  });
230
227
  }
231
- assert(result.length === rawCodewords);
232
228
  return result;
233
229
  }
234
230
  // Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
@@ -252,7 +248,6 @@ class QrCode {
252
248
  }
253
249
  }
254
250
  }
255
- assert(i === data.length * 8);
256
251
  }
257
252
  // XORs the codeword modules in this QR Code with the given mask pattern.
258
253
  // The function modules must be marked and the codeword bits must be drawn
@@ -356,9 +351,7 @@ class QrCode {
356
351
  dark = row.reduce((sum, color) => sum + (color ? 1 : 0), dark);
357
352
  const total = this.size * this.size;
358
353
  const k = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1;
359
- assert(k >= 0 && k <= 9);
360
354
  result += k * PENALTY_N4;
361
- assert(result >= 0 && result <= 2568888);
362
355
  return result;
363
356
  }
364
357
  /* -- Private helper functions -- */
@@ -381,7 +374,6 @@ class QrCode {
381
374
  // returns either 0, 1, or 2. A helper function for getPenaltyScore().
382
375
  finderPenaltyCountPatterns(runHistory) {
383
376
  const n = runHistory[1];
384
- assert(n <= this.size * 3);
385
377
  const core = n > 0 && runHistory[2] === n && runHistory[3] === n * 3 && runHistory[4] === n && runHistory[5] === n;
386
378
  return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0) + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
387
379
  }
@@ -412,10 +404,6 @@ function appendBits(val, len, bb) {
412
404
  function getBit(x, i) {
413
405
  return (x >>> i & 1) !== 0;
414
406
  }
415
- function assert(cond) {
416
- if (!cond)
417
- throw new Error("Assertion error");
418
- }
419
407
  class QrSegment {
420
408
  // Creates a new QR Code segment with the given attributes and data.
421
409
  // The character count (numChars) must agree with the mode and the bit buffer length,
@@ -520,7 +508,6 @@ function getNumRawDataModules(ver) {
520
508
  if (ver >= 7)
521
509
  result -= 36;
522
510
  }
523
- assert(result >= 208 && result <= 29648);
524
511
  return result;
525
512
  }
526
513
  function getNumDataCodewords(ver, ecl) {
@@ -561,7 +548,6 @@ function reedSolomonMultiply(x, y) {
561
548
  z = z << 1 ^ (z >>> 7) * 285;
562
549
  z ^= (y >>> i & 1) * x;
563
550
  }
564
- assert(z >>> 8 === 0);
565
551
  return z;
566
552
  }
567
553
  function encodeSegments(segs, ecl, minVersion = 1, maxVersion = 40, mask = -1, boostEcl = true) {
@@ -590,17 +576,12 @@ function encodeSegments(segs, ecl, minVersion = 1, maxVersion = 40, mask = -1, b
590
576
  for (const b of seg.getData())
591
577
  bb.push(b);
592
578
  }
593
- assert(bb.length === dataUsedBits);
594
579
  const dataCapacityBits = getNumDataCodewords(version, ecl) * 8;
595
- assert(bb.length <= dataCapacityBits);
596
580
  appendBits(0, Math.min(4, dataCapacityBits - bb.length), bb);
597
581
  appendBits(0, (8 - bb.length % 8) % 8, bb);
598
- assert(bb.length % 8 === 0);
599
582
  for (let padByte = 236; bb.length < dataCapacityBits; padByte ^= 236 ^ 17)
600
583
  appendBits(padByte, 8, bb);
601
- const dataCodewords = [];
602
- while (dataCodewords.length * 8 < bb.length)
603
- dataCodewords.push(0);
584
+ const dataCodewords = Array.from({ length: Math.ceil(bb.length / 8) }, () => 0);
604
585
  bb.forEach((b, i) => dataCodewords[i >>> 3] |= b << 7 - (i & 7));
605
586
  return new QrCode(version, ecl, dataCodewords, mask);
606
587
  }
@@ -625,6 +606,7 @@ function encode(data, options) {
625
606
  );
626
607
  const result = addBorder({
627
608
  version: qr.version,
609
+ maskPattern: qr.mask,
628
610
  size: qr.size,
629
611
  data: qr.modules,
630
612
  types: qr.types
package/dist/index.d.ts CHANGED
@@ -62,6 +62,10 @@ interface QrCodeGenerateResult {
62
62
  * Width and height of the QR Code array
63
63
  */
64
64
  size: number;
65
+ /**
66
+ * Mask pattern used
67
+ */
68
+ maskPattern: number;
65
69
  /**
66
70
  * Two dimensional array representing the QR Code
67
71
  *
package/dist/index.mjs CHANGED
@@ -57,9 +57,9 @@ class QrCode {
57
57
  // error correction level, data codeword bytes, and mask number.
58
58
  // This is a low-level API that most users should not use directly.
59
59
  // A mid-level API is the encodeSegments() function.
60
- constructor(version, errorCorrectionLevel, dataCodewords, msk) {
60
+ constructor(version, ecc, dataCodewords, msk) {
61
61
  this.version = version;
62
- this.errorCorrectionLevel = errorCorrectionLevel;
62
+ this.ecc = ecc;
63
63
  // The modules of this QR Code (false = light, true = dark).
64
64
  // Immutable after constructor finishes. Accessed through getModule().
65
65
  this.modules = [];
@@ -90,7 +90,6 @@ class QrCode {
90
90
  this.applyMask(i);
91
91
  }
92
92
  }
93
- assert(msk >= 0 && msk <= 7);
94
93
  this.mask = msk;
95
94
  this.applyMask(msk);
96
95
  this.drawFormatBits(msk);
@@ -126,12 +125,11 @@ class QrCode {
126
125
  // Draws two copies of the format bits (with its own error correction code)
127
126
  // based on the given mask and this object's error correction level field.
128
127
  drawFormatBits(mask) {
129
- const data = this.errorCorrectionLevel[1] << 3 | mask;
128
+ const data = this.ecc[1] << 3 | mask;
130
129
  let rem = data;
131
130
  for (let i = 0; i < 10; i++)
132
131
  rem = rem << 1 ^ (rem >>> 9) * 1335;
133
132
  const bits = (data << 10 | rem) ^ 21522;
134
- assert(bits >>> 15 === 0);
135
133
  for (let i = 0; i <= 5; i++)
136
134
  this.setFunctionModule(8, i, getBit(bits, i));
137
135
  this.setFunctionModule(8, 7, getBit(bits, 6));
@@ -154,7 +152,6 @@ class QrCode {
154
152
  for (let i = 0; i < 12; i++)
155
153
  rem = rem << 1 ^ (rem >>> 11) * 7973;
156
154
  const bits = this.version << 12 | rem;
157
- assert(bits >>> 18 === 0);
158
155
  for (let i = 0; i < 18; i++) {
159
156
  const color = getBit(bits, i);
160
157
  const a = this.size - 11 + i % 3;
@@ -201,7 +198,7 @@ class QrCode {
201
198
  // codewords appended to it, based on this object's version and error correction level.
202
199
  addEccAndInterleave(data) {
203
200
  const ver = this.version;
204
- const ecl = this.errorCorrectionLevel;
201
+ const ecl = this.ecc;
205
202
  if (data.length !== getNumDataCodewords(ver, ecl))
206
203
  throw new RangeError("Invalid argument");
207
204
  const numBlocks = NUM_ERROR_CORRECTION_BLOCKS[ecl[0]][ver];
@@ -226,7 +223,6 @@ class QrCode {
226
223
  result.push(block[i]);
227
224
  });
228
225
  }
229
- assert(result.length === rawCodewords);
230
226
  return result;
231
227
  }
232
228
  // Draws the given sequence of 8-bit codewords (data and error correction) onto the entire
@@ -250,7 +246,6 @@ class QrCode {
250
246
  }
251
247
  }
252
248
  }
253
- assert(i === data.length * 8);
254
249
  }
255
250
  // XORs the codeword modules in this QR Code with the given mask pattern.
256
251
  // The function modules must be marked and the codeword bits must be drawn
@@ -354,9 +349,7 @@ class QrCode {
354
349
  dark = row.reduce((sum, color) => sum + (color ? 1 : 0), dark);
355
350
  const total = this.size * this.size;
356
351
  const k = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1;
357
- assert(k >= 0 && k <= 9);
358
352
  result += k * PENALTY_N4;
359
- assert(result >= 0 && result <= 2568888);
360
353
  return result;
361
354
  }
362
355
  /* -- Private helper functions -- */
@@ -379,7 +372,6 @@ class QrCode {
379
372
  // returns either 0, 1, or 2. A helper function for getPenaltyScore().
380
373
  finderPenaltyCountPatterns(runHistory) {
381
374
  const n = runHistory[1];
382
- assert(n <= this.size * 3);
383
375
  const core = n > 0 && runHistory[2] === n && runHistory[3] === n * 3 && runHistory[4] === n && runHistory[5] === n;
384
376
  return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0) + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
385
377
  }
@@ -410,10 +402,6 @@ function appendBits(val, len, bb) {
410
402
  function getBit(x, i) {
411
403
  return (x >>> i & 1) !== 0;
412
404
  }
413
- function assert(cond) {
414
- if (!cond)
415
- throw new Error("Assertion error");
416
- }
417
405
  class QrSegment {
418
406
  // Creates a new QR Code segment with the given attributes and data.
419
407
  // The character count (numChars) must agree with the mode and the bit buffer length,
@@ -518,7 +506,6 @@ function getNumRawDataModules(ver) {
518
506
  if (ver >= 7)
519
507
  result -= 36;
520
508
  }
521
- assert(result >= 208 && result <= 29648);
522
509
  return result;
523
510
  }
524
511
  function getNumDataCodewords(ver, ecl) {
@@ -559,7 +546,6 @@ function reedSolomonMultiply(x, y) {
559
546
  z = z << 1 ^ (z >>> 7) * 285;
560
547
  z ^= (y >>> i & 1) * x;
561
548
  }
562
- assert(z >>> 8 === 0);
563
549
  return z;
564
550
  }
565
551
  function encodeSegments(segs, ecl, minVersion = 1, maxVersion = 40, mask = -1, boostEcl = true) {
@@ -588,17 +574,12 @@ function encodeSegments(segs, ecl, minVersion = 1, maxVersion = 40, mask = -1, b
588
574
  for (const b of seg.getData())
589
575
  bb.push(b);
590
576
  }
591
- assert(bb.length === dataUsedBits);
592
577
  const dataCapacityBits = getNumDataCodewords(version, ecl) * 8;
593
- assert(bb.length <= dataCapacityBits);
594
578
  appendBits(0, Math.min(4, dataCapacityBits - bb.length), bb);
595
579
  appendBits(0, (8 - bb.length % 8) % 8, bb);
596
- assert(bb.length % 8 === 0);
597
580
  for (let padByte = 236; bb.length < dataCapacityBits; padByte ^= 236 ^ 17)
598
581
  appendBits(padByte, 8, bb);
599
- const dataCodewords = [];
600
- while (dataCodewords.length * 8 < bb.length)
601
- dataCodewords.push(0);
582
+ const dataCodewords = Array.from({ length: Math.ceil(bb.length / 8) }, () => 0);
602
583
  bb.forEach((b, i) => dataCodewords[i >>> 3] |= b << 7 - (i & 7));
603
584
  return new QrCode(version, ecl, dataCodewords, mask);
604
585
  }
@@ -623,6 +604,7 @@ function encode(data, options) {
623
604
  );
624
605
  const result = addBorder({
625
606
  version: qr.version,
607
+ maskPattern: qr.mask,
626
608
  size: qr.size,
627
609
  data: qr.modules,
628
610
  types: qr.types
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "uqr",
3
3
  "type": "module",
4
- "version": "0.0.2",
4
+ "version": "0.0.3",
5
5
  "packageManager": "pnpm@8.6.11",
6
6
  "description": "Generate QR Code universally, in any runtime, to ANSI, Unicode or SVG.",
7
7
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
@@ -56,6 +56,7 @@
56
56
  "lint-staged": "^13.2.3",
57
57
  "pnpm": "^8.6.11",
58
58
  "rimraf": "^5.0.1",
59
+ "rollup": "^3.27.2",
59
60
  "simple-git-hooks": "^2.9.0",
60
61
  "typescript": "^5.1.6",
61
62
  "unbuild": "^1.2.1",