src/barcodes/codabar/index.js   A
last analyzed

Size

Lines of Code 60

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A index.js ➔ ??? 0 9 2
1
// Encoding specification:
2
// http://www.barcodeisland.com/codabar.phtml
3
4
import Barcode from "../Barcode.js";
5
6
class codabar extends Barcode{
7
	constructor(data, options){
8
		if (data.search(/^[0-9\-\$\:\.\+\/]+$/) === 0) {
9
			data = "A" + data + "A";
10
		}
11
12
		super(data.toUpperCase(), options);
13
14
		this.text = this.options.text || this.text.replace(/[A-D]/g, '');
15
	}
16
17
	valid(){
18
		return this.data.search(/^[A-D][0-9\-\$\:\.\+\/]+[A-D]$/) !== -1;
19
	}
20
21
	encode(){
22
		var result = [];
23
		var encodings = this.getEncodings();
24
		for(var i = 0; i < this.data.length; i++){
25
			result.push(encodings[this.data.charAt(i)]);
26
			// for all characters except the last, append a narrow-space ("0")
27
			if (i !== this.data.length - 1) {
28
				result.push("0");
29
			}
30
		}
31
		return {
32
			text: this.text,
33
			data: result.join('')
34
		};
35
	}
36
37
	getEncodings(){
38
		return {
39
			"0": "101010011",
40
			"1": "101011001",
41
			"2": "101001011",
42
			"3": "110010101",
43
			"4": "101101001",
44
			"5": "110101001",
45
			"6": "100101011",
46
			"7": "100101101",
47
			"8": "100110101",
48
			"9": "110100101",
49
			"-": "101001101",
50
			"$": "101100101",
51
			":": "1101011011",
52
			"/": "1101101011",
53
			".": "1101101101",
54
			"+": "101100110011",
55
			"A": "1011001001",
56
			"B": "1001001011",
57
			"C": "1010010011",
58
			"D": "1010011001"
59
		};
60
	}
61
}
62
63
export {codabar};
64