1
- " use strict" ;
1
+ ' use strict' ;
2
2
3
3
Object . defineProperty ( exports , "__esModule" , {
4
4
value : true
5
5
} ) ;
6
6
7
7
var _createClass = function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( "value" in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ( ) ;
8
8
9
- var _ean_encoder = require ( "./ean_encoder.js" ) ;
9
+ var _get = function get ( object , property , receiver ) { if ( object === null ) object = Function . prototype ; var desc = Object . getOwnPropertyDescriptor ( object , property ) ; if ( desc === undefined ) { var parent = Object . getPrototypeOf ( object ) ; if ( parent === null ) { return undefined ; } else { return get ( parent , property , receiver ) ; } } else if ( "value" in desc ) { return desc . value ; } else { var getter = desc . get ; if ( getter === undefined ) { return undefined ; } return getter . call ( receiver ) ; } } ;
10
10
11
- var _ean_encoder2 = _interopRequireDefault ( _ean_encoder ) ;
11
+ var _constants = require ( './constants' ) ;
12
12
13
- var _Barcode2 = require ( "../Barcode.js" ) ;
13
+ var _EAN2 = require ( './EAN' ) ;
14
14
15
- var _Barcode3 = _interopRequireDefault ( _Barcode2 ) ;
15
+ var _EAN3 = _interopRequireDefault ( _EAN2 ) ;
16
16
17
17
function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
18
18
@@ -23,8 +23,20 @@ function _possibleConstructorReturn(self, call) { if (!self) { throw new Referen
23
23
function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function, not " + typeof superClass ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , enumerable : false , writable : true , configurable : true } } ) ; if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : subClass . __proto__ = superClass ; } // Encoding documentation:
24
24
// https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Binary_encoding_of_data_digits_into_EAN-13_barcode
25
25
26
- var EAN13 = function ( _Barcode ) {
27
- _inherits ( EAN13 , _Barcode ) ;
26
+ // Calculate the checksum digit
27
+ // https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit
28
+ var checksum = function checksum ( number ) {
29
+ var res = number . substr ( 0 , 12 ) . split ( '' ) . map ( function ( n ) {
30
+ return + n ;
31
+ } ) . reduce ( function ( sum , a , idx ) {
32
+ return idx % 2 ? sum + a * 3 : sum + a ;
33
+ } , 0 ) ;
34
+
35
+ return ( 10 - res % 10 ) % 10 ;
36
+ } ;
37
+
38
+ var EAN13 = function ( _EAN ) {
39
+ _inherits ( EAN13 , _EAN ) ;
28
40
29
41
function EAN13 ( data , options ) {
30
42
_classCallCheck ( this , EAN13 ) ;
@@ -34,154 +46,74 @@ var EAN13 = function (_Barcode) {
34
46
data += checksum ( data ) ;
35
47
}
36
48
37
- // Make sure the font is not bigger than the space between the guard bars
49
+ // Adds a last character to the end of the barcode
38
50
var _this = _possibleConstructorReturn ( this , ( EAN13 . __proto__ || Object . getPrototypeOf ( EAN13 ) ) . call ( this , data , options ) ) ;
39
51
40
- if ( ! options . flat && options . fontSize > options . width * 10 ) {
41
- _this . fontSize = options . width * 10 ;
42
- } else {
43
- _this . fontSize = options . fontSize ;
44
- }
45
-
46
- // Make the guard bars go down half the way of the text
47
- _this . guardHeight = options . height + _this . fontSize / 2 + options . textMargin ;
48
-
49
- // Adds a last character to the end of the barcode
50
52
_this . lastChar = options . lastChar ;
51
53
return _this ;
52
54
}
53
55
54
56
_createClass ( EAN13 , [ {
55
- key : " valid" ,
57
+ key : ' valid' ,
56
58
value : function valid ( ) {
57
- return this . data . search ( / ^ [ 0 - 9 ] { 13 } $ / ) !== - 1 && this . data [ 12 ] == checksum ( this . data ) ;
59
+ return this . data . search ( / ^ [ 0 - 9 ] { 13 } $ / ) !== - 1 && + this . data [ 12 ] = == checksum ( this . data ) ;
58
60
}
59
61
} , {
60
- key : "encode" ,
61
- value : function encode ( ) {
62
- if ( this . options . flat ) {
63
- return this . flatEncoding ( ) ;
64
- } else {
65
- return this . guardedEncoding ( ) ;
66
- }
62
+ key : 'leftText' ,
63
+ value : function leftText ( ) {
64
+ return _get ( EAN13 . prototype . __proto__ || Object . getPrototypeOf ( EAN13 . prototype ) , 'leftText' , this ) . call ( this , 1 , 6 ) ;
65
+ }
66
+ } , {
67
+ key : 'leftEncode' ,
68
+ value : function leftEncode ( ) {
69
+ var data = this . data . substr ( 1 , 6 ) ;
70
+ var structure = _constants . EAN13_STRUCTURE [ this . data [ 0 ] ] ;
71
+ return _get ( EAN13 . prototype . __proto__ || Object . getPrototypeOf ( EAN13 . prototype ) , 'leftEncode' , this ) . call ( this , data , structure ) ;
72
+ }
73
+ } , {
74
+ key : 'rightText' ,
75
+ value : function rightText ( ) {
76
+ return _get ( EAN13 . prototype . __proto__ || Object . getPrototypeOf ( EAN13 . prototype ) , 'rightText' , this ) . call ( this , 7 , 6 ) ;
67
77
}
68
-
69
- // Define the EAN-13 structure
70
-
71
78
} , {
72
- key : "getStructure" ,
73
- value : function getStructure ( ) {
74
- return [ "LLLLLL" , "LLGLGG" , "LLGGLG" , "LLGGGL" , "LGLLGG" , "LGGLLG" , "LGGGLL" , "LGLGLG" , "LGLGGL" , "LGGLGL" ] ;
79
+ key : 'rightEncode' ,
80
+ value : function rightEncode ( ) {
81
+ var data = this . data . substr ( 7 , 6 ) ;
82
+ return _get ( EAN13 . prototype . __proto__ || Object . getPrototypeOf ( EAN13 . prototype ) , 'rightEncode' , this ) . call ( this , data , 'RRRRRR' ) ;
75
83
}
76
84
77
85
// The "standard" way of printing EAN13 barcodes with guard bars
78
86
79
87
} , {
80
- key : "guardedEncoding" ,
81
- value : function guardedEncoding ( ) {
82
- var encoder = new _ean_encoder2 . default ( ) ;
83
- var result = [ ] ;
84
-
85
- var structure = this . getStructure ( ) [ this . data [ 0 ] ] ;
88
+ key : 'encodeGuarded' ,
89
+ value : function encodeGuarded ( ) {
90
+ var data = _get ( EAN13 . prototype . __proto__ || Object . getPrototypeOf ( EAN13 . prototype ) , 'encodeGuarded' , this ) . call ( this ) ;
86
91
87
- // Get the string to be encoded on the left side of the EAN code
88
- var leftSide = this . data . substr ( 1 , 6 ) ;
89
-
90
- // Get the string to be encoded on the right side of the EAN code
91
- var rightSide = this . data . substr ( 7 , 6 ) ;
92
-
93
- // Add the first digigt
92
+ // Extend data with left digit & last character
94
93
if ( this . options . displayValue ) {
95
- result . push ( {
96
- data : " 000000000000" ,
94
+ data . unshift ( {
95
+ data : ' 000000000000' ,
97
96
text : this . text . substr ( 0 , 1 ) ,
98
- options : { textAlign : " left" , fontSize : this . fontSize }
97
+ options : { textAlign : ' left' , fontSize : this . fontSize }
99
98
} ) ;
100
- }
101
99
102
- // Add the guard bars
103
- result . push ( {
104
- data : "101" ,
105
- options : { height : this . guardHeight }
106
- } ) ;
107
-
108
- // Add the left side
109
- result . push ( {
110
- data : encoder . encode ( leftSide , structure ) ,
111
- text : this . text . substr ( 1 , 6 ) ,
112
- options : { fontSize : this . fontSize }
113
- } ) ;
114
-
115
- // Add the middle bits
116
- result . push ( {
117
- data : "01010" ,
118
- options : { height : this . guardHeight }
119
- } ) ;
120
-
121
- // Add the right side
122
- result . push ( {
123
- data : encoder . encode ( rightSide , "RRRRRR" ) ,
124
- text : this . text . substr ( 7 , 6 ) ,
125
- options : { fontSize : this . fontSize }
126
- } ) ;
127
-
128
- // Add the end bits
129
- result . push ( {
130
- data : "101" ,
131
- options : { height : this . guardHeight }
132
- } ) ;
133
-
134
- if ( this . options . lastChar && this . options . displayValue ) {
135
- result . push ( { data : "00" } ) ;
136
-
137
- result . push ( {
138
- data : "00000" ,
139
- text : this . options . lastChar ,
140
- options : { fontSize : this . fontSize }
141
- } ) ;
100
+ if ( this . options . lastChar ) {
101
+ data . push ( {
102
+ data : '00'
103
+ } ) ;
104
+ data . push ( {
105
+ data : '00000' ,
106
+ text : this . options . lastChar ,
107
+ options : { fontSize : this . fontSize }
108
+ } ) ;
109
+ }
142
110
}
143
- return result ;
144
- }
145
- } , {
146
- key : "flatEncoding" ,
147
- value : function flatEncoding ( ) {
148
- var encoder = new _ean_encoder2 . default ( ) ;
149
- var result = "" ;
150
-
151
- var structure = this . getStructure ( ) [ this . data [ 0 ] ] ;
152
-
153
- result += "101" ;
154
- result += encoder . encode ( this . data . substr ( 1 , 6 ) , structure ) ;
155
- result += "01010" ;
156
- result += encoder . encode ( this . data . substr ( 7 , 6 ) , "RRRRRR" ) ;
157
- result += "101" ;
158
-
159
- return {
160
- data : result ,
161
- text : this . text
162
- } ;
111
+
112
+ return data ;
163
113
}
164
114
} ] ) ;
165
115
166
116
return EAN13 ;
167
- } ( _Barcode3 . default ) ;
168
-
169
- // Calulate the checksum digit
170
- // https://en.wikipedia.org/wiki/International_Article_Number_(EAN)#Calculation_of_checksum_digit
171
-
172
-
173
- function checksum ( number ) {
174
- var result = 0 ;
175
-
176
- var i ;
177
- for ( i = 0 ; i < 12 ; i += 2 ) {
178
- result += parseInt ( number [ i ] ) ;
179
- }
180
- for ( i = 1 ; i < 12 ; i += 2 ) {
181
- result += parseInt ( number [ i ] ) * 3 ;
182
- }
183
-
184
- return ( 10 - result % 10 ) % 10 ;
185
- }
117
+ } ( _EAN3 . default ) ;
186
118
187
119
exports . default = EAN13 ;
0 commit comments