src/barcodes/ITF/ITF.js   A
last analyzed

Size

Lines of Code 37

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
nc 1
dl 0
loc 37
rs 10
noi 0

1 Function

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