Lines of Code | 54 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | // Encoding documentation: |
||
4 | import EAN from './EAN'; |
||
5 | |||
6 | // Calculate the checksum digit |
||
7 | const checksum = (number) => { |
||
8 | const res = number |
||
9 | .substr(0, 7) |
||
10 | .split('') |
||
11 | .map((n) => +n) |
||
12 | .reduce((sum, a, idx) => ( |
||
13 | idx % 2 ? sum + a : sum + a * 3 |
||
14 | ), 0); |
||
15 | |||
16 | return (10 - (res % 10)) % 10; |
||
17 | }; |
||
18 | |||
19 | class EAN8 extends EAN { |
||
20 | |||
21 | constructor(data, options) { |
||
22 | // Add checksum if it does not exist |
||
23 | if (data.search(/^[0-9]{7}$/) !== -1) { |
||
24 | data += checksum(data); |
||
25 | } |
||
26 | |||
27 | super(data, options); |
||
28 | } |
||
29 | |||
30 | valid() { |
||
31 | return ( |
||
32 | this.data.search(/^[0-9]{8}$/) !== -1 && |
||
33 | +this.data[7] === checksum(this.data) |
||
34 | ); |
||
35 | } |
||
36 | |||
37 | leftText() { |
||
38 | return super.leftText(0, 4); |
||
39 | } |
||
40 | |||
41 | leftEncode() { |
||
42 | const data = this.data.substr(0, 4); |
||
43 | return super.leftEncode(data, 'LLLL'); |
||
44 | } |
||
45 | |||
46 | rightText() { |
||
47 | return super.rightText(4, 4); |
||
48 | } |
||
49 | |||
50 | rightEncode() { |
||
51 | const data = this.data.substr(4, 4); |
||
52 | return super.rightEncode(data, 'RRRR'); |
||
53 | } |
||
54 | |||
55 | } |
||
56 | |||
57 | export default EAN8; |
||
58 |