somdigi-qr-generator 1.0.0

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/index.js ADDED
@@ -0,0 +1,7 @@
1
+ const { generateQR } = require("./src/core");
2
+ const presets = require("./src/presets")
3
+
4
+ module.exports = {
5
+ generateQR,
6
+ presets
7
+ };
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "somdigi-qr-generator",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "keywords": [],
9
+ "author": "",
10
+ "license": "ISC",
11
+ "description": "",
12
+ "dependencies": {
13
+ "canvas": "^3.2.0",
14
+ "jsdom": "^26.1.0",
15
+ "qr-code-styling": "^1.9.2"
16
+ }
17
+ }
package/src/core.js ADDED
@@ -0,0 +1,61 @@
1
+ const QRCodeStyling = require("qr-code-styling");
2
+ const { JSDOM } = require("jsdom");
3
+ const canvas = require("canvas");
4
+
5
+ let isSetup = false;
6
+
7
+ function setupNodeCanvas() {
8
+ if (isSetup) return;
9
+
10
+ const { window } = new JSDOM();
11
+ global.window = window;
12
+ global.document = window.document;
13
+ global.Image = canvas.Image;
14
+ global.HTMLCanvasElement = canvas.Canvas;
15
+
16
+ isSetup = true;
17
+ }
18
+
19
+ async function generateQR(options) {
20
+ if (!options || !options.data) {
21
+ throw new Error("options.data is required");
22
+ }
23
+
24
+ setupNodeCanvas();
25
+
26
+ const qr = new QRCodeStyling({
27
+ width: options.size || 300,
28
+ height: options.size || 300,
29
+ data: options.data,
30
+ margin: options.margin ?? 10,
31
+
32
+ dotsOptions: {
33
+ type: "dots",
34
+ gradient: options.gradient
35
+ },
36
+
37
+ cornersSquareOptions: {
38
+ type: "extra-rounded",
39
+ gradient: options.gradient
40
+ },
41
+
42
+ cornersDotOptions: {
43
+ type: "dot",
44
+ color: "#000000"
45
+ },
46
+
47
+ backgroundOptions: {
48
+ color: options.background || "#ffffff"
49
+ },
50
+
51
+ qrOptions: {
52
+ errorCorrectionLevel: "H"
53
+ }
54
+ });
55
+
56
+ return qr.getRawData(options.format || "png");
57
+ }
58
+
59
+ module.exports = {
60
+ generateQR
61
+ };
package/src/presets.js ADDED
@@ -0,0 +1,48 @@
1
+ const purpleBlue = {
2
+ type: "linear",
3
+ rotation: Math.PI / 4,
4
+ colorStops: [
5
+ { offset: 0, color: "#7F00FF" },
6
+ { offset: 1, color: "#00C6FF" }
7
+ ]
8
+ };
9
+
10
+ const sunset = {
11
+ type: "linear",
12
+ rotation: Math.PI / 2,
13
+ colorStops: [
14
+ { offset: 0, color: "#FF512F" },
15
+ { offset: 1, color: "#F09819" }
16
+ ]
17
+ };
18
+ const black = {
19
+ type: "linear",
20
+ rotation: Math.PI / 2,
21
+ colorStops: [
22
+ { offset: 0, color: "#000000" },
23
+ { offset: 1, color: "#000000" }
24
+ ]
25
+ };
26
+
27
+ const generatePresets = ({
28
+ type = "linear",
29
+ rotation = Math.PI / 4,
30
+ colorPrimary = "#000000",
31
+ colorsSecondary = "#000000"
32
+ }) => {
33
+ return{
34
+ type,
35
+ rotation,
36
+ colorStops: [
37
+ { offset: 0, color: colorPrimary },
38
+ { offset: 1, color: colorsSecondary }
39
+ ]
40
+ }
41
+ }
42
+
43
+ module.exports = {
44
+ purpleBlue,
45
+ sunset,
46
+ black,
47
+ generatePresets
48
+ };