-
Notifications
You must be signed in to change notification settings - Fork 68
/
qrcode.js
45 lines (40 loc) · 1.33 KB
/
qrcode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const RE_CODE_LENGTH_OVERFLOW = /code length overflow/i;
const vendorCode = (() => {
// @include "vendor/qrcode.js"
// @include "vendor/qrcode_UTF8.js"
return qrcode; // eslint-disable-line no-undef
})();
const minCode = (text, level, minVersion = 1) => {
minVersion = Math.max(1, minVersion);
for (let version = minVersion; version <= 40; version += 1) {
try {
const qr = vendorCode(version, level);
qr.addData(text);
qr.make();
const moduleCount = qr.getModuleCount();
const isDark = (row, col) => {
return row >= 0 &&
row < moduleCount &&
col >= 0 &&
col < moduleCount &&
qr.isDark(row, col);
};
return {text, level, version, moduleCount, isDark};
} catch (err) {
if (!RE_CODE_LENGTH_OVERFLOW.test(err.message)) {
throw err;
}
}
}
return null;
};
const quietCode = (text = '', level = 'L', minVersion = 1, quiet = 0) => {
const qr = minCode(text, level, minVersion);
if (qr) {
const prevIsDark = qr.isDark;
qr.moduleCount += 2 * quiet;
qr.isDark = (row, col) => prevIsDark(row - quiet, col - quiet);
}
return qr;
};
module.exports = quietCode;