| Lines of Code | 53 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | // Encoding specification: |
||
| 4 | class codabar{ |
||
| 5 | constructor(string){ |
||
| 6 | this.string = string.toUpperCase(); |
||
| 7 | |||
| 8 | if (this.string.search(/^[0-9\-\$\:\.\+\/]+$/) === 0) { |
||
| 9 | this.string = "A" + this.string + "A"; |
||
| 10 | } |
||
| 11 | |||
| 12 | this.encodings = { |
||
| 13 | "0": "101010011", |
||
| 14 | "1": "101011001", |
||
| 15 | "2": "101001011", |
||
| 16 | "3": "110010101", |
||
| 17 | "4": "101101001", |
||
| 18 | "5": "110101001", |
||
| 19 | "6": "100101011", |
||
| 20 | "7": "100101101", |
||
| 21 | "8": "100110101", |
||
| 22 | "9": "110100101", |
||
| 23 | "-": "101001101", |
||
| 24 | "$": "101100101", |
||
| 25 | ":": "1101011011", |
||
| 26 | "/": "1101101011", |
||
| 27 | ".": "1101101101", |
||
| 28 | "+": "101100110011", |
||
| 29 | "A": "1011001001", |
||
| 30 | "B": "1010010011", |
||
| 31 | "C": "1001001011", |
||
| 32 | "D": "1010011001" |
||
| 33 | }; |
||
| 34 | } |
||
| 35 | |||
| 36 | valid(){ |
||
| 37 | return this.string.search(/^[A-D][0-9\-\$\:\.\+\/]+[A-D]$/) !== -1; |
||
| 38 | } |
||
| 39 | |||
| 40 | encode(){ |
||
| 41 | var result = []; |
||
| 42 | for(var i = 0; i < this.string.length; i++){ |
||
| 43 | result.push(this.encodings[this.string.charAt(i)]); |
||
| 44 | // for all characters except the last, append a narrow-space ("0") |
||
| 45 | if (i !== this.string.length - 1) { |
||
| 46 | result.push("0"); |
||
| 47 | } |
||
| 48 | } |
||
| 49 | return { |
||
| 50 | text: this.string.replace(/[A-D]/g, ''), |
||
| 51 | data: result.join('') |
||
| 52 | }; |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | export {codabar}; |
||
| 57 |