isahaq-barcode 1.0.0 → 1.2.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.
@@ -1,175 +1,175 @@
1
- /**
2
- * Barcode Service - Core service for barcode generation
3
- */
4
-
5
- const { BarcodeTypes } = require('../types/BarcodeTypes');
6
- const { RenderFormats } = require('../renderers/RenderFormats');
7
- const PNGRenderer = require('../renderers/PNGRenderer');
8
- const SVGRenderer = require('../renderers/SVGRenderer');
9
- const HTMLRenderer = require('../renderers/HTMLRenderer');
10
- const PDFRenderer = require('../renderers/PDFRenderer');
11
- const { Validator } = require('../validators/Validator');
12
-
13
- class BarcodeService {
14
- constructor() {
15
- this.renderers = {
16
- png: new PNGRenderer(),
17
- svg: new SVGRenderer(),
18
- html: new HTMLRenderer(),
19
- pdf: new PDFRenderer(),
20
- };
21
- this.validator = new Validator();
22
- }
23
-
24
- /**
25
- * Generate a barcode
26
- * @param {string} data - The data to encode
27
- * @param {string} type - The barcode type
28
- * @param {string} format - The output format
29
- * @param {Object} options - Additional options
30
- * @returns {Buffer|string} Generated barcode
31
- */
32
- generate(data, type = 'code128', format = 'png', options = {}) {
33
- // Validate inputs
34
- if (!data || typeof data !== 'string') {
35
- throw new Error('Data must be a non-empty string');
36
- }
37
-
38
- if (!BarcodeTypes.isValid(type)) {
39
- throw new Error(
40
- `Invalid barcode type: ${type}. Supported types: ${BarcodeTypes.getAll().join(
41
- ', '
42
- )}`
43
- );
44
- }
45
-
46
- if (!RenderFormats.isValid(format)) {
47
- throw new Error(
48
- `Invalid format: ${format}. Supported formats: ${RenderFormats.getAll().join(
49
- ', '
50
- )}`
51
- );
52
- }
53
-
54
- // Validate data for the specific barcode type
55
- const validation = this.validator.validate(data, type);
56
- if (!validation.valid) {
57
- throw new Error(`Invalid data for ${type}: ${validation.error}`);
58
- }
59
-
60
- // Get renderer
61
- const renderer = this.renderers[format];
62
- if (!renderer) {
63
- throw new Error(`No renderer available for format: ${format}`);
64
- }
65
-
66
- // Generate barcode
67
- return renderer.render(data, type, options);
68
- }
69
-
70
- /**
71
- * Generate a barcode in PNG format
72
- * @param {string} data - The data to encode
73
- * @param {string} type - The barcode type
74
- * @param {Object} options - Additional options
75
- * @returns {Buffer} PNG buffer
76
- */
77
- png(data, type = 'code128', options = {}) {
78
- return this.generate(data, type, 'png', options);
79
- }
80
-
81
- /**
82
- * Generate a barcode in SVG format
83
- * @param {string} data - The data to encode
84
- * @param {string} type - The barcode type
85
- * @param {Object} options - Additional options
86
- * @returns {string} SVG string
87
- */
88
- svg(data, type = 'code128', options = {}) {
89
- return this.generate(data, type, 'svg', options);
90
- }
91
-
92
- /**
93
- * Generate a barcode in HTML format
94
- * @param {string} data - The data to encode
95
- * @param {string} type - The barcode type
96
- * @param {Object} options - Additional options
97
- * @returns {string} HTML string
98
- */
99
- html(data, type = 'code128', options = {}) {
100
- return this.generate(data, type, 'html', options);
101
- }
102
-
103
- /**
104
- * Generate a barcode in PDF format
105
- * @param {string} data - The data to encode
106
- * @param {string} type - The barcode type
107
- * @param {Object} options - Additional options
108
- * @returns {Buffer} PDF buffer
109
- */
110
- pdf(data, type = 'code128', options = {}) {
111
- return this.generate(data, type, 'pdf', options);
112
- }
113
-
114
- /**
115
- * Generate multiple barcodes in batch
116
- * @param {Array} items - Array of barcode generation items
117
- * @returns {Array} Array of generated barcodes
118
- */
119
- batch(items) {
120
- if (!Array.isArray(items)) {
121
- throw new Error('Items must be an array');
122
- }
123
-
124
- return items.map((item, index) => {
125
- try {
126
- const { data, type = 'code128', format = 'png', options = {} } = item;
127
- return {
128
- index,
129
- success: true,
130
- result: this.generate(data, type, format, options),
131
- data,
132
- type,
133
- format,
134
- };
135
- } catch (error) {
136
- return {
137
- index,
138
- success: false,
139
- error: error.message,
140
- data: item.data,
141
- type: item.type || 'code128',
142
- format: item.format || 'png',
143
- };
144
- }
145
- });
146
- }
147
-
148
- /**
149
- * Validate data for a specific barcode type
150
- * @param {string} data - The data to validate
151
- * @param {string} type - The barcode type
152
- * @returns {Object} Validation result
153
- */
154
- validate(data, type) {
155
- return this.validator.validate(data, type);
156
- }
157
-
158
- /**
159
- * Get available barcode types
160
- * @returns {Array} Array of barcode types
161
- */
162
- getBarcodeTypes() {
163
- return BarcodeTypes.getAll();
164
- }
165
-
166
- /**
167
- * Get available render formats
168
- * @returns {Array} Array of render formats
169
- */
170
- getRenderFormats() {
171
- return RenderFormats.getAll();
172
- }
173
- }
174
-
175
- module.exports = BarcodeService;
1
+ /**
2
+ * Barcode Service - Core service for barcode generation
3
+ */
4
+
5
+ const { BarcodeTypes } = require('../types/BarcodeTypes');
6
+ const { RenderFormats } = require('../renderers/RenderFormats');
7
+ const PNGRenderer = require('../renderers/PNGRenderer');
8
+ const SVGRenderer = require('../renderers/SVGRenderer');
9
+ const HTMLRenderer = require('../renderers/HTMLRenderer');
10
+ const PDFRenderer = require('../renderers/PDFRenderer');
11
+ const { Validator } = require('../validators/Validator');
12
+
13
+ class BarcodeService {
14
+ constructor() {
15
+ this.renderers = {
16
+ png: new PNGRenderer(),
17
+ svg: new SVGRenderer(),
18
+ html: new HTMLRenderer(),
19
+ pdf: new PDFRenderer(),
20
+ };
21
+ this.validator = new Validator();
22
+ }
23
+
24
+ /**
25
+ * Generate a barcode
26
+ * @param {string} data - The data to encode
27
+ * @param {string} type - The barcode type
28
+ * @param {string} format - The output format
29
+ * @param {Object} options - Additional options
30
+ * @returns {Buffer|string} Generated barcode
31
+ */
32
+ generate(data, type = 'code128', format = 'png', options = {}) {
33
+ // Validate inputs
34
+ if (!data || typeof data !== 'string') {
35
+ throw new Error('Data must be a non-empty string');
36
+ }
37
+
38
+ if (!BarcodeTypes.isValid(type)) {
39
+ throw new Error(
40
+ `Invalid barcode type: ${type}. Supported types: ${BarcodeTypes.getAll().join(
41
+ ', '
42
+ )}`
43
+ );
44
+ }
45
+
46
+ if (!RenderFormats.isValid(format)) {
47
+ throw new Error(
48
+ `Invalid format: ${format}. Supported formats: ${RenderFormats.getAll().join(
49
+ ', '
50
+ )}`
51
+ );
52
+ }
53
+
54
+ // Validate data for the specific barcode type
55
+ const validation = this.validator.validate(data, type);
56
+ if (!validation.valid) {
57
+ throw new Error(`Invalid data for ${type}: ${validation.error}`);
58
+ }
59
+
60
+ // Get renderer
61
+ const renderer = this.renderers[format];
62
+ if (!renderer) {
63
+ throw new Error(`No renderer available for format: ${format}`);
64
+ }
65
+
66
+ // Generate barcode
67
+ return renderer.render(data, type, options);
68
+ }
69
+
70
+ /**
71
+ * Generate a barcode in PNG format
72
+ * @param {string} data - The data to encode
73
+ * @param {string} type - The barcode type
74
+ * @param {Object} options - Additional options
75
+ * @returns {Buffer} PNG buffer
76
+ */
77
+ png(data, type = 'code128', options = {}) {
78
+ return this.generate(data, type, 'png', options);
79
+ }
80
+
81
+ /**
82
+ * Generate a barcode in SVG format
83
+ * @param {string} data - The data to encode
84
+ * @param {string} type - The barcode type
85
+ * @param {Object} options - Additional options
86
+ * @returns {string} SVG string
87
+ */
88
+ svg(data, type = 'code128', options = {}) {
89
+ return this.generate(data, type, 'svg', options);
90
+ }
91
+
92
+ /**
93
+ * Generate a barcode in HTML format
94
+ * @param {string} data - The data to encode
95
+ * @param {string} type - The barcode type
96
+ * @param {Object} options - Additional options
97
+ * @returns {string} HTML string
98
+ */
99
+ html(data, type = 'code128', options = {}) {
100
+ return this.generate(data, type, 'html', options);
101
+ }
102
+
103
+ /**
104
+ * Generate a barcode in PDF format
105
+ * @param {string} data - The data to encode
106
+ * @param {string} type - The barcode type
107
+ * @param {Object} options - Additional options
108
+ * @returns {Buffer} PDF buffer
109
+ */
110
+ pdf(data, type = 'code128', options = {}) {
111
+ return this.generate(data, type, 'pdf', options);
112
+ }
113
+
114
+ /**
115
+ * Generate multiple barcodes in batch
116
+ * @param {Array} items - Array of barcode generation items
117
+ * @returns {Array} Array of generated barcodes
118
+ */
119
+ batch(items) {
120
+ if (!Array.isArray(items)) {
121
+ throw new Error('Items must be an array');
122
+ }
123
+
124
+ return items.map((item, index) => {
125
+ try {
126
+ const { data, type = 'code128', format = 'png', options = {} } = item;
127
+ return {
128
+ index,
129
+ success: true,
130
+ result: this.generate(data, type, format, options),
131
+ data,
132
+ type,
133
+ format,
134
+ };
135
+ } catch (error) {
136
+ return {
137
+ index,
138
+ success: false,
139
+ error: error.message,
140
+ data: item.data,
141
+ type: item.type || 'code128',
142
+ format: item.format || 'png',
143
+ };
144
+ }
145
+ });
146
+ }
147
+
148
+ /**
149
+ * Validate data for a specific barcode type
150
+ * @param {string} data - The data to validate
151
+ * @param {string} type - The barcode type
152
+ * @returns {Object} Validation result
153
+ */
154
+ validate(data, type) {
155
+ return this.validator.validate(data, type);
156
+ }
157
+
158
+ /**
159
+ * Get available barcode types
160
+ * @returns {Array} Array of barcode types
161
+ */
162
+ getBarcodeTypes() {
163
+ return BarcodeTypes.getAll();
164
+ }
165
+
166
+ /**
167
+ * Get available render formats
168
+ * @returns {Array} Array of render formats
169
+ */
170
+ getRenderFormats() {
171
+ return RenderFormats.getAll();
172
+ }
173
+ }
174
+
175
+ module.exports = BarcodeService;