| Lines of Code | 58 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | class ITF{ |
||
| 2 | constructor(string){ |
||
| 3 | this.string = string; |
||
| 4 | |||
| 5 | this.binaryRepresentation = { |
||
| 6 | "0":"00110", |
||
| 7 | "1":"10001", |
||
| 8 | "2":"01001", |
||
| 9 | "3":"11000", |
||
| 10 | "4":"00101", |
||
| 11 | "5":"10100", |
||
| 12 | "6":"01100", |
||
| 13 | "7":"00011", |
||
| 14 | "8":"10010", |
||
| 15 | "9":"01010" |
||
| 16 | }; |
||
| 17 | } |
||
| 18 | |||
| 19 | valid(){ |
||
| 20 | return this.string.search(/^([0-9]{2})+$/) !== -1; |
||
| 21 | } |
||
| 22 | |||
| 23 | encode(){ |
||
| 24 | // Always add the same start bits |
||
| 25 | var result = "1010"; |
||
| 26 | |||
| 27 | // Calculate all the digit pairs |
||
| 28 | for(var i = 0; i < this.string.length; i += 2){ |
||
| 29 | result += this.calculatePair(this.string.substr(i, 2)); |
||
| 30 | } |
||
| 31 | |||
| 32 | // Always add the same end bits |
||
| 33 | result += "11101"; |
||
| 34 | |||
| 35 | return { |
||
| 36 | data: result, |
||
| 37 | text: this.string |
||
| 38 | }; |
||
| 39 | } |
||
| 40 | |||
| 41 | // Calculate the data of a number pair |
||
| 42 | calculatePair(numberPair){ |
||
| 43 | var result = ""; |
||
| 44 | |||
| 45 | var number1Struct = this.binaryRepresentation[numberPair[0]]; |
||
| 46 | var number2Struct = this.binaryRepresentation[numberPair[1]]; |
||
| 47 | |||
| 48 | // Take every second bit and add to the result |
||
| 49 | for(var i = 0; i < 5; i++){ |
||
| 50 | result += (number1Struct[i] == "1") ? "111" : "1"; |
||
| 51 | result += (number2Struct[i] == "1") ? "000" : "0"; |
||
| 52 | } |
||
| 53 | |||
| 54 | return result; |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | export {ITF}; |
||
| 59 |