Completed
Pull Request — master (#150)
by
unknown
01:09
created

auto.js ➔ autoSelectFromC   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
import { A_START_CHAR, B_START_CHAR, C_START_CHAR, A_CHARS, B_CHARS, C_CHARS } from './constants';
2
3
// Match Set functions
4
const matchSetALength = (string) => string.match(new RegExp(`^${A_CHARS}*`))[0].length;
5
const matchSetBLength = (string) => string.match(new RegExp(`^${B_CHARS}*`))[0].length;
6
const matchSetC = (string) => string.match(new RegExp(`^${C_CHARS}*`))[0];
7
8
// CODE128A or CODE128B
9
function autoSelectFromAB(string, isA){
10
	const ranges = isA ? A_CHARS : B_CHARS;
11
	const untilC = string.match(new RegExp(`^(${ranges}+?)(([0-9]{2}){2,})([^0-9]|$)`));
12
13
	if (untilC) return (
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
14
		untilC[1] +
15
		String.fromCharCode(204) +
16
		autoSelectFromC(string.substring(untilC[1].length))
17
	);
18
19
	const chars = string.match(new RegExp(`^${ranges}+`))[0];
20
	if (chars.length === string.length) return string;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
21
22
	return (
23
		chars +
24
		String.fromCharCode(isA ? 205 : 206) +
25
		autoSelectFromAB(string.substring(chars.length), !isA)
26
	);
27
}
28
29
// CODE128C
30
function autoSelectFromC(string) {
31
	const cMatch = matchSetC(string);
32
	const length = cMatch.length;
33
34
	if (length === string.length) return string;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
35
	string = string.substring(length);
36
37
	// Select A/B depending on the longest match
38
	const isA = matchSetALength(string) >= matchSetBLength(string);
39
	return cMatch + String.fromCharCode(isA ? 206 : 205) + autoSelectFromAB(string, isA);
40
}
41
42
// Detect Code Set (A, B or C) and format the string
43
export default (string) => {
44
	let newString;
45
	const cLength = matchSetC(string).length;
46
47
	// Select 128C if the string start with enough digits
48
	if (cLength >= 2) {
49
		newString = C_START_CHAR + autoSelectFromC(string);
50
	} else {
51
		// Select A/B depending on the longest match
52
		const isA = matchSetALength(string) > matchSetBLength(string);
53
		newString = (isA ? A_START_CHAR : B_START_CHAR) + autoSelectFromAB(string, isA);
54
	}
55
56
	return newString.replace(
57
		/[\xCD\xCE]([^])[\xCD\xCE]/, // Any sequence between 205 and 206 characters
58
		(match, char) => String.fromCharCode(203) + char
59
	);
60
};
61