Coverage

97%
571
554
17

/home/johan/DevZone/JsBarcode/JsBarcode.js

86%
130
113
17
LineHitsSource
11(function(){
2
3 // Main function, calls drawCanvas(...) in the right way
41 var JsBarcode = function(image, content, options){
5 // If the image is a string, query select call again
621 if(typeof image === "string"){
71 image = document.querySelector(image);
80 JsBarcode(image, content, options);
9 }
10 // If image, draw on canvas and set the uri as src
1120 else if(typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLImageElement){
120 canvas = document.createElement('canvas');
130 drawCanvas(canvas, content, options);
140 image.setAttribute("src", canvas.toDataURL());
15 }
16 // If canvas, just draw
1720 else if(image.getContext){
1819 drawCanvas(image, content, options);
19 }
20 else{
211 throw new Error("Not supported type to draw on.");
22 }
23 }
24
25 // The main function, handles everything with the modules and draws the image
261 var drawCanvas = function(canvas, content, options) {
27 // Merge the user options with the default
2819 options = merge(JsBarcode.defaults, options);
29
30 // Fix the margins
3119 options.marginTop = options.marginTop | options.margin;
3219 options.marginBottom = options.marginBottom | options.margin;
3319 options.marginRight = options.marginRight | options.margin;
3419 options.marginLeft = options.marginLeft | options.margin;
35
36 //Abort if the browser does not support HTML5 canvas
3719 if (!canvas.getContext) {
380 throw new Error('The browser does not support canvas.');
39 }
40
41 // Automatically choose barcode if format set to "auto"...
4219 if(options.format == "auto"){
433 var encoder = new (JsBarcode.autoSelectEncoder(content))(content);
44 }
45 // ...or else, get by name
46 else{
4716 var encoder = new (JsBarcode.getModule(options.format))(content);
48 }
49
50 //Abort if the barcode format does not support the content
5118 if(!encoder.valid()){
522 options.valid(false);
532 if(options.valid == JsBarcode.defaults.valid){
541 throw new Error('The data is not valid for the type of barcode.');
55 }
561 return;
57 }
58
59 // Set the binary to a cached version if possible
6016 var cachedBinary = JsBarcode.getCache(options.format, content);
6116 if(cachedBinary){
629 var binary = cachedBinary;
63 }
64 else{
65 // Encode the content
667 var binary = encoder.encoded();
67 // Cache the encoding if it will be used again later
687 JsBarcode.cache(options.format, content, binary);
69 }
70
71 // Get the canvas context
7216 var ctx = canvas.getContext("2d");
73
74 // Set font
7516 var font = options.fontOptions + " " + options.fontSize + "px "+options.font;
7616 ctx.font = font;
77
78 // Set the width and height of the barcode
7916 var width = binary.length*options.width;
80 // Replace with width of the text if it is wider then the barcode
8116 var textWidth = ctx.measureText(encoder.getText()).width;
8216 if(options.displayValue && width < textWidth){
830 if(options.textAlign == "center"){
840 var barcodePadding = Math.floor((textWidth - width)/2);
85 }
860 else if(options.textAlign == "left"){
870 var barcodePadding = 0;
88 }
890 else if(options.textAlign == "right"){
900 var barcodePadding = Math.floor(textWidth - width);
91 }
92
930 width = textWidth;
94 }
95 // Make sure barcodePadding is not undefined
9616 var barcodePadding = barcodePadding || 0;
97
9816 canvas.width = width + options.marginLeft + options.marginRight;
99
100 // Set extra height if the value is displayed under the barcode. Multiplication with 1.3 t0 ensure that some
101 //characters are not cut in half
10216 canvas.height = options.height
103 + (options.displayValue ? options.fontSize : 0)
104 + options.textMargin
105 + options.marginTop
106 + options.marginBottom;
107
108 // Paint the canvas
10916 ctx.clearRect(0,0,canvas.width,canvas.height);
11016 if(options.background){
11116 ctx.fillStyle = options.background;
11216 ctx.fillRect(0,0,canvas.width, canvas.height);
113 }
114
115 // Creates the barcode out of the encoded binary
11616 ctx.fillStyle = options.lineColor;
11716 for(var i=0;i<binary.length;i++){
1181538 var x = i*options.width + options.marginLeft + barcodePadding;
1191538 if(binary[i] == "1"){
120730 ctx.fillRect(x, options.marginTop, options.width, options.height);
121 }
122 }
123
124 // Draw the text if displayValue is set
12516 if(options.displayValue){
12615 var x, y;
127
12815 y = options.height + options.textMargin + options.marginTop;
129
13015 ctx.font = font;
13115 ctx.textBaseline = "bottom";
13215 ctx.textBaseline = 'top';
133
134 // Draw the text in the correct X depending on the textAlign option
13515 if(options.textAlign == "left" || barcodePadding > 0){
1361 x = options.marginLeft;
1371 ctx.textAlign = 'left';
138 }
13914 else if(options.textAlign == "right"){
1401 x = canvas.width - options.marginRight;
1411 ctx.textAlign = 'right';
142 }
143 //In all other cases, center the text
144 else{
14513 x = canvas.width / 2;
14613 ctx.textAlign = 'center';
147 }
148
14915 ctx.fillText(encoder.getText(), x, y);
150 }
151
152 // Send a confirmation that the generation was successful to the valid function if it does exist
15316 options.valid(true);
154 };
155
1561 JsBarcode._modules = [];
157
158 // Add a new module sorted in the array
1591 JsBarcode.register = function(module, regex, priority){
16017 var position = 0;
16117 if(typeof priority === "undefined"){
1624 position = JsBarcode._modules.length - 1;
163 }
164 else{
16513 for(var i=0;i<JsBarcode._modules.length;i++){
16638 position = i;
16738 if(!(priority < JsBarcode._modules[i].priority)){
16810 break;
169 }
170 }
171 }
172
173 // Add the module in position position
17417 JsBarcode._modules.splice(position, 0, {
175 "regex": regex,
176 "module": module,
177 "priority": priority
178 });
179 };
180
181 // Get module by name
1821 JsBarcode.getModule = function(name){
18333 for(var i in JsBarcode._modules){
184368 if(name.search(JsBarcode._modules[i].regex) !== -1){
18532 return JsBarcode._modules[i].module;
186 }
187 }
1881 throw new Error('Module ' + name + ' does not exist or is not loaded.');
189 };
190
191 // If any format is valid with the content, return the format with highest priority
1921 JsBarcode.autoSelectEncoder = function(content){
1933 for(var i in JsBarcode._modules){
19417 var barcode = new (JsBarcode._modules[i].module)(content);
19517 if(barcode.valid(content)){
1963 return JsBarcode._modules[i].module;
197 }
198 }
1990 throw new Error("Can't automatically find a barcode format matching the string '" + content + "'");
200 };
201
202 // Defining the cache dictionary
2031 JsBarcode._cache = {};
204
205 // Cache a regerated barcode
2061 JsBarcode.cache = function(format, input, output){
2077 if(!JsBarcode._cache[format]){
2084 JsBarcode._cache[format] = {};
209 }
2107 JsBarcode._cache[format][input] = output;
211 };
212
213 // Get a chached barcode
2141 JsBarcode.getCache = function(format, input){
21516 if(JsBarcode._cache[format]){
21612 if(JsBarcode._cache[format][input]){
2179 return JsBarcode._cache[format][input];
218 }
219 }
2207 return "";
221 };
222
223 // Detect if the code is running under nodejs
2241 JsBarcode._isNode = false;
2251 if (typeof module !== 'undefined' && module.exports) {
2261 module.exports = JsBarcode; // Export to nodejs
2271 JsBarcode._isNode = true;
228
229 //Register all modules in ./barcodes/
2301 var path = require("path");
2311 var dir = path.join(__dirname, "barcodes");
2321 var files = require("fs").readdirSync(dir);
2331 for(var i in files){
2348 var barcode = require(path.join(dir, files[i]));
2358 barcode.register(JsBarcode);
236 }
237 }
238
239 //Regsiter JsBarcode for the browser
2401 if(typeof window !== 'undefined'){
2410 window.JsBarcode = JsBarcode;
242 }
243
244 // Register JsBarcode as an jQuery plugin if jQuery exist
2451 if (typeof jQuery !== 'undefined') {
2460 jQuery.fn.JsBarcode = function(content, options, validFunction){
2470 JsBarcode(this.get(0), content, options, validFunction);
2480 return this;
249 };
250 }
251
252 // All the default options. If one is not set.
2531 JsBarcode.defaults = {
254 width: 2,
255 height: 100,
256 format: "auto",
257 displayValue: true,
258 fontOptions: "",
259 font: "monospace",
260 textAlign: "center",
261 textMargin: 2,
262 fontSize: 20,
263 background: "#ffffff",
264 lineColor: "#000000",
265 margin: 10,
266 marginTop: undefined,
267 marginBottom: undefined,
268 marginLeft: undefined,
269 marginRight: undefined,
270 valid: function(valid){}
271 };
272
273 // Function to merge the default options with the default ones
2741 var merge = function(m1, m2) {
27519 var newMerge = {};
27619 for (var k in m1) {
277323 newMerge[k] = m1[k];
278 }
27919 for (var k in m2) {
28026 if(typeof m2[k] !== "undefined"){
28126 newMerge[k] = m2[k];
282 }
283 }
28419 return newMerge;
285 };
286})();
287

/home/johan/DevZone/JsBarcode/barcodes/CODE128.js

100%
157
157
0
LineHitsSource
1// ASCII value ranges 0-127, 200-211
21var validCODE128 = /^[\x00-\x7F\xC8-\xD3]+$/;
3
4// This is the master class, it does require the start code to be
5//included in the string
61function CODE128(string) {
7 // Fill the bytes variable with the ascii codes of string
829 this.bytes = [];
929 for (var i = 0; i < string.length; ++i) {
10232 this.bytes.push(string.charCodeAt(i));
11 }
12
13 // First element should be startcode, remove that
1429 this.string = string.substring(1);
15
1629 this.getText = function() {
1719 var string = this.string;
18
1919 string = string.replace(String.fromCharCode(201), "[FNC3]");
2019 string = string.replace(String.fromCharCode(202), "[FNC2]");
2119 string = string.replace(String.fromCharCode(203), "[SHIFT]");
2219 string = string.replace(String.fromCharCode(207), "[FNC1]");
23
2419 return string.replace(/[^\x20-\x7E]/g, "");
25 };
26
27 // The public encoding function
2829 this.encoded = function() {
2911 var encodingResult;
3011 var bytes = this.bytes;
31 // Remove the startcode from the bytes and set its index
3211 var startIndex = bytes.shift() - 105;
33
34 // Start encode with the right type
3511 if(startIndex === 103){
364 encodingResult = nextA(bytes, 1);
37 }
387 else if(startIndex === 104){
394 encodingResult = nextB(bytes, 1);
40 }
413 else if(startIndex === 105){
423 encodingResult = nextC(bytes, 1);
43 }
44
4511 return (
46 //Add the start bits
47 getEncoding(startIndex) +
48 //Add the encoded bits
49 encodingResult.result +
50 //Add the checksum
51 getEncoding((encodingResult.checksum + startIndex) % 103) +
52 //Add the end bits
53 getEncoding(106)
54 );
55 }
56
57 //Data for each character, the last characters will not be encoded but are used for error correction
58 //Numbers encode to (n + 1000) -> binary; 740 -> (740 + 1000).toString(2) -> "11011001100"
5929 var code128b = [ // + 1000
60 740, 644, 638, 176, 164, 100, 224, 220, 124, 608, 604,
61 572, 436, 244, 230, 484, 260, 254, 650, 628, 614, 764,
62 652, 902, 868, 836, 830, 892, 844, 842, 752, 734, 590,
63 304, 112, 94, 416, 128, 122, 672, 576, 570, 464, 422,
64 134, 496, 478, 142, 910, 678, 582, 768, 762, 774, 880,
65 862, 814, 896, 890, 818, 914, 602, 930, 328, 292, 200,
66 158, 68, 62, 424, 412, 232, 218, 76, 74, 554, 616,
67 978, 556, 146, 340, 212, 182, 508, 268, 266, 956, 940,
68 938, 758, 782, 974, 400, 310, 118, 512, 506, 960, 954,
69 502, 518, 886, 966, /* Start codes */ 668, 680, 692,
70 5379
71 ];
7229 var getEncoding = function(n) {
73122 return (code128b[n] ? (code128b[n] + 1000).toString(2) : '');
74 };
75
76 // Use the regexp variable for validation
7729 this.valid = function() {
7812 return !(this.string.search(validCODE128) === -1);
79 }
80
8129 function nextA(bytes, depth){
8232 if(bytes.length <= 0){
834 return {"result": "", "checksum": 0};
84 }
85
8628 var next, index;
87
88 // Special characters
8928 if(bytes[0] >= 200){
905 index = bytes[0] - 105;
91
92 //Remove first element
935 bytes.shift();
94
95 // Swap to CODE128C
965 if(index === 99){
971 next = nextC(bytes, depth + 1);
98 }
99 // Swap to CODE128B
1004 else if(index === 100){
1012 next = nextB(bytes, depth + 1);
102 }
103 // Shift
1042 else if(index === 98){
105 // Convert the next character so that is encoded correctly
1061 bytes[0] = bytes[0] > 95 ? bytes[0] - 96 : bytes[0];
1071 next = nextA(bytes, depth + 1);
108 }
109 // Continue on CODE128A but encode a special character
110 else{
1111 next = nextA(bytes, depth + 1);
112 }
113 }
114 // Continue encoding of CODE128A
115 else{
11623 var charCode = bytes[0];
11723 index = charCode < 32 ? charCode + 64 : charCode - 32;
118
119 // Remove first element
12023 bytes.shift();
121
12223 next = nextA(bytes, depth + 1);
123 }
124
125 // Get the correct binary encoding and calculate the weight
12628 var enc = getEncoding(index);
12728 var weight = index * depth;
128
12928 return {"result": enc + next.result, "checksum": weight + next.checksum}
130 }
131
13229 function nextB(bytes, depth){
13342 if(bytes.length <= 0){
1344 return {"result": "", "checksum": 0};
135 }
136
13738 var next, index;
138
139 // Special characters
14038 if(bytes[0] >= 200){
1415 index = bytes[0] - 105;
142
143 //Remove first element
1445 bytes.shift();
145
146 // Swap to CODE128C
1475 if(index === 99){
1482 next = nextC(bytes, depth + 1);
149 }
150 // Swap to CODE128A
1513 else if(index === 101){
1521 next = nextA(bytes, depth + 1);
153 }
154 // Shift
1552 else if(index === 98){
156 // Convert the next character so that is encoded correctly
1571 bytes[0] = bytes[0] < 32 ? bytes[0] + 96 : bytes[0];
1581 next = nextB(bytes, depth + 1);
159 }
160 // Continue on CODE128B but encode a special character
161 else{
1621 next = nextB(bytes, depth + 1);
163 }
164 }
165 // Continue encoding of CODE128B
166 else {
16733 index = bytes[0] - 32;
16833 bytes.shift();
16933 next = nextB(bytes, depth + 1);
170 }
171
172 // Get the correct binary encoding and calculate the weight
17338 var enc = getEncoding(index);
17438 var weight = index * depth;
175
17638 return {"result": enc + next.result, "checksum": weight + next.checksum};
177 }
178
17929 function nextC(bytes, depth){
18026 if(bytes.length <= 0){
1813 return {"result": "", "checksum": 0};
182 }
183
18423 var next, index;
185
186 // Special characters
18723 if(bytes[0] >= 200){
1885 index = bytes[0] - 105;
189
190 // Remove first element
1915 bytes.shift();
192
193 // Swap to CODE128B
1945 if(index === 100){
1951 next = nextB(bytes, depth + 1);
196 }
197 // Swap to CODE128A
1984 else if(index === 101){
1992 next = nextA(bytes, depth + 1);
200 }
201 // Continue on CODE128C but encode a special character
202 else{
2032 next = nextC(bytes, depth + 1);
204 }
205 }
206 // Continue encoding of CODE128C
207 else{
20818 index = (bytes[0]-48) * 10 + bytes[1]-48;
20918 bytes.shift();
21018 bytes.shift();
21118 next = nextC(bytes, depth + 1);
212 }
213
214 // Get the correct binary encoding and calculate the weight
21523 var enc = getEncoding(index);
21623 var weight = index * depth;
217
21823 return {"result": enc + next.result, "checksum": weight + next.checksum};
219 }
220}
221
2221function autoSelectModes(string){
223 // ASCII ranges 0-98 and 200-207 (FUNCs and SHIFTs)
22418 var aLength = string.match(/^[\x00-\x5F\xC8-\xCF]*/)[0].length;
225 // ASCII ranges 32-127 and 200-207 (FUNCs and SHIFTs)
22618 var bLength = string.match(/^[\x20-\x7F\xC8-\xCF]*/)[0].length;
227 // Number pairs or [FNC1]
22818 var cLength = string.match(/^(\xCF*[0-9]{2}\xCF*)*/)[0].length;
229
23018 var newString;
231 // Select CODE128C if the string start with enough digits
23218 if(cLength >= 2){
2332 newString = String.fromCharCode(210) + autoSelectFromC(string);
234 }
235 // Select A/C depending on the longest match
23616 else if(aLength > bLength){
2373 newString = String.fromCharCode(208) + autoSelectFromA(string);
238 }
239 else{
24013 newString = String.fromCharCode(209) + autoSelectFromB(string);
241 }
242
24318 newString = newString.replace(/[\xCD\xCE]([^])[\xCD\xCE]/, function(match, char){
2442 return String.fromCharCode(203) + char;
245 });
246
24718 return newString;
248}
249
2501function autoSelectFromA(string){
2518 var untilC = string.match(/^([\x00-\x5F\xC8-\xCF]+?)(([0-9]{2}){2,})([^0-9]|$)/);
252
2538 if(untilC){
2541 return untilC[1] + String.fromCharCode(204) + autoSelectFromC(string.substring(untilC[1].length));
255 }
256
2577 var aChars = string.match(/^[\x00-\x5F\xC8-\xCF]+/);
2587 if(aChars[0].length === string.length){
2593 return string;
260 }
261
2624 return aChars[0] + String.fromCharCode(205) + autoSelectFromB(string.substring(aChars[0].length));
263}
264
2651function autoSelectFromB(string){
26618 var untilC = string.match(/^([\x20-\x7F\xC8-\xCF]+?)(([0-9]{2}){2,})([^0-9]|$)/);
267
26818 if(untilC){
2692 return untilC[1] + String.fromCharCode(204) + autoSelectFromC(string.substring(untilC[1].length));
270 }
271
27216 var bChars = string.match(/^[\x20-\x7F\xC8-\xCF]+/);
27316 if(bChars[0].length === string.length){
27413 return string;
275 }
276
2773 return bChars[0] + String.fromCharCode(206) + autoSelectFromA(string.substring(bChars[0].length));
278}
279
280
2811function autoSelectFromC(string){
2825 var cMatch = string.match(/^(\xCF*[0-9]{2}\xCF*)+/)[0];
2835 var length = cMatch.length;
284
2855 if(length === string.length){
2862 return string;
287 }
288
2893 string = string.substring(length);
290
291 // Select A/B depending on the longest match
2923 var aLength = string.match(/^[\x00-\x5F\xC8-\xCF]*/)[0].length;
2933 var bLength = string.match(/^[\x20-\x7F\xC8-\xCF]*/)[0].length;
2943 if(aLength >= bLength){
2952 return cMatch + String.fromCharCode(206) + autoSelectFromA(string);
296 }
297 else{
2981 return cMatch + String.fromCharCode(205) + autoSelectFromB(string);
299 }
300}
301
3021function CODE128AUTO(string) {
303 // Check the validity of the string, don't even bother auto it when
304 //it's not valid
30519 if(string.search(validCODE128) !== -1){
30618 return new CODE128(autoSelectModes(string));
307 }
3081 return new CODE128(string);
309}
3101function CODE128A(string) {
3113 var code128 = new CODE128(String.fromCharCode(208) + string);
3123 code128.valid = function(){
3132 return this.string.search(/^[\x00-\x5F\xC8-\xCF]+$/) !== -1;
314 }
3153 return code128;
316}
3171function CODE128B(string) {
3183 var code128 = new CODE128(String.fromCharCode(209) + string);
3193 code128.valid = function(){
3202 return this.string.search(/^[\x20-\x7F\xC8-\xCF]+$/) !== -1;
321 }
3223 return code128;
323}
3241function CODE128C(string) {
3254 var code128 = new CODE128(String.fromCharCode(210) + string);
3264 code128.valid = function(str){
3273 return this.string.search(/^(\xCF*[0-9]{2}\xCF*)+$/) !== -1;
328 }
3294 return code128;
330}
331
332//Required to register for both browser and nodejs
3331var register = function(core) {
3341 core.register(CODE128AUTO, /^CODE128(.?AUTO)?$/, 10);
3351 core.register(CODE128A, /^CODE128.?A$/i, 2);
3361 core.register(CODE128B, /^CODE128.?B$/i, 3);
3371 core.register(CODE128C, /^CODE128.?C$/i, 2);
338}
3392try {register(JsBarcode)} catch(e) {}
3402try {module.exports.register = register} catch(e) {}
341

/home/johan/DevZone/JsBarcode/barcodes/CODE39.js

100%
18
18
0
LineHitsSource
11function CODE39(string){
29 this.string = string.toUpperCase();
3
49 var encodings = {
5 "0": 20957, "1": 29783, "2": 23639, "3": 30485,
6 "4": 20951, "5": 29813, "6": 23669, "7": 20855,
7 "8": 29789, "9": 23645, "A": 29975, "B": 23831,
8 "C": 30533, "D": 22295, "E": 30149, "F": 24005,
9 "G": 21623, "H": 29981, "I": 23837, "J": 22301,
10 "K": 30023, "L": 23879, "M": 30545, "N": 22343,
11 "O": 30161, "P": 24017, "Q": 21959, "R": 30065,
12 "S": 23921, "T": 22385, "U": 29015, "V": 18263,
13 "W": 29141, "X": 17879, "Y": 29045, "Z": 18293,
14 "-": 17783, ".": 29021, " ": 18269, "$": 17477,
15 "/": 17489, "+": 17681, "%": 20753, "*": 35770
16 };
17
18
199 this.getText = function(){
209 return this.string;
21 };
22
239 this.encoded = function(){
245 var result = "";
255 result += encodings["*"].toString(2);
265 for(var i=0; i<this.string.length; i++){
2724 result += encodings[this.string[i]].toString(2) + "0";
28 }
295 result += encodings["*"].toString(2);
30
315 return result;
32 };
33
34 //Use the regexp variable for validation
359 this.valid = function(){
367 return this.string.search(/^[0-9A-Z\-\.\ \$\/\+\%]+$/) !== -1;
37 };
38}
39
40
41//Required to register for both browser and nodejs
421var register = function(core){
431 core.register(CODE39, /^CODE.?39$/i, 3);
44};
452try{register(JsBarcode)} catch(e){}
462try{module.exports.register = register} catch(e){}
47

/home/johan/DevZone/JsBarcode/barcodes/EAN_UPC.js

100%
99
99
0
LineHitsSource
11function EAN(EANnumber){
215 this.EANnumber = EANnumber+"";
3
4 //Regexp to test if the EAN code is correct formated
515 var fullEanRegexp = /^[0-9]{13}$/;
615 var needLastDigitRegexp = /^[0-9]{12}$/;
7
8 //Add checksum if it does not exist
915 if(this.EANnumber.search(needLastDigitRegexp)!=-1){
102 this.EANnumber += checksum(this.EANnumber);
11 }
12
1315 this.getText = function(){
146 return this.EANnumber;
15 };
16
1715 this.valid = function(){
1812 return valid(this.EANnumber);
19 };
20
2115 this.encoded = function (){
224 return createEAN13(this.EANnumber);
23 }
24
25 //Create the binary representation of the EAN code
26 //number needs to be a string
2715 function createEAN13(number){
284 var encoder = new EANencoder();
29
30 //Create the return variable
314 var result = "";
32
334 var structure = encoder.getEANstructure(number);
34
35 //Get the number to be encoded on the left side of the EAN code
364 var leftSide = number.substr(1,7);
37
38 //Get the number to be encoded on the right side of the EAN code
394 var rightSide = number.substr(7,6);
40
41 //Add the start bits
424 result += encoder.startBin;
43
44 //Add the left side
454 result += encoder.encode(leftSide, structure);
46
47 //Add the middle bits
484 result += encoder.middleBin;
49
50 //Add the right side
514 result += encoder.encode(rightSide,"RRRRRR");
52
53 //Add the end bits
544 result += encoder.endBin;
55
564 return result;
57 }
58
59 //Calulate the checksum digit
6015 function checksum(number){
616 var result = 0;
62
6342 for(var i=0;i<12;i+=2){result+=parseInt(number[i])}
6442 for(var i=1;i<12;i+=2){result+=parseInt(number[i])*3}
65
666 return (10 - (result % 10)) % 10;
67 }
68
6915 function valid(number){
7012 if(number.search(fullEanRegexp)!=-1){
714 return number[12] == checksum(number);
72 }
73 else{
748 return false;
75 }
76 }
77}
78
791function EAN8(EAN8number){
808 this.EAN8number = EAN8number+"";
81
82 //Regexp to test if the EAN code is correct formated
838 var fullEanRegexp = /^[0-9]{8}$/;
848 var needLastDigitRegexp = /^[0-9]{7}$/;
85
86 //Add checksum if it does not exist
878 if(this.EAN8number.search(needLastDigitRegexp)!=-1){
881 this.EAN8number += checksum(this.EAN8number);
89 }
90
918 this.getText = function(){
921 return this.EAN8number;
93 }
94
958 this.valid = function(){
968 return valid(this.EAN8number);
97 };
98
998 this.encoded = function (){
1002 return createEAN8(this.EAN8number);
101 }
102
1038 function valid(number){
1048 if(number.search(fullEanRegexp)!=-1){
1053 return number[7] == checksum(number);
106 }
107 else{
1085 return false;
109 }
110 }
111
112 //Calulate the checksum digit
1138 function checksum(number){
1144 var result = 0;
115
11620 for(var i=0;i<7;i+=2){result+=parseInt(number[i])*3}
11716 for(var i=1;i<7;i+=2){result+=parseInt(number[i])}
118
1194 return (10 - (result % 10)) % 10;
120 }
121
1228 function createEAN8(number){
1232 var encoder = new EANencoder();
124
125 //Create the return variable
1262 var result = "";
127
128 //Get the number to be encoded on the left side of the EAN code
1292 var leftSide = number.substr(0,4);
130
131 //Get the number to be encoded on the right side of the EAN code
1322 var rightSide = number.substr(4,4);
133
134 //Add the start bits
1352 result += encoder.startBin;
136
137 //Add the left side
1382 result += encoder.encode(leftSide, "LLLL");
139
140 //Add the middle bits
1412 result += encoder.middleBin;
142
143 //Add the right side
1442 result += encoder.encode(rightSide,"RRRR");
145
146 //Add the end bits
1472 result += encoder.endBin;
148
1492 return result;
150 }
151}
152
153
1541function UPC(UPCnumber){
1556 this.ean = new EAN("0"+UPCnumber);
156
1576 this.getText = function(){
1581 return this.ean.getText().substring(1);
159 }
160
1616 this.valid = function(){
1624 return this.ean.valid();
163 }
164
1656 this.encoded = function(){
1661 return this.ean.encoded();
167 }
168
169}
170
171//
172// Help class that does all the encoding
173//
1741function EANencoder(){
175 //The start bits
1766 this.startBin = "101";
177 //The end bits
1786 this.endBin = "101";
179 //The middle bits
1806 this.middleBin = "01010";
181
182 //The L (left) type of encoding
1836 var Lbinary = {
184 0: "0001101",
185 1: "0011001",
186 2: "0010011",
187 3: "0111101",
188 4: "0100011",
189 5: "0110001",
190 6: "0101111",
191 7: "0111011",
192 8: "0110111",
193 9: "0001011"};
194
195 //The G type of encoding
1966 var Gbinary = {
197 0: "0100111",
198 1: "0110011",
199 2: "0011011",
200 3: "0100001",
201 4: "0011101",
202 5: "0111001",
203 6: "0000101",
204 7: "0010001",
205 8: "0001001",
206 9: "0010111"};
207
208 //The R (right) type of encoding
2096 var Rbinary = {
210 0: "1110010",
211 1: "1100110",
212 2: "1101100",
213 3: "1000010",
214 4: "1011100",
215 5: "1001110",
216 6: "1010000",
217 7: "1000100",
218 8: "1001000",
219 9: "1110100"};
220
221 //The left side structure in EAN-13
2226 var EANstructure = {
223 0: "LLLLLL",
224 1: "LLGLGG",
225 2: "LLGGLG",
226 3: "LLGGGL",
227 4: "LGLLGG",
228 5: "LGGLLG",
229 6: "LGGGLL",
230 7: "LGLGLG",
231 8: "LGLGGL",
232 9: "LGGLGL"}
233
2346 this.getEANstructure = function(number){
2354 return EANstructure[number[0]];
236 };
237
238 //Convert a numberarray to the representing
2396 this.encode = function(number,structure){
240 //Create the variable that should be returned at the end of the function
24112 var result = "";
242
243 //Loop all the numbers
24412 for(var i = 0;i<number.length;i++){
245 //Using the L, G or R encoding and add it to the returning variable
24668 if(structure[i]=="L"){
24723 result += Lbinary[number[i]];
248 }
24945 else if(structure[i]=="G"){
2509 result += Gbinary[number[i]];
251 }
25236 else if(structure[i]=="R"){
25332 result += Rbinary[number[i]];
254 }
255 }
25612 return result;
257 };
258}
259
260
261//Required to register for both browser and nodejs
2621var register = function(core){
2631 core.register(EAN, /^EAN(.?13)?$/i, 8);
2641 core.register(EAN8, /^EAN.?8$/i, 8);
2651 core.register(UPC, /^UPC(.?A)?$/i, 8);
266}
2672try{register(JsBarcode)} catch(e){}
2682try{module.exports.register = register} catch(e){}
269

/home/johan/DevZone/JsBarcode/barcodes/ITF.js

100%
31
31
0
LineHitsSource
11function ITF(ITFNumber){
2
36 this.ITFNumber = ITFNumber+"";
4
56 this.getText = function(){
61 return this.ITFNumber;
7 };
8
96 this.valid = function(){
104 return valid(this.ITFNumber);
11 };
12
136 this.encoded = function(){
14 //Create the variable that should be returned at the end of the function
151 var result = "";
16
17 //Always add the same start bits
181 result += startBin;
19
20 //Calculate all the digit pairs
211 for(var i=0;i<this.ITFNumber.length;i+=2){
223 result += calculatePair(this.ITFNumber.substr(i,2));
23 }
24
25 //Always add the same end bits
261 result += endBin;
27
281 return result;
29 }
30
31 //The structure for the all digits, 1 is wide and 0 is narrow
326 var digitStructure = {
33 "0":"00110"
34 ,"1":"10001"
35 ,"2":"01001"
36 ,"3":"11000"
37 ,"4":"00101"
38 ,"5":"10100"
39 ,"6":"01100"
40 ,"7":"00011"
41 ,"8":"10010"
42 ,"9":"01010"}
43
44 //The start bits
456 var startBin = "1010";
46 //The end bits
476 var endBin = "11101";
48
49 //Regexp for a valid Inter25 code
506 var regexp = /^([0-9][0-9])+$/;
51
52 //Calculate the data of a number pair
536 function calculatePair(twoNumbers){
543 var result = "";
55
563 var number1Struct = digitStructure[twoNumbers[0]];
573 var number2Struct = digitStructure[twoNumbers[1]];
58
59 //Take every second bit and add to the result
603 for(var i=0;i<5;i++){
6115 result += (number1Struct[i]=="1") ? "111" : "1";
6215 result += (number2Struct[i]=="1") ? "000" : "0";
63 }
643 return result;
65 }
66
676 function valid(number){
684 return number.search(regexp)!==-1;
69 }
70}
71
72//Required to register for both browser and nodejs
731var register = function(core){
741 core.register(ITF, /^ITF$/i, 4);
75};
762try{register(JsBarcode)} catch(e){}
772try{module.exports.register = register} catch(e){}
78

/home/johan/DevZone/JsBarcode/barcodes/ITF14.js

100%
41
41
0
LineHitsSource
11function ITF14(string){
29 this.string = string+"";
3}
4
51ITF14.prototype.getText = function(){
61 return this.string;
7};
8
91ITF14.prototype.valid = function(){
106 return valid(this.string);
11};
12
131ITF14.prototype.encoded = function(){
14 //Create the variable that should be returned at the end of the function
152 var result = "";
16
17 //If checksum is not already calculated, do it
182 if(this.string.length == 13){
191 this.string += checksum(this.string);
20 }
21
22 //Always add the same start bits
232 result += startBin;
24
25 //Calculate all the digit pairs
262 for(var i=0;i<14;i+=2){
2714 result += calculatePair(this.string.substr(i,2));
28 }
29
30 //Always add the same end bits
312 result += endBin;
32
332 return result;
34};
35
36//The structure for the all digits, 1 is wide and 0 is narrow
371var digitStructure = {
38 "0":"00110"
39,"1":"10001"
40,"2":"01001"
41,"3":"11000"
42,"4":"00101"
43,"5":"10100"
44,"6":"01100"
45,"7":"00011"
46,"8":"10010"
47,"9":"01010"}
48
49//The start bits
501var startBin = "1010";
51//The end bits
521var endBin = "11101";
53
54//Regexp for a valid ITF14 code
551var regexp = /^[0-9]{13,14}$/;
56
57//Calculate the data of a number pair
581function calculatePair(twoNumbers){
5914 var result = "";
60
6114 var number1Struct = digitStructure[twoNumbers[0]];
6214 var number2Struct = digitStructure[twoNumbers[1]];
63
64 //Take every second bit and add to the result
6514 for(var i=0;i<5;i++){
6670 result += (number1Struct[i]=="1") ? "111" : "1";
6770 result += (number2Struct[i]=="1") ? "000" : "0";
68 }
6914 return result;
70}
71
72//Calulate the checksum digit
731function checksum(numberString){
743 var result = 0;
75
7642 for(var i=0;i<13;i++){result+=parseInt(numberString[i])*(3-(i%2)*2)}
77
783 return 10 - (result % 10);
79}
80
811function valid(number){
826 if(number.search(regexp)==-1){
833 return false;
84 }
85 //Check checksum if it is already calculated
863 else if(number.length==14){
872 return number[13] == checksum(number);
88 }
891 return true;
90}
91
92//Required to register for both browser and nodejs
931var register = function(core){
941 core.register(ITF14, /^ITF.?14$/i, 5);
95}
962try{register(JsBarcode)} catch(e){}
972try{module.exports.register = register} catch(e){}
98

/home/johan/DevZone/JsBarcode/barcodes/MSI.js

100%
63
63
0
LineHitsSource
11var prototype = {};
2
31prototype.getText = function(){
411 return this.string;
5};
6
71prototype.encoded = function(){
82 var ret = "110";
9
102 for(var i=0;i<this.string.length;i++){
1116 var digit = parseInt(this.string[i]);
1216 var bin = digit.toString(2);
1316 bin = addZeroes(bin, 4-bin.length);
1416 for(var b=0;b<bin.length;b++){
1564 ret += bin[b]==0 ? "100" : "110";
16 }
17 }
18
192 ret += "1001";
202 return ret;
21};
22
231prototype.valid = function(){
247 return this.string.search(/^[0-9]+$/) != -1;
25};
26
271function MSI(string){
285 this.string = ""+string;
29}
30
311MSI.prototype = Object.create(prototype);
32
331function MSI10(string){
343 this.string = ""+string;
353 this.string += mod10(this.string);
36}
371MSI10.prototype = Object.create(prototype);
38
391function MSI11(string){
404 this.string = "" + string;
414 this.string += mod11(this.string);
42}
431MSI11.prototype = Object.create(prototype);
44
451function MSI1010(string){
462 this.string = "" + string;
472 this.string += mod10(this.string);
482 this.string += mod10(this.string);
49}
501MSI1010.prototype = Object.create(prototype);
51
521function MSI1110(string){
532 this.string = "" + string;
542 this.string += mod11(this.string);
552 this.string += mod10(this.string);
56}
571MSI1110.prototype = Object.create(prototype);
58
591function mod10(number){
609 var sum = 0;
619 for(var i=0;i<number.length;i++){
6254 var n = parseInt(number[i]);
6354 if((i + number.length) % 2 == 0){
6424 sum += n;
65 }
66 else{
6730 sum += (n*2)%10 + Math.floor((n*2)/10)
68 }
69 }
709 return (10-(sum%10))%10;
71}
72
731function mod11(number){
746 var sum = 0;
756 var weights = [2,3,4,5,6,7];
766 for(var i=0;i<number.length;i++){
7746 var n = parseInt(number[number.length-1-i]);
7846 sum += weights[i % weights.length] * n;
79 }
806 return (11-(sum%11))%11;
81}
82
831function addZeroes(number, n){
8416 for(var i=0;i<n;i++){
8524 number = "0"+number;
86 }
8716 return number;
88}
89
90//Required to register for both browser and nodejs
911var register = function(core){
921 core.register(MSI, /^MSI$/i, 4);
931 core.register(MSI10, /^MSI.?10$/i);
941 core.register(MSI11, /^MSI.?11$/i);
951 core.register(MSI1010, /^MSI.?1010$/i);
961 core.register(MSI1110, /^MSI.?1110$/i);
97}
982try{register(JsBarcode)} catch(e){}
992try{module.exports.register = register} catch(e){}
100

/home/johan/DevZone/JsBarcode/barcodes/pharmacode.js

100%
32
32
0
LineHitsSource
11function pharmacode(number){
2 //Ensure that the input is inturpreted as a number
36 this.number = parseInt(number);
4
56 this.getText = function(){
61 return this.number + "";
7 };
8
96 function recursiveEncoding(code,state){
10 //End condition
1121 if(code.length == 0) return "";
12
1315 var generated;
1415 var nextState = false;
1515 var nZeros = zeros(code);
1615 if(nZeros == 0){
177 generated = state ? "001" : "00111";
187 nextState = state;
19 }
20 else{
218 generated = "001".repeat(nZeros - (state ? 1 : 0));
228 generated += "00111";
23 }
2415 return recursiveEncoding(code.substr(0,code.length - nZeros - 1),nextState) + generated;
25 };
26
276 this.encoded = function(){
283 return recursiveEncoding(this.number.toString(2),true).substr(2);
29 };
30
316 this.valid = function(){
322 return this.number >= 3 && this.number <= 131070;
33 };
34
35 //A help function to calculate the zeros at the end of a string (the code)
366 var zeros = function(code){
3715 var i = code.length - 1;
3815 var zeros = 0;
3915 while(code[i]=="0" || i<0){
4013 zeros++;
4113 i--;
42 }
4315 return zeros;
44 };
45
46 //http://stackoverflow.com/a/202627
476 String.prototype.repeat = function( num )
48 {
498 return new Array( num + 1 ).join( this );
50 }
51};
52
53//Required to register for both browser and nodejs
541var register = function(core){
551 core.register(pharmacode, /^pharmacode$/i, 2);
56}
572try{register(JsBarcode)} catch(e){}
582try{module.exports.register = register} catch(e){}
59