isahaq-barcode 1.9.0 → 2.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/CHANGELOG.md +107 -0
- package/LICENSE +21 -0
- package/README.md +317 -553
- package/bin/generate.js +171 -134
- package/browser.js +163 -170
- package/index.d.ts +497 -0
- package/index.js +119 -51
- package/package.json +69 -41
- package/src/builders/QrCodeBuilder.js +328 -165
- package/src/encoders/BarcodeEncoder.js +260 -0
- package/src/renderers/HTMLRenderer.js +36 -169
- package/src/renderers/PDFRenderer.js +108 -130
- package/src/renderers/PNGRenderer.js +7 -83
- package/src/renderers/RenderFormats.js +89 -38
- package/src/renderers/SVGRenderer.js +8 -182
- package/src/services/BarcodeService.js +96 -36
- package/src/types/BarcodeTypes.js +535 -144
- package/src/validators/Validator.js +140 -122
- package/.eslintrc.js +0 -29
- package/.prettierignore +0 -11
- package/.prettierrc +0 -11
- package/eslint.config.js +0 -67
- package/examples/basic-usage.js +0 -58
- package/examples/batch-example.json +0 -56
- package/examples/express-server.js +0 -163
- package/jest.config.js +0 -13
- package/tests/BarcodeGenerator.test.js +0 -187
- package/tests/BarcodeService.test.js +0 -76
- package/tests/QrCodeBuilder.test.js +0 -130
- package/tests/setup.js +0 -59
- package/webpack.config.js +0 -26
package/index.js
CHANGED
|
@@ -1,50 +1,65 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Isahaq Barcode Generator
|
|
3
|
-
* A universal barcode generator
|
|
3
|
+
* A universal barcode generator supporting 40+ symbologies and PNG/SVG/HTML/PDF output.
|
|
4
|
+
*
|
|
5
|
+
* Bundlers pick up ./browser.js through the "browser" field in package.json;
|
|
6
|
+
* the check below is a backstop for environments that load this file directly.
|
|
4
7
|
*/
|
|
5
8
|
|
|
6
|
-
// Detect environment
|
|
7
9
|
const isBrowser =
|
|
8
10
|
typeof window !== 'undefined' &&
|
|
9
11
|
typeof document !== 'undefined' &&
|
|
10
12
|
typeof process === 'undefined';
|
|
11
13
|
|
|
12
14
|
if (isBrowser) {
|
|
13
|
-
// Browser environment - use browser-compatible version
|
|
14
15
|
module.exports = require('./browser');
|
|
15
16
|
} else {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
17
|
+
const BarcodeService = require('./src/services/BarcodeService');
|
|
18
|
+
const QrCodeBuilder = require('./src/builders/QrCodeBuilder');
|
|
19
|
+
const { BarcodeTypes } = require('./src/types/BarcodeTypes');
|
|
20
|
+
const { RenderFormats } = require('./src/renderers/RenderFormats');
|
|
21
|
+
const { BarcodeEncoder } = require('./src/encoders/BarcodeEncoder');
|
|
22
|
+
const { Validator } = require('./src/validators/Validator');
|
|
23
|
+
|
|
24
|
+
const WATERMARK_POSITIONS = [
|
|
25
|
+
'top-left',
|
|
26
|
+
'top-center',
|
|
27
|
+
'top-right',
|
|
28
|
+
'left-center',
|
|
29
|
+
'center',
|
|
30
|
+
'right-center',
|
|
31
|
+
'bottom-left',
|
|
32
|
+
'bottom-center',
|
|
33
|
+
'bottom-right',
|
|
34
|
+
];
|
|
31
35
|
|
|
32
|
-
// Main service instance
|
|
33
36
|
const barcodeService = new BarcodeService();
|
|
34
37
|
|
|
35
38
|
/**
|
|
36
39
|
* Main Barcode Generator Class
|
|
37
40
|
*/
|
|
38
41
|
class BarcodeGenerator {
|
|
42
|
+
/**
|
|
43
|
+
* Generate a barcode in any supported format
|
|
44
|
+
* @param {string} data - The data to encode
|
|
45
|
+
* @param {string} type - The barcode type
|
|
46
|
+
* @param {string} format - The output format (png, svg, html, pdf)
|
|
47
|
+
* @param {Object} options - Generation options
|
|
48
|
+
* @returns {Promise<Buffer|string>} Generated barcode
|
|
49
|
+
*/
|
|
50
|
+
static generate(data, type = 'code128', format = 'png', options = {}) {
|
|
51
|
+
return barcodeService.generate(data, type, format, options);
|
|
52
|
+
}
|
|
53
|
+
|
|
39
54
|
/**
|
|
40
55
|
* Generate a barcode in PNG format
|
|
41
56
|
* @param {string} data - The data to encode
|
|
42
57
|
* @param {string} type - The barcode type
|
|
43
58
|
* @param {Object} options - Generation options
|
|
44
|
-
* @returns {Buffer} PNG buffer
|
|
59
|
+
* @returns {Promise<Buffer>} PNG buffer
|
|
45
60
|
*/
|
|
46
61
|
static png(data, type = 'code128', options = {}) {
|
|
47
|
-
return barcodeService.
|
|
62
|
+
return barcodeService.png(data, type, options);
|
|
48
63
|
}
|
|
49
64
|
|
|
50
65
|
/**
|
|
@@ -52,21 +67,43 @@ if (isBrowser) {
|
|
|
52
67
|
* @param {string} data - The data to encode
|
|
53
68
|
* @param {string} type - The barcode type
|
|
54
69
|
* @param {Object} options - Generation options
|
|
55
|
-
* @returns {string} SVG
|
|
70
|
+
* @returns {Promise<string>} SVG markup
|
|
56
71
|
*/
|
|
57
72
|
static svg(data, type = 'code128', options = {}) {
|
|
58
|
-
return barcodeService.
|
|
73
|
+
return barcodeService.svg(data, type, options);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Generate a barcode in SVG format synchronously
|
|
78
|
+
* @param {string} data - The data to encode
|
|
79
|
+
* @param {string} type - The barcode type
|
|
80
|
+
* @param {Object} options - Generation options
|
|
81
|
+
* @returns {string} SVG markup
|
|
82
|
+
*/
|
|
83
|
+
static svgSync(data, type = 'code128', options = {}) {
|
|
84
|
+
return barcodeService.svgSync(data, type, options);
|
|
59
85
|
}
|
|
60
86
|
|
|
61
87
|
/**
|
|
62
|
-
* Generate a barcode
|
|
88
|
+
* Generate a barcode as an HTML fragment
|
|
63
89
|
* @param {string} data - The data to encode
|
|
64
90
|
* @param {string} type - The barcode type
|
|
65
91
|
* @param {Object} options - Generation options
|
|
66
|
-
* @returns {string} HTML
|
|
92
|
+
* @returns {Promise<string>} HTML markup
|
|
67
93
|
*/
|
|
68
94
|
static html(data, type = 'code128', options = {}) {
|
|
69
|
-
return barcodeService.
|
|
95
|
+
return barcodeService.html(data, type, options);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Generate a barcode as an HTML fragment synchronously
|
|
100
|
+
* @param {string} data - The data to encode
|
|
101
|
+
* @param {string} type - The barcode type
|
|
102
|
+
* @param {Object} options - Generation options
|
|
103
|
+
* @returns {string} HTML markup
|
|
104
|
+
*/
|
|
105
|
+
static htmlSync(data, type = 'code128', options = {}) {
|
|
106
|
+
return barcodeService.htmlSync(data, type, options);
|
|
70
107
|
}
|
|
71
108
|
|
|
72
109
|
/**
|
|
@@ -76,12 +113,12 @@ if (isBrowser) {
|
|
|
76
113
|
* @param {Object} options - Generation options
|
|
77
114
|
* @returns {Promise<Buffer>} PDF buffer
|
|
78
115
|
*/
|
|
79
|
-
static
|
|
80
|
-
return barcodeService.
|
|
116
|
+
static pdf(data, type = 'code128', options = {}) {
|
|
117
|
+
return barcodeService.pdf(data, type, options);
|
|
81
118
|
}
|
|
82
119
|
|
|
83
120
|
/**
|
|
84
|
-
*
|
|
121
|
+
* Create a QR code builder for logo, label and watermark support
|
|
85
122
|
* @param {string} data - The data to encode
|
|
86
123
|
* @param {Object} options - Generation options
|
|
87
124
|
* @returns {QrCodeBuilder} QR code builder instance
|
|
@@ -90,6 +127,15 @@ if (isBrowser) {
|
|
|
90
127
|
return new QrCodeBuilder({ data, ...options });
|
|
91
128
|
}
|
|
92
129
|
|
|
130
|
+
/**
|
|
131
|
+
* Create a QR code builder from an options object
|
|
132
|
+
* @param {Object} options - QR code options
|
|
133
|
+
* @returns {QrCodeBuilder} QR code builder instance
|
|
134
|
+
*/
|
|
135
|
+
static modernQr(options = {}) {
|
|
136
|
+
return new QrCodeBuilder(options);
|
|
137
|
+
}
|
|
138
|
+
|
|
93
139
|
/**
|
|
94
140
|
* Validate barcode data
|
|
95
141
|
* @param {string} data - The data to validate
|
|
@@ -102,51 +148,58 @@ if (isBrowser) {
|
|
|
102
148
|
|
|
103
149
|
/**
|
|
104
150
|
* Generate multiple barcodes in batch
|
|
105
|
-
* @param {Array} items - Array of barcode configurations
|
|
106
|
-
* @returns {Promise<Array
|
|
151
|
+
* @param {Array<Object>} items - Array of barcode configurations
|
|
152
|
+
* @returns {Promise<Array<Object>>} Array of results
|
|
107
153
|
*/
|
|
108
|
-
static
|
|
154
|
+
static batch(items) {
|
|
109
155
|
return barcodeService.batch(items);
|
|
110
156
|
}
|
|
111
157
|
|
|
112
158
|
/**
|
|
113
159
|
* Get supported barcode types
|
|
114
|
-
* @returns {
|
|
160
|
+
* @returns {string[]} Array of supported types
|
|
115
161
|
*/
|
|
116
162
|
static getBarcodeTypes() {
|
|
117
|
-
return BarcodeTypes.
|
|
163
|
+
return BarcodeTypes.getAll();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Get supported barcode categories
|
|
168
|
+
* @returns {string[]} Array of category names
|
|
169
|
+
*/
|
|
170
|
+
static getBarcodeCategories() {
|
|
171
|
+
return BarcodeTypes.getCategories();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Get supported barcode types in a category
|
|
176
|
+
* @param {string} category - The category name
|
|
177
|
+
* @returns {string[]} Array of types
|
|
178
|
+
*/
|
|
179
|
+
static getBarcodeTypesByCategory(category) {
|
|
180
|
+
return BarcodeTypes.getByCategory(category);
|
|
118
181
|
}
|
|
119
182
|
|
|
120
183
|
/**
|
|
121
184
|
* Get supported render formats
|
|
122
|
-
* @returns {
|
|
185
|
+
* @returns {string[]} Array of supported formats
|
|
123
186
|
*/
|
|
124
187
|
static getRenderFormats() {
|
|
125
|
-
return RenderFormats.
|
|
188
|
+
return RenderFormats.getAll();
|
|
126
189
|
}
|
|
127
190
|
|
|
128
191
|
/**
|
|
129
|
-
* Get watermark positions
|
|
130
|
-
* @returns {
|
|
192
|
+
* Get watermark positions supported by the QR code builder
|
|
193
|
+
* @returns {string[]} Array of watermark positions
|
|
131
194
|
*/
|
|
132
195
|
static getWatermarkPositions() {
|
|
133
|
-
return [
|
|
134
|
-
'top-left',
|
|
135
|
-
'top-center',
|
|
136
|
-
'top-right',
|
|
137
|
-
'left-center',
|
|
138
|
-
'center',
|
|
139
|
-
'right-center',
|
|
140
|
-
'bottom-left',
|
|
141
|
-
'bottom-center',
|
|
142
|
-
'bottom-right',
|
|
143
|
-
];
|
|
196
|
+
return [...WATERMARK_POSITIONS];
|
|
144
197
|
}
|
|
145
198
|
|
|
146
199
|
/**
|
|
147
200
|
* Get barcode type information
|
|
148
201
|
* @param {string} type - The barcode type
|
|
149
|
-
* @returns {Object} Type information
|
|
202
|
+
* @returns {Object|null} Type information
|
|
150
203
|
*/
|
|
151
204
|
static getBarcodeTypeInfo(type) {
|
|
152
205
|
return BarcodeTypes.getTypeInfo(type);
|
|
@@ -155,12 +208,27 @@ if (isBrowser) {
|
|
|
155
208
|
/**
|
|
156
209
|
* Get render format information
|
|
157
210
|
* @param {string} format - The render format
|
|
158
|
-
* @returns {Object} Format information
|
|
211
|
+
* @returns {Object|null} Format information
|
|
159
212
|
*/
|
|
160
213
|
static getRenderFormatInfo(format) {
|
|
161
214
|
return RenderFormats.getFormatInfo(format);
|
|
162
215
|
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Get the default render options
|
|
219
|
+
* @returns {Object} Default options
|
|
220
|
+
*/
|
|
221
|
+
static getDefaultOptions() {
|
|
222
|
+
return BarcodeEncoder.getDefaultOptions();
|
|
223
|
+
}
|
|
163
224
|
}
|
|
164
225
|
|
|
226
|
+
BarcodeGenerator.QrCodeBuilder = QrCodeBuilder;
|
|
227
|
+
BarcodeGenerator.BarcodeTypes = BarcodeTypes;
|
|
228
|
+
BarcodeGenerator.RenderFormats = RenderFormats;
|
|
229
|
+
BarcodeGenerator.BarcodeEncoder = BarcodeEncoder;
|
|
230
|
+
BarcodeGenerator.BarcodeService = BarcodeService;
|
|
231
|
+
BarcodeGenerator.Validator = Validator;
|
|
232
|
+
|
|
165
233
|
module.exports = BarcodeGenerator;
|
|
166
234
|
}
|
package/package.json
CHANGED
|
@@ -1,94 +1,122 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "isahaq-barcode",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Universal barcode and QR code generator for Node.js and the browser: 40+ symbologies, PNG/SVG/HTML/PDF output, batch generation, logo and watermark support, validation, CLI and Express.js integration",
|
|
5
5
|
"main": "index.js",
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"browser": "./browser.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"browser": "./browser.js",
|
|
12
|
+
"require": "./index.js",
|
|
13
|
+
"default": "./index.js"
|
|
14
|
+
},
|
|
15
|
+
"./package.json": "./package.json"
|
|
15
16
|
},
|
|
16
17
|
"bin": {
|
|
17
18
|
"barcode-generate": "./bin/generate.js"
|
|
18
19
|
},
|
|
19
20
|
"scripts": {
|
|
20
|
-
"test": "
|
|
21
|
-
"test:watch": "
|
|
22
|
-
"test:coverage": "
|
|
23
|
-
"lint": "
|
|
24
|
-
"lint:fix": "
|
|
25
|
-
"format": "
|
|
26
|
-
"format:check": "
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
21
|
+
"test": "jest",
|
|
22
|
+
"test:watch": "jest --watch",
|
|
23
|
+
"test:coverage": "jest --coverage",
|
|
24
|
+
"lint": "eslint .",
|
|
25
|
+
"lint:fix": "eslint . --fix",
|
|
26
|
+
"format": "prettier --write .",
|
|
27
|
+
"format:check": "prettier --check .",
|
|
28
|
+
"typecheck": "tsc --noEmit index.d.ts",
|
|
29
|
+
"verify:scan": "node scripts/verify-scannability.mjs",
|
|
30
|
+
"build": "npm run lint && npm run format:check && npm run typecheck && npm test",
|
|
31
|
+
"prepare": "husky",
|
|
32
|
+
"prepublishOnly": "npm run build && npm run verify:scan",
|
|
30
33
|
"dev": "node examples/express-server.js",
|
|
31
34
|
"start": "node examples/express-server.js"
|
|
32
35
|
},
|
|
33
36
|
"keywords": [
|
|
34
37
|
"barcode",
|
|
38
|
+
"barcode-generator",
|
|
35
39
|
"qrcode",
|
|
40
|
+
"qr-code",
|
|
36
41
|
"generator",
|
|
37
42
|
"code128",
|
|
38
43
|
"code39",
|
|
44
|
+
"code93",
|
|
39
45
|
"ean13",
|
|
46
|
+
"ean8",
|
|
40
47
|
"upc",
|
|
48
|
+
"itf14",
|
|
41
49
|
"datamatrix",
|
|
42
50
|
"pdf417",
|
|
43
51
|
"aztec",
|
|
52
|
+
"maxicode",
|
|
53
|
+
"postnet",
|
|
54
|
+
"codabar",
|
|
55
|
+
"pharmacode",
|
|
44
56
|
"svg",
|
|
45
57
|
"png",
|
|
46
58
|
"pdf",
|
|
59
|
+
"html",
|
|
47
60
|
"cli",
|
|
48
61
|
"express",
|
|
49
|
-
"node"
|
|
62
|
+
"node",
|
|
63
|
+
"browser"
|
|
50
64
|
],
|
|
51
|
-
"author": "hmisahaq01@gmail.com",
|
|
65
|
+
"author": "Isahaq <hmisahaq01@gmail.com>",
|
|
52
66
|
"license": "MIT",
|
|
53
67
|
"dependencies": {
|
|
54
|
-
"
|
|
68
|
+
"bwip-js": "^4.11.2",
|
|
69
|
+
"commander": "^14.0.1",
|
|
70
|
+
"pdfkit": "^0.17.2",
|
|
55
71
|
"qrcode": "^1.5.4"
|
|
56
72
|
},
|
|
57
73
|
"optionalDependencies": {
|
|
58
|
-
"canvas": "^3.2.0"
|
|
59
|
-
"chalk": "^5.6.2",
|
|
60
|
-
"commander": "^14.0.1",
|
|
61
|
-
"ora": "^9.0.0",
|
|
62
|
-
"pdfkit": "^0.17.2"
|
|
63
|
-
},
|
|
64
|
-
"peerDependencies": {
|
|
65
|
-
"jsbarcode": "^3.12.1",
|
|
66
|
-
"qrcode": "^1.5.4"
|
|
74
|
+
"canvas": "^3.2.0"
|
|
67
75
|
},
|
|
68
76
|
"devDependencies": {
|
|
69
77
|
"@types/node": "^22.10.0",
|
|
70
78
|
"eslint": "^9.38.0",
|
|
71
79
|
"eslint-config-prettier": "^10.1.8",
|
|
72
|
-
"eslint-config-standard": "^17.1.0",
|
|
73
|
-
"eslint-plugin-import": "^2.32.0",
|
|
74
|
-
"eslint-plugin-node": "^11.1.0",
|
|
75
80
|
"eslint-plugin-prettier": "^5.5.4",
|
|
76
|
-
"
|
|
81
|
+
"express": "^5.2.1",
|
|
77
82
|
"husky": "^9.1.7",
|
|
78
83
|
"jest": "^30.2.0",
|
|
79
84
|
"lint-staged": "^15.2.10",
|
|
80
85
|
"prettier": "^3.4.0",
|
|
81
|
-
"typescript": "^5.7.0"
|
|
86
|
+
"typescript": "^5.7.0",
|
|
87
|
+
"zxing-wasm": "^2.2.4"
|
|
88
|
+
},
|
|
89
|
+
"lint-staged": {
|
|
90
|
+
"*.js": [
|
|
91
|
+
"eslint --fix",
|
|
92
|
+
"prettier --write"
|
|
93
|
+
],
|
|
94
|
+
"*.{json,md,yml}": [
|
|
95
|
+
"prettier --write"
|
|
96
|
+
]
|
|
82
97
|
},
|
|
83
98
|
"engines": {
|
|
84
99
|
"node": ">=18.0.0"
|
|
85
100
|
},
|
|
86
101
|
"repository": {
|
|
87
102
|
"type": "git",
|
|
88
|
-
"url": "https://github.com/isahaq1/
|
|
103
|
+
"url": "git+https://github.com/isahaq1/npm-barcodegenerator.git"
|
|
89
104
|
},
|
|
90
105
|
"bugs": {
|
|
91
|
-
"url": "https://github.com/isahaq1/
|
|
106
|
+
"url": "https://github.com/isahaq1/npm-barcodegenerator/issues"
|
|
107
|
+
},
|
|
108
|
+
"homepage": "https://github.com/isahaq1/npm-barcodegenerator#readme",
|
|
109
|
+
"publishConfig": {
|
|
110
|
+
"access": "public"
|
|
92
111
|
},
|
|
93
|
-
"
|
|
112
|
+
"files": [
|
|
113
|
+
"index.js",
|
|
114
|
+
"index.d.ts",
|
|
115
|
+
"browser.js",
|
|
116
|
+
"bin/",
|
|
117
|
+
"src/",
|
|
118
|
+
"README.md",
|
|
119
|
+
"CHANGELOG.md",
|
|
120
|
+
"LICENSE"
|
|
121
|
+
]
|
|
94
122
|
}
|