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

Size

Lines of Code 40

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
A index.js ➔ ??? 0 4 1
1
// Encoding documentation
2
// http://www.gomaro.ch/ftproot/Laetus_PHARMA-CODE.pdf
3
4
import Barcode from "../Barcode.js";
5
6
class pharmacode extends Barcode{
7
	constructor(data, options){
8
		super(data, options);
9
		this.number = parseInt(data, 10);
10
	}
11
12
	encode(){
13
		var z = this.number;
14
		var result = "";
15
16
		// http://i.imgur.com/RMm4UDJ.png
17
		// (source: http://www.gomaro.ch/ftproot/Laetus_PHARMA-CODE.pdf, page: 34)
18
		while(!isNaN(z) && z != 0){
19
			if(z % 2 === 0){ // Even
20
				result = "11100" + result;
21
				z = (z - 2) / 2;
22
			}
23
			else{ // Odd
24
				result = "100" + result;
25
				z = (z - 1) / 2;
26
			}
27
		}
28
29
		// Remove the two last zeroes
30
		result = result.slice(0, -2);
31
32
		return {
33
			data: result,
34
			text: this.text
35
		};
36
	}
37
38
	valid(){
39
		return this.number >= 3 && this.number <= 131070;
40
	}
41
}
42
43
export {pharmacode};
44