| Lines of Code | 106 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | class EANencoder{ |
||
| 2 | constructor(){ |
||
| 3 | // Standard start end and middle bits |
||
| 4 | this.startBin = "101"; |
||
| 5 | this.endBin = "101"; |
||
| 6 | this.middleBin = "01010"; |
||
| 7 | |||
| 8 | this.binaries = { |
||
| 9 | // The L (left) type of encoding |
||
| 10 | "L": [ |
||
| 11 | "0001101", |
||
| 12 | "0011001", |
||
| 13 | "0010011", |
||
| 14 | "0111101", |
||
| 15 | "0100011", |
||
| 16 | "0110001", |
||
| 17 | "0101111", |
||
| 18 | "0111011", |
||
| 19 | "0110111", |
||
| 20 | "0001011" |
||
| 21 | ], |
||
| 22 | |||
| 23 | // The G type of encoding |
||
| 24 | "G": [ |
||
| 25 | "0100111", |
||
| 26 | "0110011", |
||
| 27 | "0011011", |
||
| 28 | "0100001", |
||
| 29 | "0011101", |
||
| 30 | "0111001", |
||
| 31 | "0000101", |
||
| 32 | "0010001", |
||
| 33 | "0001001", |
||
| 34 | "0010111" |
||
| 35 | ], |
||
| 36 | |||
| 37 | // The R (right) type of encoding |
||
| 38 | "R": [ |
||
| 39 | "1110010", |
||
| 40 | "1100110", |
||
| 41 | "1101100", |
||
| 42 | "1000010", |
||
| 43 | "1011100", |
||
| 44 | "1001110", |
||
| 45 | "1010000", |
||
| 46 | "1000100", |
||
| 47 | "1001000", |
||
| 48 | "1110100" |
||
| 49 | ], |
||
| 50 | |||
| 51 | // The O (odd) encoding for UPC-E |
||
| 52 | "O": [ |
||
| 53 | "0001101", |
||
| 54 | "0011001", |
||
| 55 | "0010011", |
||
| 56 | "0111101", |
||
| 57 | "0100011", |
||
| 58 | "0110001", |
||
| 59 | "0101111", |
||
| 60 | "0111011", |
||
| 61 | "0110111", |
||
| 62 | "0001011" |
||
| 63 | ], |
||
| 64 | |||
| 65 | // The E (even) encoding for UPC-E |
||
| 66 | "E": [ |
||
| 67 | "0100111", |
||
| 68 | "0110011", |
||
| 69 | "0011011", |
||
| 70 | "0100001", |
||
| 71 | "0011101", |
||
| 72 | "0111001", |
||
| 73 | "0000101", |
||
| 74 | "0010001", |
||
| 75 | "0001001", |
||
| 76 | "0010111" |
||
| 77 | ] |
||
| 78 | }; |
||
| 79 | } |
||
| 80 | |||
| 81 | // Convert a numberarray to the representing |
||
| 82 | encode(number, structure, separator){ |
||
| 83 | // Create the variable that should be returned at the end of the function |
||
| 84 | var result = ""; |
||
| 85 | |||
| 86 | // Make sure that the separator is set |
||
| 87 | separator = separator || ""; |
||
| 88 | |||
| 89 | // Loop all the numbers |
||
| 90 | for(var i = 0; i < number.length; i++){ |
||
| 91 | // Using the L, G or R encoding and add it to the returning variable |
||
| 92 | let binary = this.binaries[structure[i]]; |
||
| 93 | if (binary) { |
||
| 94 | result += binary[number[i]]; |
||
| 95 | } |
||
| 96 | |||
| 97 | // Add separator in between encodings |
||
| 98 | if(i < number.length - 1){ |
||
| 99 | result += separator; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | return result; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | export default EANencoder; |
||
| 107 |