forked from lindell/JsBarcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpharmacode.js
50 lines (44 loc) · 1.34 KB
/
pharmacode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function pharmacode(number){
//Ensure that the input is inturpreted as a number
this.number = parseInt(number);
function recursiveEncoding(code,state){
//End condition
if(code.length == 0) return "";
var generated;
var nextState = false;
var nZeros = zeros(code);
if(nZeros == 0){
generated = state ? "001" : "00111";
nextState = state;
}
else{
generated = "001".repeat(nZeros - (state ? 1 : 0));
generated += "00111";
}
return recursiveEncoding(code.substr(0,code.length - nZeros - 1),nextState) + generated;
};
this.encoded = function(){
if(this.valid(this.number)){
return recursiveEncoding(this.number.toString(2),true).substr(2);
}
return "";
};
this.valid = function(){
return this.number >= 3 && this.number <= 131070;
};
//A help function to calculate the zeros at the end of a string (the code)
var zeros = function(code){
var i = code.length - 1;
var zeros = 0;
while(code[i]=="0" || i<0){
zeros++;
i--;
}
return zeros;
};
//http://stackoverflow.com/a/202627
String.prototype.repeat = function( num )
{
return new Array( num + 1 ).join( this );
}
};