src/barcodes/MSI/MSI.js   A
last analyzed

Size

Lines of Code 45

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 45
rs 10
c 0
b 0
f 0
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
MSI.js ➔ addZeroes 0 6 ?
A MSI.js ➔ ??? 0 3 1
1
// Encoding documentation
2
// https://en.wikipedia.org/wiki/MSI_Barcode#Character_set_and_binary_lookup
3
4
import Barcode from "../Barcode.js";
5
6
class MSI extends Barcode{
7
	constructor(data, options){
8
		super(data, options);
9
	}
10
11
	encode(){
12
		// Start bits
13
		var ret = "110";
14
15
		for(var i = 0; i < this.data.length; i++){
16
			// Convert the character to binary (always 4 binary digits)
17
			var digit = parseInt(this.data[i]);
18
			var bin = digit.toString(2);
19
			bin = addZeroes(bin, 4 - bin.length);
20
21
			// Add 100 for every zero and 110 for every 1
22
			for(var b = 0; b < bin.length; b++){
23
				ret += bin[b] == "0" ? "100" : "110";
24
			}
25
		}
26
27
		// End bits
28
		ret += "1001";
29
30
		return {
31
			data: ret,
32
			text: this.text
33
		};
34
	}
35
36
	valid(){
37
		return this.data.search(/^[0-9]+$/) !== -1;
38
	}
39
}
40
41
function addZeroes(number, n){
42
	for(var i = 0; i < n; i++){
43
		number = "0" + number;
44
	}
45
	return number;
46
}
47
48
export default MSI;
49