|
1
|
|
|
// Encoding documentation: |
|
2
|
|
|
// https://en.wikipedia.org/wiki/Code_39#Encoding |
|
3
|
|
|
|
|
4
|
|
|
import Barcode from "../Barcode.js"; |
|
5
|
|
|
|
|
6
|
|
|
class CODE39 extends Barcode { |
|
7
|
|
|
constructor(data, options){ |
|
8
|
|
|
data = data.toUpperCase(); |
|
9
|
|
|
|
|
10
|
|
|
// Calculate mod43 checksum if enabled |
|
11
|
|
|
if(options.mod43){ |
|
12
|
|
|
data += getCharacter(mod43checksum(data)); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
super(data, options); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
encode(){ |
|
19
|
|
|
// First character is always a * |
|
20
|
|
|
var result = getEncoding("*"); |
|
21
|
|
|
|
|
22
|
|
|
// Take every character and add the binary representation to the result |
|
23
|
|
|
for(let i = 0; i < this.data.length; i++){ |
|
24
|
|
|
result += getEncoding(this.data[i]) + "0"; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
// Last character is always a * |
|
28
|
|
|
result += getEncoding("*"); |
|
29
|
|
|
|
|
30
|
|
|
return { |
|
31
|
|
|
data: result, |
|
32
|
|
|
text: this.text |
|
33
|
|
|
}; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
valid(){ |
|
37
|
|
|
return this.data.search(/^[0-9A-Z\-\.\ \$\/\+\%]+$/) !== -1; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
// All characters. The position in the array is the (checksum) value |
|
47
|
|
|
var characters = [ |
|
48
|
|
|
"0", "1", "2", "3", |
|
49
|
|
|
"4", "5", "6", "7", |
|
50
|
|
|
"8", "9", "A", "B", |
|
51
|
|
|
"C", "D", "E", "F", |
|
52
|
|
|
"G", "H", "I", "J", |
|
53
|
|
|
"K", "L", "M", "N", |
|
54
|
|
|
"O", "P", "Q", "R", |
|
55
|
|
|
"S", "T", "U", "V", |
|
56
|
|
|
"W", "X", "Y", "Z", |
|
57
|
|
|
"-", ".", " ", "$", |
|
58
|
|
|
"/", "+", "%", "*" |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
// The decimal representation of the characters, is converted to the |
|
62
|
|
|
// corresponding binary with the getEncoding function |
|
63
|
|
|
var encodings = [ |
|
64
|
|
|
20957, 29783, 23639, 30485, |
|
65
|
|
|
20951, 29813, 23669, 20855, |
|
66
|
|
|
29789, 23645, 29975, 23831, |
|
67
|
|
|
30533, 22295, 30149, 24005, |
|
68
|
|
|
21623, 29981, 23837, 22301, |
|
69
|
|
|
30023, 23879, 30545, 22343, |
|
70
|
|
|
30161, 24017, 21959, 30065, |
|
71
|
|
|
23921, 22385, 29015, 18263, |
|
72
|
|
|
29141, 17879, 29045, 18293, |
|
73
|
|
|
17783, 29021, 18269, 17477, |
|
74
|
|
|
17489, 17681, 20753, 35770 |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
// Get the binary representation of a character by converting the encodings |
|
78
|
|
|
// from decimal to binary |
|
79
|
|
|
function getEncoding(character){ |
|
80
|
|
|
return getBinary(characterValue(character)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
function getBinary(characterValue){ |
|
84
|
|
|
return encodings[characterValue].toString(2); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
function getCharacter(characterValue){ |
|
88
|
|
|
return characters[characterValue]; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
function characterValue(character){ |
|
92
|
|
|
return characters.indexOf(character); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
function mod43checksum(data){ |
|
96
|
|
|
var checksum = 0; |
|
97
|
|
|
for(let i = 0; i < data.length; i++){ |
|
98
|
|
|
checksum += characterValue(data[i]); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
checksum = checksum % 43; |
|
102
|
|
|
return checksum; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
export {CODE39}; |
|
106
|
|
|
|