uqr 0.1.0 → 0.1.2
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 +19 -3
- package/dist/index.d.cts +132 -0
- package/dist/index.d.mts +132 -0
- package/dist/index.mjs +19 -3
- package/package.json +6 -7
package/dist/index.cjs
CHANGED
|
@@ -10,6 +10,12 @@ var QrCodeDataType = /* @__PURE__ */ ((QrCodeDataType2) => {
|
|
|
10
10
|
return QrCodeDataType2;
|
|
11
11
|
})(QrCodeDataType || {});
|
|
12
12
|
|
|
13
|
+
var __defProp = Object.defineProperty;
|
|
14
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
15
|
+
var __publicField = (obj, key, value) => {
|
|
16
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
17
|
+
return value;
|
|
18
|
+
};
|
|
13
19
|
const LOW = [0, 1];
|
|
14
20
|
const MEDIUM = [1, 0];
|
|
15
21
|
const QUARTILE = [2, 3];
|
|
@@ -62,10 +68,18 @@ class QrCode {
|
|
|
62
68
|
constructor(version, ecc, dataCodewords, msk) {
|
|
63
69
|
this.version = version;
|
|
64
70
|
this.ecc = ecc;
|
|
71
|
+
/* -- Fields -- */
|
|
72
|
+
// The width and height of this QR Code, measured in modules, between
|
|
73
|
+
// 21 and 177 (inclusive). This is equal to version * 4 + 17.
|
|
74
|
+
__publicField(this, "size");
|
|
75
|
+
// The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).
|
|
76
|
+
// Even if a QR Code is created with automatic masking requested (mask = -1),
|
|
77
|
+
// the resulting object still has a mask value between 0 and 7.
|
|
78
|
+
__publicField(this, "mask");
|
|
65
79
|
// The modules of this QR Code (false = light, true = dark).
|
|
66
80
|
// Immutable after constructor finishes. Accessed through getModule().
|
|
67
|
-
this
|
|
68
|
-
this
|
|
81
|
+
__publicField(this, "modules", []);
|
|
82
|
+
__publicField(this, "types", []);
|
|
69
83
|
if (version < MIN_VERSION || version > MAX_VERSION)
|
|
70
84
|
throw new RangeError("Version value out of range");
|
|
71
85
|
if (msk < -1 || msk > 7)
|
|
@@ -595,7 +609,9 @@ function encode(data, options) {
|
|
|
595
609
|
maskPattern = -1,
|
|
596
610
|
border = 1
|
|
597
611
|
} = options || {};
|
|
598
|
-
const segment = typeof data === "string" ? makeSegments(data) : [makeBytes(data)];
|
|
612
|
+
const segment = typeof data === "string" ? makeSegments(data) : Array.isArray(data) ? [makeBytes(data)] : void 0;
|
|
613
|
+
if (!segment)
|
|
614
|
+
throw new Error(`uqr only supports encoding string and binary data, but got: ${typeof data}`);
|
|
599
615
|
const qr = encodeSegments(
|
|
600
616
|
segment,
|
|
601
617
|
EccMap[ecc],
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* String or binary
|
|
3
|
+
*/
|
|
4
|
+
type QrCodeGenerateData = string | Readonly<Array<number>>;
|
|
5
|
+
interface QrCodeGenerateOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Error correction level
|
|
8
|
+
*
|
|
9
|
+
* L - Allows recovery of up to 7% data loss
|
|
10
|
+
* M - Allows recovery of up to 15% data loss
|
|
11
|
+
* Q - Allows recovery of up to 25% data loss
|
|
12
|
+
* H - Allows recovery of up to 30% data loss
|
|
13
|
+
*
|
|
14
|
+
* @default 'L'
|
|
15
|
+
*/
|
|
16
|
+
ecc?: 'L' | 'M' | 'Q' | 'H';
|
|
17
|
+
/**
|
|
18
|
+
* Mask pattern to use
|
|
19
|
+
*
|
|
20
|
+
* @default -1 (auto)
|
|
21
|
+
*/
|
|
22
|
+
maskPattern?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Boost the error correction level to the maximum allowed by the version and size
|
|
25
|
+
*/
|
|
26
|
+
boostEcc?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Minimum version of the QR code (1-40)
|
|
29
|
+
* @default 1
|
|
30
|
+
*/
|
|
31
|
+
minVersion?: number;
|
|
32
|
+
/**
|
|
33
|
+
* Maximum version of the QR code (1-40)
|
|
34
|
+
* @default 40
|
|
35
|
+
*/
|
|
36
|
+
maxVersion?: number;
|
|
37
|
+
/**
|
|
38
|
+
* Border around the QR code
|
|
39
|
+
*
|
|
40
|
+
* @default 1
|
|
41
|
+
*/
|
|
42
|
+
border?: number;
|
|
43
|
+
/**
|
|
44
|
+
* Invert black and white
|
|
45
|
+
*/
|
|
46
|
+
invert?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Callback function to receive the generated QR Code
|
|
49
|
+
*/
|
|
50
|
+
onEncoded?: (qr: QrCodeGenerateResult) => void;
|
|
51
|
+
}
|
|
52
|
+
declare enum QrCodeDataType {
|
|
53
|
+
Border = -1,
|
|
54
|
+
Data = 0,
|
|
55
|
+
Function = 1,
|
|
56
|
+
Position = 2,
|
|
57
|
+
Timing = 3,
|
|
58
|
+
Alignment = 4
|
|
59
|
+
}
|
|
60
|
+
interface QrCodeGenerateResult {
|
|
61
|
+
/**
|
|
62
|
+
* QR Code version
|
|
63
|
+
*/
|
|
64
|
+
version: number;
|
|
65
|
+
/**
|
|
66
|
+
* Width and height of the QR Code array
|
|
67
|
+
*/
|
|
68
|
+
size: number;
|
|
69
|
+
/**
|
|
70
|
+
* Mask pattern used
|
|
71
|
+
*/
|
|
72
|
+
maskPattern: number;
|
|
73
|
+
/**
|
|
74
|
+
* Two dimensional array representing the QR Code
|
|
75
|
+
*
|
|
76
|
+
* `true` for black, `false` for white
|
|
77
|
+
*/
|
|
78
|
+
data: boolean[][];
|
|
79
|
+
/**
|
|
80
|
+
* Data type of each module
|
|
81
|
+
*/
|
|
82
|
+
types: QrCodeDataType[][];
|
|
83
|
+
}
|
|
84
|
+
interface QrCodeGenerateInvertableOptions extends QrCodeGenerateOptions {
|
|
85
|
+
invert?: boolean;
|
|
86
|
+
}
|
|
87
|
+
interface QrCodeGenerateUnicodeOptions extends QrCodeGenerateInvertableOptions {
|
|
88
|
+
whiteChar?: string;
|
|
89
|
+
blackChar?: string;
|
|
90
|
+
}
|
|
91
|
+
interface QrCodeGenerateSvgOptions extends QrCodeGenerateOptions {
|
|
92
|
+
/**
|
|
93
|
+
* Size of each pixel
|
|
94
|
+
*
|
|
95
|
+
* @default 20
|
|
96
|
+
*/
|
|
97
|
+
pixelSize?: number;
|
|
98
|
+
/**
|
|
99
|
+
* Color of the white module
|
|
100
|
+
*
|
|
101
|
+
* @default 'white'
|
|
102
|
+
*/
|
|
103
|
+
whiteColor?: string;
|
|
104
|
+
/**
|
|
105
|
+
* Color of the black module
|
|
106
|
+
*
|
|
107
|
+
* @default 'black'
|
|
108
|
+
*/
|
|
109
|
+
blackColor?: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
declare function encode(data: QrCodeGenerateData, options?: QrCodeGenerateOptions): QrCodeGenerateResult;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Render QR Code with unicode `█`, `░`
|
|
116
|
+
*/
|
|
117
|
+
declare function renderUnicode(data: QrCodeGenerateData, options?: QrCodeGenerateUnicodeOptions): string;
|
|
118
|
+
/**
|
|
119
|
+
* Render QR Code with ANSI color for terminal
|
|
120
|
+
*/
|
|
121
|
+
declare function renderANSI(data: QrCodeGenerateData, options?: QrCodeGenerateOptions): string;
|
|
122
|
+
/**
|
|
123
|
+
* Render QR Code with two rows into one line with unicode `▀`, `▄`, `█`, ` `
|
|
124
|
+
*/
|
|
125
|
+
declare function renderUnicodeCompact(data: QrCodeGenerateData, options?: QrCodeGenerateOptions): string;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Render QR Code with ANSI color for terminal
|
|
129
|
+
*/
|
|
130
|
+
declare function renderSVG(data: QrCodeGenerateData, options?: QrCodeGenerateSvgOptions): string;
|
|
131
|
+
|
|
132
|
+
export { QrCodeDataType, QrCodeGenerateData, QrCodeGenerateInvertableOptions, QrCodeGenerateOptions, QrCodeGenerateResult, QrCodeGenerateSvgOptions, QrCodeGenerateUnicodeOptions, encode, renderANSI, renderSVG, renderUnicode, renderUnicodeCompact };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* String or binary
|
|
3
|
+
*/
|
|
4
|
+
type QrCodeGenerateData = string | Readonly<Array<number>>;
|
|
5
|
+
interface QrCodeGenerateOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Error correction level
|
|
8
|
+
*
|
|
9
|
+
* L - Allows recovery of up to 7% data loss
|
|
10
|
+
* M - Allows recovery of up to 15% data loss
|
|
11
|
+
* Q - Allows recovery of up to 25% data loss
|
|
12
|
+
* H - Allows recovery of up to 30% data loss
|
|
13
|
+
*
|
|
14
|
+
* @default 'L'
|
|
15
|
+
*/
|
|
16
|
+
ecc?: 'L' | 'M' | 'Q' | 'H';
|
|
17
|
+
/**
|
|
18
|
+
* Mask pattern to use
|
|
19
|
+
*
|
|
20
|
+
* @default -1 (auto)
|
|
21
|
+
*/
|
|
22
|
+
maskPattern?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Boost the error correction level to the maximum allowed by the version and size
|
|
25
|
+
*/
|
|
26
|
+
boostEcc?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Minimum version of the QR code (1-40)
|
|
29
|
+
* @default 1
|
|
30
|
+
*/
|
|
31
|
+
minVersion?: number;
|
|
32
|
+
/**
|
|
33
|
+
* Maximum version of the QR code (1-40)
|
|
34
|
+
* @default 40
|
|
35
|
+
*/
|
|
36
|
+
maxVersion?: number;
|
|
37
|
+
/**
|
|
38
|
+
* Border around the QR code
|
|
39
|
+
*
|
|
40
|
+
* @default 1
|
|
41
|
+
*/
|
|
42
|
+
border?: number;
|
|
43
|
+
/**
|
|
44
|
+
* Invert black and white
|
|
45
|
+
*/
|
|
46
|
+
invert?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Callback function to receive the generated QR Code
|
|
49
|
+
*/
|
|
50
|
+
onEncoded?: (qr: QrCodeGenerateResult) => void;
|
|
51
|
+
}
|
|
52
|
+
declare enum QrCodeDataType {
|
|
53
|
+
Border = -1,
|
|
54
|
+
Data = 0,
|
|
55
|
+
Function = 1,
|
|
56
|
+
Position = 2,
|
|
57
|
+
Timing = 3,
|
|
58
|
+
Alignment = 4
|
|
59
|
+
}
|
|
60
|
+
interface QrCodeGenerateResult {
|
|
61
|
+
/**
|
|
62
|
+
* QR Code version
|
|
63
|
+
*/
|
|
64
|
+
version: number;
|
|
65
|
+
/**
|
|
66
|
+
* Width and height of the QR Code array
|
|
67
|
+
*/
|
|
68
|
+
size: number;
|
|
69
|
+
/**
|
|
70
|
+
* Mask pattern used
|
|
71
|
+
*/
|
|
72
|
+
maskPattern: number;
|
|
73
|
+
/**
|
|
74
|
+
* Two dimensional array representing the QR Code
|
|
75
|
+
*
|
|
76
|
+
* `true` for black, `false` for white
|
|
77
|
+
*/
|
|
78
|
+
data: boolean[][];
|
|
79
|
+
/**
|
|
80
|
+
* Data type of each module
|
|
81
|
+
*/
|
|
82
|
+
types: QrCodeDataType[][];
|
|
83
|
+
}
|
|
84
|
+
interface QrCodeGenerateInvertableOptions extends QrCodeGenerateOptions {
|
|
85
|
+
invert?: boolean;
|
|
86
|
+
}
|
|
87
|
+
interface QrCodeGenerateUnicodeOptions extends QrCodeGenerateInvertableOptions {
|
|
88
|
+
whiteChar?: string;
|
|
89
|
+
blackChar?: string;
|
|
90
|
+
}
|
|
91
|
+
interface QrCodeGenerateSvgOptions extends QrCodeGenerateOptions {
|
|
92
|
+
/**
|
|
93
|
+
* Size of each pixel
|
|
94
|
+
*
|
|
95
|
+
* @default 20
|
|
96
|
+
*/
|
|
97
|
+
pixelSize?: number;
|
|
98
|
+
/**
|
|
99
|
+
* Color of the white module
|
|
100
|
+
*
|
|
101
|
+
* @default 'white'
|
|
102
|
+
*/
|
|
103
|
+
whiteColor?: string;
|
|
104
|
+
/**
|
|
105
|
+
* Color of the black module
|
|
106
|
+
*
|
|
107
|
+
* @default 'black'
|
|
108
|
+
*/
|
|
109
|
+
blackColor?: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
declare function encode(data: QrCodeGenerateData, options?: QrCodeGenerateOptions): QrCodeGenerateResult;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Render QR Code with unicode `█`, `░`
|
|
116
|
+
*/
|
|
117
|
+
declare function renderUnicode(data: QrCodeGenerateData, options?: QrCodeGenerateUnicodeOptions): string;
|
|
118
|
+
/**
|
|
119
|
+
* Render QR Code with ANSI color for terminal
|
|
120
|
+
*/
|
|
121
|
+
declare function renderANSI(data: QrCodeGenerateData, options?: QrCodeGenerateOptions): string;
|
|
122
|
+
/**
|
|
123
|
+
* Render QR Code with two rows into one line with unicode `▀`, `▄`, `█`, ` `
|
|
124
|
+
*/
|
|
125
|
+
declare function renderUnicodeCompact(data: QrCodeGenerateData, options?: QrCodeGenerateOptions): string;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Render QR Code with ANSI color for terminal
|
|
129
|
+
*/
|
|
130
|
+
declare function renderSVG(data: QrCodeGenerateData, options?: QrCodeGenerateSvgOptions): string;
|
|
131
|
+
|
|
132
|
+
export { QrCodeDataType, QrCodeGenerateData, QrCodeGenerateInvertableOptions, QrCodeGenerateOptions, QrCodeGenerateResult, QrCodeGenerateSvgOptions, QrCodeGenerateUnicodeOptions, encode, renderANSI, renderSVG, renderUnicode, renderUnicodeCompact };
|
package/dist/index.mjs
CHANGED
|
@@ -8,6 +8,12 @@ var QrCodeDataType = /* @__PURE__ */ ((QrCodeDataType2) => {
|
|
|
8
8
|
return QrCodeDataType2;
|
|
9
9
|
})(QrCodeDataType || {});
|
|
10
10
|
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
+
var __publicField = (obj, key, value) => {
|
|
14
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
15
|
+
return value;
|
|
16
|
+
};
|
|
11
17
|
const LOW = [0, 1];
|
|
12
18
|
const MEDIUM = [1, 0];
|
|
13
19
|
const QUARTILE = [2, 3];
|
|
@@ -60,10 +66,18 @@ class QrCode {
|
|
|
60
66
|
constructor(version, ecc, dataCodewords, msk) {
|
|
61
67
|
this.version = version;
|
|
62
68
|
this.ecc = ecc;
|
|
69
|
+
/* -- Fields -- */
|
|
70
|
+
// The width and height of this QR Code, measured in modules, between
|
|
71
|
+
// 21 and 177 (inclusive). This is equal to version * 4 + 17.
|
|
72
|
+
__publicField(this, "size");
|
|
73
|
+
// The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive).
|
|
74
|
+
// Even if a QR Code is created with automatic masking requested (mask = -1),
|
|
75
|
+
// the resulting object still has a mask value between 0 and 7.
|
|
76
|
+
__publicField(this, "mask");
|
|
63
77
|
// The modules of this QR Code (false = light, true = dark).
|
|
64
78
|
// Immutable after constructor finishes. Accessed through getModule().
|
|
65
|
-
this
|
|
66
|
-
this
|
|
79
|
+
__publicField(this, "modules", []);
|
|
80
|
+
__publicField(this, "types", []);
|
|
67
81
|
if (version < MIN_VERSION || version > MAX_VERSION)
|
|
68
82
|
throw new RangeError("Version value out of range");
|
|
69
83
|
if (msk < -1 || msk > 7)
|
|
@@ -593,7 +607,9 @@ function encode(data, options) {
|
|
|
593
607
|
maskPattern = -1,
|
|
594
608
|
border = 1
|
|
595
609
|
} = options || {};
|
|
596
|
-
const segment = typeof data === "string" ? makeSegments(data) : [makeBytes(data)];
|
|
610
|
+
const segment = typeof data === "string" ? makeSegments(data) : Array.isArray(data) ? [makeBytes(data)] : void 0;
|
|
611
|
+
if (!segment)
|
|
612
|
+
throw new Error(`uqr only supports encoding string and binary data, but got: ${typeof data}`);
|
|
597
613
|
const qr = encodeSegments(
|
|
598
614
|
segment,
|
|
599
615
|
EccMap[ecc],
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "uqr",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.2",
|
|
5
5
|
"packageManager": "pnpm@8.6.12",
|
|
6
6
|
"description": "Generate QR Code universally, in any runtime, to ANSI, Unicode or SVG.",
|
|
7
7
|
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
|
|
@@ -16,7 +16,6 @@
|
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"exports": {
|
|
18
18
|
".": {
|
|
19
|
-
"types": "./dist/index.d.ts",
|
|
20
19
|
"import": "./dist/index.mjs",
|
|
21
20
|
"require": "./dist/index.cjs"
|
|
22
21
|
}
|
|
@@ -48,18 +47,18 @@
|
|
|
48
47
|
"devDependencies": {
|
|
49
48
|
"@antfu/eslint-config": "^0.40.2",
|
|
50
49
|
"@antfu/ni": "^0.21.5",
|
|
51
|
-
"@antfu/utils": "^0.7.
|
|
52
|
-
"@types/node": "^20.
|
|
50
|
+
"@antfu/utils": "^0.7.6",
|
|
51
|
+
"@types/node": "^20.5.0",
|
|
53
52
|
"bumpp": "^9.1.1",
|
|
54
|
-
"eslint": "^8.
|
|
53
|
+
"eslint": "^8.47.0",
|
|
55
54
|
"esno": "^0.17.0",
|
|
56
|
-
"lint-staged": "^
|
|
55
|
+
"lint-staged": "^14.0.0",
|
|
57
56
|
"pnpm": "^8.6.12",
|
|
58
57
|
"rimraf": "^5.0.1",
|
|
59
58
|
"rollup": "^3.28.0",
|
|
60
59
|
"simple-git-hooks": "^2.9.0",
|
|
61
60
|
"typescript": "^5.1.6",
|
|
62
|
-
"unbuild": "^
|
|
61
|
+
"unbuild": "^2.0.0-rc.0",
|
|
63
62
|
"vite": "^4.4.9",
|
|
64
63
|
"vitest": "^0.34.1"
|
|
65
64
|
},
|