qr-min 0.0.1 → 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/README.md +6 -1
- package/javascript/qr-min.js +85 -7
- package/package.json +13 -3
- package/test/mocha.js +82 -0
package/README.md
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
# qr-
|
|
1
|
+
# qr-min
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/qr-min)
|
|
4
|
+
[](https://www.npmjs.com/package/qr-min)
|
|
5
|
+
[](https://github.com/jazz-soft/qr-min/actions)
|
|
6
|
+
[](https://coveralls.io/github/jazz-soft/qr-min?branch=main)
|
|
2
7
|
|
|
3
8
|
Minimal implementation to generate a QR code from a UTF-8 string.
|
|
4
9
|
This is all you need for 99.9% of applications.
|
package/javascript/qr-min.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
}
|
|
9
9
|
else {
|
|
10
10
|
if (!global) global = window;
|
|
11
|
-
if (global.QR
|
|
11
|
+
if (global.QR) return;
|
|
12
12
|
global.QR = factory();
|
|
13
13
|
}
|
|
14
14
|
})(this, function() {
|
|
@@ -65,9 +65,17 @@
|
|
|
65
65
|
for (_nxt(p, d.length); r[p[0]][p[1]]; _nxt(p, d.length)) ;
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
for (i = 0; i < 8; i++) {
|
|
69
|
+
b = _dup(d);
|
|
70
|
+
_format(b, 0, i);
|
|
71
|
+
_mask(b, r, i);
|
|
72
|
+
j = _score(b);
|
|
73
|
+
if (!i || j < k) {
|
|
74
|
+
k = j;
|
|
75
|
+
bbb = b;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return bbb;
|
|
71
79
|
};
|
|
72
80
|
function _nxt(p, sz) {
|
|
73
81
|
var d = (p[1] > 5 ? p[1] - 1 : p[1]) % 4;
|
|
@@ -135,6 +143,17 @@
|
|
|
135
143
|
}
|
|
136
144
|
}
|
|
137
145
|
d[sz - 8][8] = 1;
|
|
146
|
+
if (v > 6) {
|
|
147
|
+
m = v;
|
|
148
|
+
for (k = 0; k < 12; k++) m = (m << 1) ^ ((m >>> 11) * 0x1F25);
|
|
149
|
+
m = v << 12 | m;
|
|
150
|
+
for (k = 0; k < 18; k++) {
|
|
151
|
+
i = sz - 11 + k % 3;
|
|
152
|
+
j = Math.floor(k / 3);
|
|
153
|
+
d[i][j] = (m & (1 << k)) >>> k;
|
|
154
|
+
d[j][i] = (m & (1 << k)) >>> k;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
138
157
|
}
|
|
139
158
|
function _res(v) {
|
|
140
159
|
var i, j, k, m, sz = v * 4 + 17, e = _eyes(v);
|
|
@@ -160,6 +179,10 @@
|
|
|
160
179
|
r[e[k] + i - 2][e[m] + j - 2] = 1;
|
|
161
180
|
}
|
|
162
181
|
}
|
|
182
|
+
if (v > 6) for (i = 0; i < 6; i++) for (j = 0; j < 3; j++) {
|
|
183
|
+
r[i][sz - j - 9] = 1;
|
|
184
|
+
r[sz - j - 9][i] = 1;
|
|
185
|
+
}
|
|
163
186
|
return r;
|
|
164
187
|
}
|
|
165
188
|
function _format(d, c, m) {
|
|
@@ -180,15 +203,70 @@
|
|
|
180
203
|
for (i = 0; i < d.length; i++) for (j = 0; j < d.length; j++) {
|
|
181
204
|
if (r[i][j]) continue;
|
|
182
205
|
k = !m ? k = (i + j) % 2 :
|
|
183
|
-
m == 1 ?
|
|
184
|
-
m == 2 ?
|
|
206
|
+
m == 1 ? i % 2 :
|
|
207
|
+
m == 2 ? j % 3 :
|
|
185
208
|
m == 3 ? (i + j) % 3 :
|
|
186
|
-
m == 4 ? (Math.floor(
|
|
209
|
+
m == 4 ? (Math.floor(j / 3) + Math.floor(i / 2)) % 2 :
|
|
187
210
|
m == 5 ? i * j % 2 + i * j % 3 :
|
|
188
211
|
m == 6 ? (i * j % 2 + i * j % 3) % 2 : ((i + j) % 2 + i * j % 3) % 2;
|
|
189
212
|
if (!k) d[i][j] = 1 - d[i][j];
|
|
190
213
|
}
|
|
191
214
|
}
|
|
215
|
+
function _score(d) {
|
|
216
|
+
var i, j, k, n, x;
|
|
217
|
+
k = 0;
|
|
218
|
+
for (i = 0; i < d.length; i++) for (j = 0; j < d.length; j++) k += d[i][j];
|
|
219
|
+
n = d.length * d.length;
|
|
220
|
+
x = Math.floor(Math.abs(n - 2 * k) / n * 10) * 10;
|
|
221
|
+
for (i = 0; i < d.length - 1; i++) for (j = 0; j < d.length - 1; j++) {
|
|
222
|
+
if (d[i][j] == d[i][j + 1] && d[i][j] == d[i + 1][j] && d[i][j] == d[i + 1][j + 1]) x += 3;
|
|
223
|
+
}
|
|
224
|
+
for (i = 0; i < d.length; i++) {
|
|
225
|
+
k = d[i][0];
|
|
226
|
+
n = 1;
|
|
227
|
+
for (j = 1; j < d.length; j++) {
|
|
228
|
+
if (k != d[i][j]) {
|
|
229
|
+
k = d[i][j];
|
|
230
|
+
n = 1;
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
n++;
|
|
234
|
+
if (n == 5) x += 3;
|
|
235
|
+
if (n > 5) x++;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
for (i = 0; i < d.length; i++) {
|
|
240
|
+
k = d[0][i];
|
|
241
|
+
n = 1;
|
|
242
|
+
for (j = 1; j < d.length; j++) {
|
|
243
|
+
if (k != d[j][i]) {
|
|
244
|
+
k = d[j][i];
|
|
245
|
+
n = 1;
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
n++;
|
|
249
|
+
if (n == 5) x += 3;
|
|
250
|
+
if (n > 5) x++;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
for (i = 0; i < d.length; i++) for (j = 0; j < d.length - 6; j++) {
|
|
255
|
+
if (d[i][j] && !d[i][j + 1] && d[i][j + 2] && d[i][j + 3] && d[i][j + 4] && !d[i][j + 5] && d[i][j + 6]) {
|
|
256
|
+
for (k = 0; k < 4; k++) if (j - 1 - k >= 0 && d[i][j - 1 - k]) break;
|
|
257
|
+
if (k == 4 && (j + 7 >= d.length || !d[i][j + 7])) x += 40;
|
|
258
|
+
for (k = 0; k < 4; k++) if (j + 7 + k < d.length && d[i][j + 7 + k]) break;
|
|
259
|
+
if (k == 4 && (!j || !d[i][j - 1])) x += 40;
|
|
260
|
+
}
|
|
261
|
+
if (d[j][i] && !d[j + 1][i] && d[j + 2][i] && d[j + 3][i] && d[j + 4][i] && !d[j + 5][i] && d[j + 6][i]) {
|
|
262
|
+
for (k = 0; k < 4; k++) if (j - 1 - k >= 0 && d[j - 1 - k][i]) break;
|
|
263
|
+
if (k == 4 && (j + 7 >= d.length || !d[j + 7][i])) x += 40;
|
|
264
|
+
for (k = 0; k < 4; k++) if (j + 7 + k < d.length && d[j + 7 + k][i]) break;
|
|
265
|
+
if (k == 4 && (!j || !d[j - 1][i])) x += 40;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return x;
|
|
269
|
+
}
|
|
192
270
|
function _dots(v) {
|
|
193
271
|
var i, d = [], sz = v * 4 + 17;
|
|
194
272
|
for (i = 0; i < sz; i++) d.push(Array(sz).fill(0));
|
package/package.json
CHANGED
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qr-min",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "Minimal implementation to generate a QR code from a UTF-8 string",
|
|
5
5
|
"main": "javascript/qr-min.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "
|
|
7
|
+
"test": "nyc mocha",
|
|
8
|
+
"lint": "eslint javascript test",
|
|
9
|
+
"coverage": "nyc report --reporter=lcov"
|
|
8
10
|
},
|
|
9
11
|
"keywords": [
|
|
10
12
|
"qr",
|
|
11
13
|
"qr-code"
|
|
12
14
|
],
|
|
13
15
|
"author": "jazz-soft (https://jazz-soft.net/)",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"eslint": "^9.39.2",
|
|
18
|
+
"grunt": "^1.6.1",
|
|
19
|
+
"grunt-contrib-jshint": "^3.2.0",
|
|
20
|
+
"grunt-contrib-uglify": "^5.2.2",
|
|
21
|
+
"mocha": "^11.7.5",
|
|
22
|
+
"nyc": "^17.1.0"
|
|
23
|
+
},
|
|
14
24
|
"repository": {
|
|
15
25
|
"type": "git",
|
|
16
26
|
"url": "git+https://github.com/jazz-soft/qr-min.git"
|
package/test/mocha.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
var assert = require('assert');
|
|
2
|
+
var QR = require('..');
|
|
3
|
+
|
|
4
|
+
function text(qr) {
|
|
5
|
+
var i, j, s = '';
|
|
6
|
+
for (i = 0; i < qr.length; i += 2) {
|
|
7
|
+
for (j = 0; j < qr.length; j++) s += i + 1 < qr.length && qr[i + 1][j] ? qr[i][j] ? '█' : '▄' : qr[i][j] ? '▀' : ' ';
|
|
8
|
+
s += '\n';
|
|
9
|
+
}
|
|
10
|
+
return s;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
describe('qr', function() {
|
|
14
|
+
it('short', function() {
|
|
15
|
+
assert.equal(text(QR('a')),`
|
|
16
|
+
█▀▀▀▀▀█ █▄█▀ █▀▀▀▀▀█
|
|
17
|
+
█ ███ █ ▀█ █▀ █ ███ █
|
|
18
|
+
█ ▀▀▀ █ ▀ █ █ ▀▀▀ █
|
|
19
|
+
▀▀▀▀▀▀▀ █▄▀▄█ ▀▀▀▀▀▀▀
|
|
20
|
+
▀▀█ ██▀█▀▀█▀ ▀█ █▄
|
|
21
|
+
▄▄ ▄▀▄▀▀▀██ ▀ ▄ ▀ ▄ ▀
|
|
22
|
+
▀▀ ▀▀▀▀▄▄ ▄▀▄▀▄▀▄▀▄█
|
|
23
|
+
█▀▀▀▀▀█ █▄▄█▄█▀█▄█▀▀▀
|
|
24
|
+
█ ███ █ ▀ █▀ ▀█▀ ▀█▄▀
|
|
25
|
+
█ ▀▀▀ █ ██ ▀ ▄ ▀ ▄▄▀
|
|
26
|
+
▀▀▀▀▀▀▀ ▀▀ ▀ ▀ ▀ ▀▀▀
|
|
27
|
+
`.substring(1));
|
|
28
|
+
});
|
|
29
|
+
it('medium', function() {
|
|
30
|
+
assert.equal(text(QR('https://github.com/jazz-soft/qr-min')),`
|
|
31
|
+
█▀▀▀▀▀█ ▄▀ ▄▀ ▀ █ █▀▀▀▀▀█
|
|
32
|
+
█ ███ █ ▀▄ ▀▄▄ ▀ ▀▀█ █ ███ █
|
|
33
|
+
█ ▀▀▀ █ ▀▄▄█▀▄▄▀███▄ █ ▀▀▀ █
|
|
34
|
+
▀▀▀▀▀▀▀ █ ▀▄▀ ▀ █▄█▄▀ ▀▀▀▀▀▀▀
|
|
35
|
+
█▀▀▄██▀▀█▄ ▄█▄ ▀██▄ █▀█ ▄▀ ▄
|
|
36
|
+
█ ▄ █▄▀ ▄▄█▀█▄█▄ ██ ▀▀▀▄█ ▀█▀
|
|
37
|
+
█▄ ▄ █▀▀ ██ █▀█▀ ▀███ █ ▀█
|
|
38
|
+
▀█▀█▄▀▀█▄ ▀▄ ▀ ▄▄█▄ ▄▄█ ▀█ █▀
|
|
39
|
+
█▄▀█▀ ▄ ▄█▄ ▀██ ▀▀▄█▀ █▄▀█
|
|
40
|
+
▀▄▄▄ █▀ ▄▀ ▀█▄█▄▄█ ▄▄█▄██▄ █▀
|
|
41
|
+
▀ ▀ ▀ ▄▀██ █▀▀█▄██▀▀▀█ ▄▄▄
|
|
42
|
+
█▀▀▀▀▀█ █ █▄ ▀ ▀▄ ▀█ ▀ ██ ▀█
|
|
43
|
+
█ ███ █ ▀▀█▄█▄ ▄▀▀ █▀▀██ ▄▄█
|
|
44
|
+
█ ▀▀▀ █ █▄ ▀█▄█▄ █▄█▄▄ ▀█▀ ▄▀
|
|
45
|
+
▀▀▀▀▀▀▀ ▀▀▀▀ ▀ ▀ ▀▀▀ ▀▀ ▀▀
|
|
46
|
+
`.substring(1));
|
|
47
|
+
}); it('long', function() {
|
|
48
|
+
assert.equal(text(QR('😀😃😄😁😆😅😂🤣😊😇🙂🙃😉😌😍🥰😘😗😙😚😋😛😝😜🤪🤨🧐🤓😎🤩🥳😏😒😞😀😃😄😁😆😅😂🤣😊😇🙂🙃😉😌😍🥰😘😗😙😚😋😛😝😜🤪🤨🧐🤓😎🤩🥳😏😒😞')),`
|
|
49
|
+
█▀▀▀▀▀█ ▄▄▄▀█ ▀ █▄ ▀█▄█ █▄▀▄██▀▄▄█▄ ▄█ ▄▀▀▄ ▄ ██ █▀▀▀▀▀█
|
|
50
|
+
█ ███ █ ▀██ ██▄▄█ █ ██▀▄▄██ ▄▀█▄█▄█▄ █▀▄▀▄▄▀████▀█ █ ███ █
|
|
51
|
+
█ ▀▀▀ █ ▀██ ██▄ ▀▄ ▄█ ▀▄█▀ █▀▀▀██ ▀▀▀▄ ▄ ▄▀ ▀▄██▀ █ ▀▀▀ █
|
|
52
|
+
▀▀▀▀▀▀▀ █▄▀ █▄▀ ▀▄█ █ █▄█ ▀▄█ ▀ █▄█ ▀▄▀ █▄▀▄▀ █ ▀▄▀ ▀ ▀▀▀▀▀▀▀
|
|
53
|
+
▀▀█▄▀█▀▀█▄▄█▀▄ ███ █▀▀▄▄█ ▀▄████▀██ ▄▄█▀ ▄▄ █▄▄ ▄█████▄ █
|
|
54
|
+
▄ ▀█▀ ▀█▄▄███▄ ▀▄▄ █▀▄▀▄██ █▄██▄▄▀▀ █▀█ █ ██ █▀█▀▄▄█ █▄▄
|
|
55
|
+
█▀█ █▀▀█ ▄█▄▄▀▄██ ██ ▄▄▀▄ ▄█▀▀ ▀▄ ▀█▀▄ ▄█▄ ▄██▄█▄██▀▄▄ ██ ▀
|
|
56
|
+
▄█▄▄▀█▀▀█▄█▀▄█▄▄▀ ▀▄█ ▀▀▄▄▀▀▄█▀ ▄█ ▀██ ▀█ █▄▀▄ ██▄▀█ █▄█ ▄
|
|
57
|
+
▄ ▀▀▀▄▀▀ █ ▀██ █▀▄██ ▀█▀ ▄▄▀█▀ ▄█▄ ▄ ▄█▄▀ ▄█▄ ▄█▀▄▄█▀▄▄▀▀▀
|
|
58
|
+
▀▀▀▄ ▀▀▄ ▀▄▄▀▄ ▄▄▀▀▄▄▀█▀▀▄███▄▄ █▀▄▄█ ▀ ██ ▀█ ▄█ ▀▀█ ▀ █ ▄
|
|
59
|
+
▀█▄ ██▀▀ ▄ ▀▄█▀▄▄ ██▀▄██ ▄███ ██▀ ▄▄█ ▀▄█ ▄█▄▄ ▀█▄▄█▄▄ ▀▄▀
|
|
60
|
+
███▄▄█▀▄█▄█▀▀▄█▀▄ ▀▄▀▄▀▄██▄██▄█▀▄█▀▀ ▀█▄▀█▀▀▀▄▄ █▀▄▀▄█▀ █▄▄
|
|
61
|
+
▄▀█▀█▀▀█▄ ▀▄ ▀██ ▀ █ ███ ▀▄██ ▀ ▄▄ █▄▄ ▄█ ▄███▄█▄██▀▄ ▄██ █
|
|
62
|
+
▀█▄▀██▀▀█▀▀▀▀ ▀▄▀▀█ ▄▀▄▀▄▄▀█▀▀█ ▀█ ▄▀ ▄ ▀█▀▄ █▄███▄▀█ ▀▄▄██
|
|
63
|
+
█▀▀ █▀▀▀█▀ █▀█ ▀▄▀▄██ ██ ██▀▀▀█▄██▄ ▄ ▄█▄█▄▀▄▄ ▄█▀█▀▀▀█▄▀▄█
|
|
64
|
+
▀█▄▀█ ▀ █▀▀▄▀▀▀██▄▀▀▄█▄▄▀▀▄▄█ ▀ █▀█▀ ▄▀▀ ▄▄▄▀▀▄▀▀▀▀▀█ ▀ █ ▄ ▄
|
|
65
|
+
▄██▀█▀▀▀████▄▀██▀ ███ █ █▀▀██▀█▄ ▄▄█▄ ▄█▄ █▄ ▄▀▄███▀▀██ ▀
|
|
66
|
+
▄█ ██▀▀▄ █ ▀██▀▀▄▄▀▄▀ ▄▄ █▀█▄██▀█▀ ▀█▄▀ █▄▄▀▀▀▄█ █▀ █ ▄█▄
|
|
67
|
+
▀▀▄▀█▀▀█▀ ████▄▄▀▄ ██▄ █ █ ▄▀ ▄▄ █▄█▄▄ ▄▄█▀▄▄██▀▀▄▄▄██ █
|
|
68
|
+
▄▀█▀▀ ██▄▀ ▀█▄▀█▄▄█▀▄▀▄ ▀▀▄▄ ▀▄▄▀ █ ▀ █▀ ▄ ▄▀███ ▀▄██▄
|
|
69
|
+
▄▀ █▄ ▀█ ▄▄ ▀▀▄▀▄█ █▀ ██ ▀█ ▄ ▀▄▄█▀▄██ ▄█▄▄ ▀▄ ▄██▀▄ ██▄█
|
|
70
|
+
▄ ▀█▀▄▀▀ ▄█▀▀ ▄▀█▀▄ ▄▀▀▄█▀▄█▄█▀█▀▀█ ▀ ▀█ ▄▄ █ ▄▄ ▀▄▀▄ ▀
|
|
71
|
+
▀▄▀▀▄▀▀█ ▄█▀ ███ ██ ▀▄ ▄█▀▄███▄ ▀█▀▄ ▄█▄ ▄█ ▄ █▀▀▄▀███ ▀
|
|
72
|
+
▄ ▀ ▀ █ ▄█▄▀ ▄█▀▄▄▀ ▀▀█▄ ▀█▄▀ ███ ▀▀█ █▀█ ██▀ █ █ ▄▀▄█▄
|
|
73
|
+
█▄█ ▄ ▀ ▄▄▄██ █ █ ▀▄ ▀█▀▄ █ ▄▄█▄ █▄▀▄ █ ▄▄█▄ ▄██▀▀▄██▀█ █
|
|
74
|
+
▄▄█▀█▀▀█▄ █▀▀▀▄ ▄█▀ ▀▀▄█▀ ▄▀▄ ▀▀▄█▀ █▀▀ ▀▀▄▀ ▄█ █▀██ ▀█▄▀
|
|
75
|
+
▀▀▀▀ ▀ ▄█▄▄█▄▀▀▄▄▄██ ██▀▄▀█▀▀▀██▀▀ ▄█ ▀ ▄▄ █ █▄▀█▀▀▀█▄ ▀▀
|
|
76
|
+
█▀▀▀▀▀█ █▄▀▀▄▄▄▄▄ ▀▄█▄ ▄▀█▄▄█ ▀ █▀█▄ ▄ ▀ ▀ ██ ▀▀▄ ▀█ ▀ █ █▄
|
|
77
|
+
█ ███ █ ▀█▄▀ ▀▄█ ▀ █▄ ▀▄█ █▀█▀▀██▄▄ █▄ ▄█▀ ▄█ ▄▀█▀██████ ▄
|
|
78
|
+
█ ▀▀▀ █ ██▄▄▀ ▀▄█▀▄ ▄▀ ▄▀▄ ████▀██▄ ▀▄ █▄ ▀▀▀█▀ █ █▄▄█ ▄▄█▄▀
|
|
79
|
+
▀▀▀▀▀▀▀ ▀ ▀▀▀ ▀ ▀ ▀ ▀ ▀▀ ▀▀ ▀ ▀ ▀ ▀▀ ▀▀ ▀▀▀▀
|
|
80
|
+
`.substring(1));
|
|
81
|
+
});
|
|
82
|
+
});
|