1
hao
2025-03-27 e610e1c17f62b423a717fadaaa7b139d02857793
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { A_START_CHAR, B_START_CHAR, C_START_CHAR, A_CHARS, B_CHARS, C_CHARS } from './constants';
 
// Match Set functions
const matchSetALength = (string) => string.match(new RegExp(`^${A_CHARS}*`))[0].length;
const matchSetBLength = (string) => string.match(new RegExp(`^${B_CHARS}*`))[0].length;
const matchSetC = (string) => string.match(new RegExp(`^${C_CHARS}*`))[0];
 
// CODE128A or CODE128B
function autoSelectFromAB(string, isA){
    const ranges = isA ? A_CHARS : B_CHARS;
    const untilC = string.match(new RegExp(`^(${ranges}+?)(([0-9]{2}){2,})([^0-9]|$)`));
 
    if (untilC) {
        return (
            untilC[1] +
            String.fromCharCode(204) +
            autoSelectFromC(string.substring(untilC[1].length))
        );
    }
 
    const chars = string.match(new RegExp(`^${ranges}+`))[0];
 
    if (chars.length === string.length) {
        return string;
    }
 
    return (
        chars +
        String.fromCharCode(isA ? 205 : 206) +
        autoSelectFromAB(string.substring(chars.length), !isA)
    );
}
 
// CODE128C
function autoSelectFromC(string) {
    const cMatch = matchSetC(string);
    const length = cMatch.length;
 
    if (length === string.length) {
        return string;
    }
 
    string = string.substring(length);
 
    // Select A/B depending on the longest match
    const isA = matchSetALength(string) >= matchSetBLength(string);
    return cMatch + String.fromCharCode(isA ? 206 : 205) + autoSelectFromAB(string, isA);
}
 
// Detect Code Set (A, B or C) and format the string
export default (string) => {
    let newString;
    const cLength = matchSetC(string).length;
 
    // Select 128C if the string start with enough digits
    if (cLength >= 2) {
        newString = C_START_CHAR + autoSelectFromC(string);
    } else {
        // Select A/B depending on the longest match
        const isA = matchSetALength(string) > matchSetBLength(string);
        newString = (isA ? A_START_CHAR : B_START_CHAR) + autoSelectFromAB(string, isA);
    }
 
    return newString.replace(
        /[\xCD\xCE]([^])[\xCD\xCE]/, // Any sequence between 205 and 206 characters
        (match, char) => String.fromCharCode(203) + char
    );
};