Completed
Pull Request — master (#100)
by Johan
01:13
created

src/barcodes/codabar/index.js   A

Size

Lines of Code 53

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
B index.js ➔ ??? 0 30 2
1
// Encoding specification:
2
// http://www.barcodeisland.com/codabar.phtml
3
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