[email protected] 22/50
EAN/GTIN in Smalltalk (1/2)
"reversed makes no difference in the GTIN-13 case as it has an odd number of
digits, but allows GTIN-8 and GTIN-18 with no need to check input parity"
"Note the index flip when moving from validation to generation"
| lastWeight |
lastWeight:= 3. "right to left, weights are 1,3,1,3 ..."
('1234512745657' reversed inject: 0 into: [ :sum :each |
each digitValue * (lastWeight:= 4-lastWeight) + sum ]) \\ 10
"Returns 0, which indicates the number is correct"
"To generate a check digit, run the algorithm on the `payload`
of the first 12 digits and subtract the result from 10"
| lastWeight |
lastWeight:= 1. "right to left, weights are 3,1,3,1 ..."
(10 - ('123451274565' reversed inject: 0 into: [ :sum :each |
each digitValue * (lastWeight:= 4-lastWeight) + sum ]) \\ 10) \\ 10
"Returns 7 as expected from the above. If this returns 10,
the check digit is 0, hence the additional \\ 10 at the end"
"NB: without the check digit, our first digit is even, not odd."