Coverage

96%
492
475
17

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

87%
133
116
17
LineHitsSource
11(function(){
2
3 // Main function, calls drawCanvas(...) in the right way
41 var JsBarcode = function(image, content, options, validFunction){
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, validFunction);
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, validFunction);
140 image.setAttribute("src", canvas.toDataURL());
15 }
16 // If canvas, just draw
1720 else if(image.getContext){
1819 drawCanvas(image, content, options, validFunction);
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, validFunction) {
27
28 // This tries to call the valid function only if it's specified. Otherwise nothing happens
2919 var validFunctionIfExist = function(valid){
3018 if(validFunction){
312 validFunction(valid);
32 }
33 };
34
35 // Merge the user options with the default
3619 options = merge(JsBarcode.defaults, options);
37
38 // Fix the margins
3919 options.marginTop = options.marginTop | options.margin;
4019 options.marginBottom = options.marginBottom | options.margin;
4119 options.marginRight = options.marginRight | options.margin;
4219 options.marginLeft = options.marginLeft | options.margin;
43
44 //Abort if the browser does not support HTML5 canvas
4519 if (!canvas.getContext) {
460 throw new Error('The browser does not support canvas.');
47 }
48
49 // Automatically choose barcode if format set to "auto"...
5019 if(options.format == "auto"){
513 var encoder = new (JsBarcode.autoSelectEncoder(content))(content);
52 }
53 // ...or else, get by name
54 else{
5516 var encoder = new (JsBarcode.getModule(options.format))(content);
56 }
57
58 //Abort if the barcode format does not support the content
5918 if(!encoder.valid()){
602 validFunctionIfExist(false);
612 if(!validFunction){
621 throw new Error('The data is not valid for the type of barcode.');
63 }
641 return;
65 }
66
67 // Set the binary to a cached version if possible
6816 var cachedBinary = JsBarcode.getCache(options.format, content);
6916 if(cachedBinary){
709 var binary = cachedBinary;
71 }
72 else{
73 // Encode the content
747 var binary = encoder.encoded();
75 // Cache the encoding if it will be used again later
767 JsBarcode.cache(options.format, content, binary);
77 }
78
79 // Get the canvas context
8016 var ctx = canvas.getContext("2d");
81
82 // Set font
8316 var font = options.fontOptions + " " + options.fontSize + "px "+options.font;
8416 ctx.font = font;
85
86 // Set the width and height of the barcode
8716 var width = binary.length*options.width;
88 // Replace with width of the text if it is wider then the barcode
8916 var textWidth = ctx.measureText(encoder.getText()).width;
9016 if(options.displayValue && width < textWidth){
910 if(options.textAlign == "center"){
920 var barcodePadding = Math.floor((textWidth - width)/2);
93 }
940 else if(options.textAlign == "left"){
950 var barcodePadding = 0;
96 }
970 else if(options.textAlign == "right"){
980 var barcodePadding = Math.floor(textWidth - width);
99 }
100
1010 width = textWidth;
102 }
103 // Make sure barcodePadding is not undefined
10416 var barcodePadding = barcodePadding || 0;
105
10616 canvas.width = width + options.marginLeft + options.marginRight;
107
108 // Set extra height if the value is displayed under the barcode. Multiplication with 1.3 t0 ensure that some
109 //characters are not cut in half
11016 canvas.height = options.height
111 + (options.displayValue ? options.fontSize : 0)
112 + options.textMargin
113 + options.marginTop
114 + options.marginBottom;
115
116 // Paint the canvas
11716 ctx.clearRect(0,0,canvas.width,canvas.height);
11816 if(options.background){
11916 ctx.fillStyle = options.background;
12016 ctx.fillRect(0,0,canvas.width, canvas.height);
121 }
122
123 // Creates the barcode out of the encoded binary
12416 ctx.fillStyle = options.lineColor;
12516 for(var i=0;i<binary.length;i++){
1261538 var x = i*options.width + options.marginLeft + barcodePadding;
1271538 if(binary[i] == "1"){
128730 ctx.fillRect(x, options.marginTop, options.width, options.height);
129 }
130 }
131
132 // Draw the text if displayValue is set
13316 if(options.displayValue){
13415 var x, y;
135
13615 y = options.height + options.textMargin + options.marginTop;
137
13815 ctx.font = font;
13915 ctx.textBaseline = "bottom";
14015 ctx.textBaseline = 'top';
141
142 // Draw the text in the correct X depending on the textAlign option
14315 if(options.textAlign == "left" || barcodePadding > 0){
1441 x = options.marginLeft;
1451 ctx.textAlign = 'left';
146 }
14714 else if(options.textAlign == "right"){
1481 x = canvas.width - options.marginRight;
1491 ctx.textAlign = 'right';
150 }
151 //In all other cases, center the text
152 else{
15313 x = canvas.width / 2;
15413 ctx.textAlign = 'center';
155 }
156
15715 ctx.fillText(encoder.getText(), x, y);
158 }
159
160 // Send a confirmation that the generation was successful to the valid function if it does exist
16116 validFunctionIfExist(true);
162 };
163
1641 JsBarcode._modules = [];
165
166 // Add a new module sorted in the array
1671 JsBarcode.register = function(module, regex, priority){
16815 var position = 0;
16915 if(typeof priority === "undefined"){
1704 position = JsBarcode._modules.length - 1;
171 }
172 else{
17311 for(var i=0;i<JsBarcode._modules.length;i++){
17432 position = i;
17532 if(!(priority < JsBarcode._modules[i].priority)){
1769 break;
177 }
178 }
179 }
180
181 // Add the module in position position
18215 JsBarcode._modules.splice(position, 0, {
183 "regex": regex,
184 "module": module,
185 "priority": priority
186 });
187 };
188
189 // Get module by name
1901 JsBarcode.getModule = function(name){
19131 for(var i in JsBarcode._modules){
192312 if(name.search(JsBarcode._modules[i].regex) !== -1){
19330 return JsBarcode._modules[i].module;
194 }
195 }
1961 throw new Error('Module ' + name + ' does not exist or is not loaded.');
197 };
198
199 // If any format is valid with the content, return the format with highest priority
2001 JsBarcode.autoSelectEncoder = function(content){
2013 for(var i in JsBarcode._modules){
20217 var barcode = new (JsBarcode._modules[i].module)(content);
20317 if(barcode.valid(content)){
2043 return JsBarcode._modules[i].module;
205 }
206 }
2070 throw new Error("Can't automatically find a barcode format matching the string '" + content + "'");
208 };
209
210 // Defining the cache dictionary
2111 JsBarcode._cache = {};
212
213 // Cache a regerated barcode
2141 JsBarcode.cache = function(format, input, output){
2157 if(!JsBarcode._cache[format]){
2164 JsBarcode._cache[format] = {};
217 }
2187 JsBarcode._cache[format][input] = output;
219 };
220
221 // Get a chached barcode
2221 JsBarcode.getCache = function(format, input){
22316 if(JsBarcode._cache[format]){
22412 if(JsBarcode._cache[format][input]){
2259 return JsBarcode._cache[format][input];
226 }
227 }
2287 return "";
229 };
230
231 // Detect if the code is running under nodejs
2321 JsBarcode._isNode = false;
2331 if (typeof module !== 'undefined' && module.exports) {
2341 module.exports = JsBarcode; // Export to nodejs
2351 JsBarcode._isNode = true;
236
237 //Register all modules in ./barcodes/
2381 var path = require("path");
2391 var dir = path.join(__dirname, "barcodes");
2401 var files = require("fs").readdirSync(dir);
2411 for(var i in files){
2428 var barcode = require(path.join(dir, files[i]));
2438 barcode.register(JsBarcode);
244 }
245 }
246
247 //Regsiter JsBarcode for the browser
2481 if(typeof window !== 'undefined'){
2490 window.JsBarcode = JsBarcode;
250 }
251
252 // Register JsBarcode as an jQuery plugin if jQuery exist
2531 if (typeof jQuery !== 'undefined') {
2540 jQuery.fn.JsBarcode = function(content, options, validFunction){
2550 JsBarcode(this.get(0), content, options, validFunction);
2560 return this;
257 };
258 }
259
260 // All the default options. If one is not set.
2611 JsBarcode.defaults = {
262 width: 2,
263 height: 100,
264 format: "auto",
265 displayValue: true,
266 fontOptions: "",
267 font: "monospace",
268 textAlign: "center",
269 textMargin: 2,
270 fontSize: 14,
271 background: "#fff",
272 lineColor: "#000",
273 margin: 10,
274 marginTop: undefined,
275 marginBottom: undefined,
276 marginLeft: undefined,
277 marginRight: undefined
278 };
279
280 // Function to merge the default options with the default ones
2811 var merge = function(m1, m2) {
28219 var newMerge = {};
28319 for (var k in m1) {
284304 newMerge[k] = m1[k];
285 }
28619 for (var k in m2) {
28724 if(typeof m2[k] !== "undefined"){
28824 newMerge[k] = m2[k];
289 }
290 }
29119 return newMerge;
292 };
293})();
294

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

100%
68
68
0
LineHitsSource
11function CODE128(string, code){
213 code = code || "B";
3
413 this.string = string+"";
5
613 this.valid = valid;
7
813 this.getText = function(){
919 return this.string;
10 };
11
12 //The public encoding function
1313 this.encoded = function(){
143 return calculate["code128" + code](string);
15 }
16
17 //Data for each character, the last characters will not be encoded but are used for error correction
1813 var code128b = [
19 [" ","11011001100",0],
20 ["!","11001101100",1],
21 ["\"","11001100110",2],
22 ["#","10010011000",3],
23 ["$","10010001100",4],
24 ["%","10001001100",5],
25 ["&","10011001000",6],
26 ["'","10011000100",7],
27 ["(","10001100100",8],
28 [")","11001001000",9],
29 ["*","11001000100",10],
30 ["+","11000100100",11],
31 [",","10110011100",12],
32 ["-","10011011100",13],
33 [".","10011001110",14],
34 ["/","10111001100",15],
35 ["0","10011101100",16],
36 ["1","10011100110",17],
37 ["2","11001110010",18],
38 ["3","11001011100",19],
39 ["4","11001001110",20],
40 ["5","11011100100",21],
41 ["6","11001110100",22],
42 ["7","11101101110",23],
43 ["8","11101001100",24],
44 ["9","11100101100",25],
45 [":","11100100110",26],
46 [";","11101100100",27],
47 ["<","11100110100",28],
48 ["=","11100110010",29],
49 [">","11011011000",30],
50 ["?","11011000110",31],
51 ["@","11000110110",32],
52 ["A","10100011000",33],
53 ["B","10001011000",34],
54 ["C","10001000110",35],
55 ["D","10110001000",36],
56 ["E","10001101000",37],
57 ["F","10001100010",38],
58 ["G","11010001000",39],
59 ["H","11000101000",40],
60 ["I","11000100010",41],
61 ["J","10110111000",42],
62 ["K","10110001110",43],
63 ["L","10001101110",44],
64 ["M","10111011000",45],
65 ["N","10111000110",46],
66 ["O","10001110110",47],
67 ["P","11101110110",48],
68 ["Q","11010001110",49],
69 ["R","11000101110",50],
70 ["S","11011101000",51],
71 ["T","11011100010",52],
72 ["U","11011101110",53],
73 ["V","11101011000",54],
74 ["W","11101000110",55],
75 ["X","11100010110",56],
76 ["Y","11101101000",57],
77 ["Z","11101100010",58],
78 ["[","11100011010",59],
79 ["\\","11101111010",60],
80 ["]","11001000010",61],
81 ["^","11110001010",62],
82 ["_","10100110000",63],
83 ["`","10100001100",64],
84 ["a","10010110000",65],
85 ["b","10010000110",66],
86 ["c","10000101100",67],
87 ["d","10000100110",68],
88 ["e","10110010000",69],
89 ["f","10110000100",70],
90 ["g","10011010000",71],
91 ["h","10011000010",72],
92 ["i","10000110100",73],
93 ["j","10000110010",74],
94 ["k","11000010010",75],
95 ["l","11001010000",76],
96 ["m","11110111010",77],
97 ["n","11000010100",78],
98 ["o","10001111010",79],
99 ["p","10100111100",80],
100 ["q","10010111100",81],
101 ["r","10010011110",82],
102 ["s","10111100100",83],
103 ["t","10011110100",84],
104 ["u","10011110010",85],
105 ["v","11110100100",86],
106 ["w","11110010100",87],
107 ["x","11110010010",88],
108 ["y","11011011110",89],
109 ["z","11011110110",90],
110 ["{","11110110110",91],
111 ["|","10101111000",92],
112 ["}","10100011110",93],
113 ["~","10001011110",94],
114 [String.fromCharCode(127),"10111101000",95],
115 [String.fromCharCode(128),"10111100010",96],
116 [String.fromCharCode(129),"11110101000",97],
117 [String.fromCharCode(130),"11110100010",98],
118 [String.fromCharCode(131),"10111011110",99],
119 [String.fromCharCode(132),"10111101110",100],
120 [String.fromCharCode(133),"11101011110",101],
121 [String.fromCharCode(134),"11110101110",102],
122 //Start codes
123 [String.fromCharCode(135),"11010000100",103],
124 [String.fromCharCode(136),"11010010000",104],
125 [String.fromCharCode(137),"11010011100",105]];
126
127 //The end bits
12813 var endBin = "1100011101011";
129
130 //Use the regexp variable for validation
13113 function valid(){
13211 if(this.string.search(/^[!-~ ]+$/)==-1){
1331 return false;
134 }
13510 return true;
136 }
137
138 //The encoder function that return a complete binary string. Data need to be validated before sent to this function
139 //This is general calculate function, which is called by code specific calculate functions
14013 function calculateCode128(string, encodeFn, startCode, checksumFn){
1413 var result = "";
142
143 //Add the start bits
1443 result += encodingById(startCode);
145
146 //Add the encoded bits
1473 result += encodeFn(string);
148
149 //Add the checksum
1503 result += encodingById(checksumFn(string, startCode));
151
152 //Add the end bits
1533 result += endBin;
154
1553 return result;
156 }
157
158 //Code specific calculate functions
15913 var calculate = {
160 code128B: function(string){
1612 return calculateCode128(string, encodeB, 104, checksumB);
162 },
163 code128C: function(string){
1641 string = string.replace(/ /g, "");
1651 return calculateCode128(string, encodeC, 105, checksumC);
166 }
167 }
168
169 //Encode the characters (128 B)
17013 function encodeB(string){
1712 var result = "";
1722 for(var i=0;i<string.length;i++){
17310 result+=encodingByChar(string[i]);
174 }
1752 return result;
176 }
177
178 //Encode the characters (128 C)
17913 function encodeC(string){
1801 var result = "";
1811 for(var i=0;i<string.length;i+=2){
1823 result+=encodingById(parseInt(string.substr(i, 2)));
183 }
1841 return result;
185 }
186
187 //Calculate the checksum (128 B)
18813 function checksumB(string, startCode){
1892 var sum = 0;
1902 for(var i=0;i<string.length;i++){
19110 sum += weightByCharacter(string[i])*(i+1);
192 }
1932 return (sum+startCode) % 103;
194 }
195
196 //Calculate the checksum (128 C)
19713 function checksumC(string, startCode){
1981 var sum = 0;
1991 var w = 1;
2001 for(var i=0;i<string.length;i+=2){
2013 sum += parseInt(string.substr(i, 2))*(w);
2023 w++;
203 }
2041 return (sum+startCode) % 103;
205 }
206
207 //Get the encoded data by the id of the character
20813 function encodingById(id){
2099 for(var i=0;i<code128b.length;i++){
210565 if(code128b[i][2]==id){
2119 return code128b[i][1];
212 }
213 }
214 }
215
216 //Get the id (weight) of a character
21713 function weightByCharacter(character){
21810 for(var i=0;i<code128b.length;i++){
219527 if(code128b[i][0]==character){
22010 return code128b[i][2];
221 }
222 }
223 }
224
225 //Get the encoded data of a character
22613 function encodingByChar(character){
22710 for(var i=0;i<code128b.length;i++){
228527 if(code128b[i][0]==character){
22910 return code128b[i][1];
230 }
231 }
232 }
233}
234
2351function CODE128B(string) {
23612 return new CODE128(string, "B");
237}
2381function CODE128C(string) {
2391 return new CODE128(string, "C");
240};
241
242//Required to register for both browser and nodejs
2431var register = function(core){
2441 core.register(CODE128B, /^CODE128(.?B)?$/i, 2);
2451 core.register(CODE128C, /^CODE128.?C$/i, 2);
246}
2472try{register(JsBarcode)} catch(e){}
2482try{module.exports.register = register} catch(e){}
249

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

100%
25
25
0
LineHitsSource
11function CODE39(string){
29 this.string = string.toUpperCase();
3
49 var code39 = [
5 [0,"0","101000111011101"]
6,[1,"1","111010001010111"]
7,[2,"2","101110001010111"]
8,[3,"3","111011100010101"]
9,[4,"4","101000111010111"]
10,[5,"5","111010001110101"]
11,[6,"6","101110001110101"]
12,[7,"7","101000101110111"]
13,[8,"8","111010001011101"]
14,[9,"9","101110001011101"]
15,[10,"A","111010100010111"]
16,[11,"B","101110100010111"]
17,[12,"C","111011101000101"]
18,[13,"D","101011100010111"]
19,[14,"E","111010111000101"]
20,[15,"F","101110111000101"]
21,[16,"G","101010001110111"]
22,[17,"H","111010100011101"]
23,[18,"I","101110100011101"]
24,[19,"J","101011100011101"]
25,[20,"K","111010101000111"]
26,[21,"L","101110101000111"]
27,[22,"M","111011101010001"]
28,[23,"N","101011101000111"]
29,[24,"O","111010111010001"]
30,[25,"P","101110111010001"]
31,[26,"Q","101010111000111"]
32,[27,"R","111010101110001"]
33,[28,"S","101110101110001"]
34,[29,"T","101011101110001"]
35,[30,"U","111000101010111"]
36,[31,"V","100011101010111"]
37,[32,"W","111000111010101"]
38,[33,"X","100010111010111"]
39,[34,"Y","111000101110101"]
40,[35,"Z","100011101110101"]
41,[36,"-","100010101110111"]
42,[37,".","111000101011101"]
43,[38," ","100011101011101"]
44,[39,"$","100010001000101"]
45,[40,"/","100010001010001"]
46,[41,"+","100010100010001"]
47,[42,"%","101000100010001"]];
48
499 this.getText = function(){
509 return this.string;
51 };
52
539 this.encoded = function(){
545 var result = "";
555 result += "1000101110111010";
565 for(var i=0;i<this.string.length;i++){
5725 result+=encodingByChar(this.string[i])+"0";
58 }
595 result += "1000101110111010";
605 return result;
61 };
62
63 //Use the regexp variable for validation
649 this.valid = function(){
657 if(this.string.search(/^[0-9A-Z\-\.\ \$\/\+\%]+$/)==-1){
661 return false;
67 }
686 return true;
69 }
70
71 //Get the encoded data of a character
729 function encodingByChar(character){
7325 for(var i=0;i<code39.length;i++){
74431 if(code39[i][1]==character){
7524 return code39[i][2];
76 }
77 }
781 return "";
79 }
80}
81
82
83//Required to register for both browser and nodejs
841var register = function(core){
851 core.register(CODE39, /^CODE.?39$/i, 3);
86}
872try{register(JsBarcode)} catch(e){}
882try{module.exports.register = register} catch(e){}
89

/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
34 this.number = parseInt(number);
4
54 this.getText = function(){
61 return this.number + "";
7 };
8
94 function recursiveEncoding(code,state){
10 //End condition
117 if(code.length == 0) return "";
12
135 var generated;
145 var nextState = false;
155 var nZeros = zeros(code);
165 if(nZeros == 0){
171 generated = state ? "001" : "00111";
181 nextState = state;
19 }
20 else{
214 generated = "001".repeat(nZeros - (state ? 1 : 0));
224 generated += "00111";
23 }
245 return recursiveEncoding(code.substr(0,code.length - nZeros - 1),nextState) + generated;
25 };
26
274 this.encoded = function(){
281 return recursiveEncoding(this.number.toString(2),true).substr(2);
29 };
30
314 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)
364 var zeros = function(code){
375 var i = code.length - 1;
385 var zeros = 0;
395 while(code[i]=="0" || i<0){
406 zeros++;
416 i--;
42 }
435 return zeros;
44 };
45
46 //http://stackoverflow.com/a/202627
474 String.prototype.repeat = function( num )
48 {
494 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