Skip to content

Commit 7c2863a

Browse files
author
Alex Tessmer
committed
Move the CODE93 class out of index.js
Also move the helper methods to static class methods
1 parent 1061af5 commit 7c2863a

2 files changed

Lines changed: 74 additions & 71 deletions

File tree

src/barcodes/CODE93/CODE93.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Encoding documentation:
2+
// https://en.wikipedia.org/wiki/Code_93#Detailed_outline
3+
4+
import { SYMBOLS, BINARIES } from './constants';
5+
import Barcode from "../Barcode.js";
6+
7+
class CODE93 extends Barcode {
8+
constructor(data, options){
9+
data = data.toUpperCase();
10+
11+
super(data, options);
12+
}
13+
14+
valid(){
15+
return /^[0-9A-Z\-. $/+%]+$/.test(this.data);
16+
}
17+
18+
encode(){
19+
const symbols = this.data.split('');
20+
const encoded = symbols
21+
.map(s => CODE93.getEncoding(s))
22+
.join('');
23+
24+
// Compute checksum symbols
25+
const csumC = CODE93.checksum(symbols, 20);
26+
const csumK = CODE93.checksum(symbols.concat(csumC), 15);
27+
28+
return {
29+
text: this.text,
30+
data:
31+
// Add the start bits
32+
CODE93.getEncoding('*') +
33+
// Add the encoded bits
34+
encoded +
35+
// Add the checksum
36+
CODE93.getEncoding(csumC) + CODE93.getEncoding(csumK) +
37+
// Add the stop bits
38+
CODE93.getEncoding('*') +
39+
// Add the termination bit
40+
'1'
41+
};
42+
}
43+
44+
// Get the binary encoding of a symbol
45+
static getEncoding(symbol) {
46+
return BINARIES[CODE93.symbolValue(symbol)];
47+
}
48+
49+
// Get the symbol for a symbol value
50+
static getSymbol(symbolValue) {
51+
return SYMBOLS[symbolValue];
52+
}
53+
54+
// Get the symbol value of a symbol
55+
static symbolValue(symbol) {
56+
return SYMBOLS.indexOf(symbol);
57+
}
58+
59+
// Calculate a checksum symbol
60+
static checksum(symbols, maxWeight) {
61+
const csum = symbols
62+
.slice()
63+
.reverse()
64+
.reduce((sum, symbol, idx) => {
65+
const weight = (idx % maxWeight) + 1;
66+
return sum + (CODE93.symbolValue(symbol) * weight);
67+
}, 0);
68+
69+
return CODE93.getSymbol(csum % 47);
70+
}
71+
}
72+
73+
export default CODE93;

src/barcodes/CODE93/index.js

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,3 @@
1-
// Encoding documentation:
2-
// https://en.wikipedia.org/wiki/Code_93#Detailed_outline
3-
4-
import { SYMBOLS, BINARIES } from './constants';
5-
import Barcode from "../Barcode.js";
6-
7-
class CODE93 extends Barcode {
8-
constructor(data, options){
9-
data = data.toUpperCase();
10-
11-
super(data, options);
12-
}
13-
14-
valid(){
15-
return /^[0-9A-Z\-. $/+%]+$/.test(this.data);
16-
}
17-
18-
encode(){
19-
const symbols = this.data.split('');
20-
const encoded = symbols
21-
.map(s => getEncoding(s))
22-
.join('');
23-
24-
// Compute checksum symbols
25-
const csumC = checksum(symbols, 20);
26-
const csumK = checksum(symbols.concat(csumC), 15);
27-
28-
return {
29-
text: this.text,
30-
data:
31-
// Add the start bits
32-
getEncoding('*') +
33-
// Add the encoded bits
34-
encoded +
35-
// Add the checksum
36-
getEncoding(csumC) + getEncoding(csumK) +
37-
// Add the stop bits
38-
getEncoding('*') +
39-
// Add the termination bit
40-
'1'
41-
};
42-
}
43-
}
44-
45-
// Get the binary encoding of a symbol
46-
const getEncoding = (symbol) => {
47-
return BINARIES[symbolValue(symbol)];
48-
};
49-
50-
// Get the symbol for a symbol value
51-
const getSymbol = (symbolValue) => {
52-
return SYMBOLS[symbolValue];
53-
};
54-
55-
// Get the symbol value of a symbol
56-
const symbolValue = (symbol) => {
57-
return SYMBOLS.indexOf(symbol);
58-
};
59-
60-
// Calculate a checksum symbol
61-
const checksum = (symbols, maxWeight) => {
62-
const csum = symbols
63-
.slice()
64-
.reverse()
65-
.reduce((sum, symbol, idx) => {
66-
const weight = (idx % maxWeight) + 1;
67-
return sum + (symbolValue(symbol) * weight);
68-
}, 0);
69-
70-
return getSymbol(csum % 47);
71-
};
1+
import CODE93 from './CODE93.js';
722

733
export {CODE93};

0 commit comments

Comments
 (0)