-
Notifications
You must be signed in to change notification settings - Fork 11
/
mod.ts
36 lines (31 loc) · 960 Bytes
/
mod.ts
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
// @deno-types="./qrcode.d.ts"
import { qrcode as _qrcode } from "./qrcode.js";
export async function qrcode(
text: string,
options?: Options,
): Promise<string> {
options = options || {};
const typeNumber: TypeNumber = options.typeNumber || 4;
const errorCorrectLevel: ErrorCorrectionLevel = options.errorCorrectLevel ||
"M";
const size: number = options.size || 500;
let qr;
try {
qr = _qrcode(typeNumber, errorCorrectLevel || "M");
qr.addData(text);
qr.make();
} catch (e) {
if (typeNumber >= 40) {
throw new Error("Text too long to encode");
} else {
return qrcode(text, {
size: size,
errorCorrectLevel: errorCorrectLevel,
typeNumber: (typeNumber + 1 as TypeNumber),
});
}
}
const cellsize = Math.floor(size / qr.getModuleCount());
const margin = Math.floor((size - qr.getModuleCount() * cellsize) / 2);
return qr.createDataURL(cellsize, margin, size);
}