| 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 ( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  | 
            
                        
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 you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.