Completed
Pull Request — master (#99)
by Johan
01:15
created

src/barcodes/EAN_UPC/EAN2.js   A

Size

Lines of Code 33

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A EAN2.js ➔ ??? 0 5 1
1
// Encoding documentation:
2
// https://en.wikipedia.org/wiki/EAN_2#Encoding
3
4
import EANencoder from './ean_encoder.js';
5
6
class EAN2{
7
	constructor(string){
8
		this.string = string;
9
10
		this.structure = ["LL", "LG", "GL", "GG"];
11
	}
12
13
	valid(){
14
		return this.string.search(/^[0-9]{2}$/) !== -1;
15
	}
16
17
	encode(){
18
		var encoder = new EANencoder();
19
20
		// Choose the structure based on the number mod 4
21
		var structure = this.structure[parseInt(this.string) % 4];
22
23
		// Start bits
24
		var result = "1011";
25
26
		// Encode the two digits with 01 in between
27
		result += encoder.encode(this.string, structure, "01");
28
29
		return {
30
			data: result,
31
			text: this.string
32
		};
33
	}
34
}
35
36
export default EAN2;
37