Lines of Code | 40 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | // Encoding documentation |
||
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 |