ITF.js 703 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import { START_BIN, END_BIN, BINARIES } from './constants';
  2. import Barcode from '../Barcode';
  3. class ITF extends Barcode {
  4. valid() {
  5. return this.data.search(/^([0-9]{2})+$/) !== -1;
  6. }
  7. encode() {
  8. // Calculate all the digit pairs
  9. const encoded = this.data
  10. .match(/.{2}/g)
  11. .map(pair => this.encodePair(pair))
  12. .join('');
  13. return {
  14. data: START_BIN + encoded + END_BIN,
  15. text: this.text
  16. };
  17. }
  18. // Calculate the data of a number pair
  19. encodePair(pair) {
  20. const second = BINARIES[pair[1]];
  21. return BINARIES[pair[0]]
  22. .split('')
  23. .map((first, idx) => (
  24. (first === '1' ? '111' : '1') +
  25. (second[idx] === '1' ? '000' : '0')
  26. ))
  27. .join('');
  28. }
  29. }
  30. export default ITF;