EAN2.js 641 B

123456789101112131415161718192021222324252627282930
  1. // Encoding documentation:
  2. // https://en.wikipedia.org/wiki/EAN_2#Encoding
  3. import { EAN2_STRUCTURE } from './constants';
  4. import encode from './encoder';
  5. import Barcode from '../Barcode';
  6. class EAN2 extends Barcode {
  7. constructor(data, options) {
  8. super(data, options);
  9. }
  10. valid() {
  11. return this.data.search(/^[0-9]{2}$/) !== -1;
  12. }
  13. encode(){
  14. // Choose the structure based on the number mod 4
  15. const structure = EAN2_STRUCTURE[parseInt(this.data) % 4];
  16. return {
  17. // Start bits + Encode the two digits with 01 in between
  18. data: '1011' + encode(this.data, structure, '01'),
  19. text: this.text
  20. };
  21. }
  22. }
  23. export default EAN2;