-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzebra.js
194 lines (167 loc) · 5.16 KB
/
zebra.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
exports.TYPE_EAN13 = 'EAN-13';
exports.TYPE_EAN8 = 'EAN-8';
exports.TYPE_UPCA = 'UPC-A';
exports.TYPE_UPCE = 'UPC-E';
function EAN8(code) {
if (code.length != 8) {
throw 'EAN-8: Invalid code: ' + code;
}
this.code = code;
this.type = exports.TYPE_EAN8;
}
function calcUPCCheck(code) {
var checkDigit = 0;
var odd = true;
for(var i=0; i<code.length; i++) {
if (odd) {
checkDigit += parseInt(code[i])*3;
} else {
checkDigit += parseInt(code[i]);
}
odd = !odd;
}
checkDigit = checkDigit % 10;
checkDigit = 10 - checkDigit;
checkDigit = checkDigit % 10;
return new String(checkDigit);
}
function EAN13(code) {
if (code.length != 13) {
throw 'EAN-13: Invalid code: ' + code;
}
this.code = code;
this.numberSystem = code[0] + code[1];
this.manufacturerCode = code.substring(2, 7);
this.productCode = code.substring(7, 12);
this.checkDigit = code[12];
this.type = exports.TYPE_EAN13;
}
EAN13.prototype.calcCheckDigit = function() {
return calcUPCCheck(this.code);
};
EAN13.prototype.isCheckDigitValid = function() {
return this.checkDigit == this.calcCheckDigit();
};
EAN13.prototype.isUPCA = function() {
return this.code[0] == '0';
};
EAN13.prototype.toUPCA = function() {
if (this.isUPCA()) {
return new UPCA(this.code.substring(1));
} else {
throw 'EAN-13: Not a UPC-A: ' + code;
}
};
function UPCA(code) {
if (code.length != 12) {
throw 'UPC-A: Invalid code: ' + code;
}
var typeMap = {
0: 'REGULAR',
1: 'RESERVED',
2: 'VARIABLE_WEIGHT',
3: 'DRUG_HEALTH',
4: 'IN_STORE_NON_FOOD',
5: 'COUPON',
6: 'REGULAR',
7: 'REGULAR',
8: 'REGULAR',
9: 'REGULAR'
}
this.code = code;
this.numberSystem = code[0];
this.manufacturerCode = code.substring(1, 6);
this.productCode = code.substring(6, 11);
this.checkDigit = code[11];
this.productType = typeMap[this.numberSystem];
this.type = exports.TYPE_UPCA;
}
UPCA.prototype.toEAN13 = function() {
return new EAN13('0' + this.code);
};
UPCA.prototype.calcCheckDigit = function() {
return calcUPCCheck(this.code);
};
UPCA.prototype.isCheckDigitValid = function() {
return this.checkDigit == this.calcCheckDigit();
};
function UPCE(code) {
if (code.length != 8 || code[0] != '0') {
throw 'UPC-E: Invalid code: ' + code;
}
this.code = code;
// UPC-E always starts with 0
this.numberSystem = '0';
/*
* 1.) manufacturer code ends with 000, 100, or 200, the UPC-E code consists
* of the first two characters of the manufacturer code, the last three
* characters of the product code, followed by the third character of the
* manufacturer code. Under this case, The product code must be 00000 and
* 00999.
*
* 2.) manufacturer code ends with 00 but does not meet the #1 requirement,
* the UPC-E code consists of the first three characters of the
* manufacturer code, the last two characters of the product code,
* followed by digit “3”. The product code can only contain two digits
* (00000 to 00099).
*
* 3.) manufacturer code ends in 0 but non of the above qualifies, the UPC-E
* consists of the first four digits manufacturer code and the last digit
* of the product code, followed by the digit “4”. The product code in
* this case can only contain one digit(00000 to 00009).
*
* 4.) manufacturer code ends with non-zero digit, the UPC-E code consists of
* the manufacturer code and the last digit of the product code. In this
* case the product case can only be one from 00005 to 00009 because 0 to
* 4 has been used for the above four cases.
*/
if (code[6] in ['0', '1', '2']) {
this.manufacturerCode = code[1] + code[2] + code[6] + '00';
this.productCode = '00' + code[3] + code[4] + code[5];
} else if (code[6] == '3') {
this.manufacturerCode = code[1] + code[2] + code[3] + '00';
this.productCode = '000' + code[4] + code[5];
} else if (code[6] == '4') {
this.manufacturerCode = code[1] + code[2] + code[3] + code[4] + '0';
this.productCode = '0000' + code[5];
} else {
this.manufacturerCode = code.substring(1, 6);
this.productCode = '0000' + code[6];
}
this.checkDigit = code[7];
this.productType = 'REGULAR';
this.type = exports.TYPE_UPCE;
}
UPCE.prototype.toUPCA = function() {
return new UPCA('0' + this.manufacturerCode + this.productCode + this.checkDigit);
};
UPCE.prototype.toEAN13 = function() {
return this.toUPCA().toEAN13();
};
UPCE.prototype.calcCheckDigit = function() {
return calcUPCCheck(this.code);
};
UPCE.prototype.isCheckDigitValid = function() {
return this.checkDigit == this.calcCheckDigit();
};
exports.EAN8 = EAN8;
exports.EAN13 = EAN13;
exports.UPCA = UPCA;
exports.UPCE = UPCE;
exports.parse = function(code) {
if (!code) {
throw 'most provide code';
} else {
if (code.length == 12) {
return new UPCA(code);
} else if (code.length == 8 && code[0] == '0') {
return new UPCE(code);
} else if (code.length == 13) {
return new EAN13(code);
} else if(code.length == 8 && code[0] != '0') {
return new EAN8(code);
} else {
throw 'invalid code';
}
}
};