forked from lindell/JsBarcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request lindell#226 from SanichKotikov/refactor-ean
Refactor EAN
- Loading branch information
Showing
9 changed files
with
234 additions
and
350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,30 @@ | ||
// Encoding documentation: | ||
// https://en.wikipedia.org/wiki/EAN_2#Encoding | ||
|
||
import EANencoder from './ean_encoder.js'; | ||
import Barcode from "../Barcode.js"; | ||
import { EAN2_STRUCTURE } from './constants'; | ||
import encode from './encoder'; | ||
import Barcode from '../Barcode'; | ||
|
||
class EAN2 extends Barcode{ | ||
constructor(data, options){ | ||
super(data, options); | ||
class EAN2 extends Barcode { | ||
|
||
this.structure = ["LL", "LG", "GL", "GG"]; | ||
constructor(data, options) { | ||
super(data, options); | ||
} | ||
|
||
valid(){ | ||
valid() { | ||
return this.data.search(/^[0-9]{2}$/) !== -1; | ||
} | ||
|
||
encode(){ | ||
var encoder = new EANencoder(); | ||
|
||
// Choose the structure based on the number mod 4 | ||
var structure = this.structure[parseInt(this.data) % 4]; | ||
|
||
// Start bits | ||
var result = "1011"; | ||
|
||
// Encode the two digits with 01 in between | ||
result += encoder.encode(this.data, structure, "01"); | ||
|
||
const structure = EAN2_STRUCTURE[parseInt(this.data) % 4]; | ||
return { | ||
data: result, | ||
// Start bits + Encode the two digits with 01 in between | ||
data: '1011' + encode(this.data, structure, '01'), | ||
text: this.text | ||
}; | ||
} | ||
|
||
} | ||
|
||
export default EAN2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,40 @@ | ||
// Encoding documentation: | ||
// https://en.wikipedia.org/wiki/EAN_5#Encoding | ||
|
||
import EANencoder from './ean_encoder.js'; | ||
import Barcode from "../Barcode.js"; | ||
|
||
class EAN5 extends Barcode{ | ||
constructor(data, options){ | ||
import { EAN5_STRUCTURE } from './constants'; | ||
import encode from './encoder'; | ||
import Barcode from '../Barcode'; | ||
|
||
const checksum = (data) => { | ||
const result = data | ||
.split('') | ||
.map(n => +n) | ||
.reduce((sum, a, idx) => { | ||
return idx % 2 | ||
? sum + a * 9 | ||
: sum + a * 3; | ||
}, 0); | ||
return result % 10; | ||
}; | ||
|
||
class EAN5 extends Barcode { | ||
|
||
constructor(data, options) { | ||
super(data, options); | ||
|
||
// Define the EAN-13 structure | ||
this.structure = [ | ||
"GGLLL", | ||
"GLGLL", | ||
"GLLGL", | ||
"GLLLG", | ||
"LGGLL", | ||
"LLGGL", | ||
"LLLGG", | ||
"LGLGL", | ||
"LGLLG", | ||
"LLGLG" | ||
]; | ||
} | ||
|
||
valid(){ | ||
valid() { | ||
return this.data.search(/^[0-9]{5}$/) !== -1; | ||
} | ||
|
||
encode(){ | ||
var encoder = new EANencoder(); | ||
var checksum = this.checksum(); | ||
|
||
// Start bits | ||
var result = "1011"; | ||
|
||
// Use normal ean encoding with 01 in between all digits | ||
result += encoder.encode(this.data, this.structure[checksum], "01"); | ||
|
||
encode() { | ||
const structure = EAN5_STRUCTURE[checksum(this.data)]; | ||
return { | ||
data: result, | ||
data: '1011' + encode(this.data, structure, '01'), | ||
text: this.text | ||
}; | ||
} | ||
|
||
checksum(){ | ||
var result = 0; | ||
|
||
result += parseInt(this.data[0]) * 3; | ||
result += parseInt(this.data[1]) * 9; | ||
result += parseInt(this.data[2]) * 3; | ||
result += parseInt(this.data[3]) * 9; | ||
result += parseInt(this.data[4]) * 3; | ||
|
||
return result % 10; | ||
} | ||
} | ||
|
||
export default EAN5; |
Oops, something went wrong.