Completed
Push — master ( e298b3...c31ed7 )
by Johan
9s
created

src/barcodes/ITF/index.js   A

Size

Lines of Code 58

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
nc 1
dl 0
loc 58
rs 10
c 1
b 0
f 1
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A index.js ➔ ??? 0 16 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