UNPKG

ts-barcode-generator

Version:

Simple Barcode Generator created in TypeScript

228 lines (227 loc) 5.54 kB
import { convertBinaryStringToArray, convertToPairs, generateSimpleSvg1D } from '../utils'; class CODE128 { static calculateParity(data) { const leftDigits = data.slice(0, 6); const rightDigits = data.slice(6); const leftParity = leftDigits .split('') .map((digit) => this.LEFT_PARITY[digit]) .join(''); const rightParity = rightDigits .split('') .map((digit) => this.RIGHT_PARITY[digit]) .join(''); return leftParity + this.MIDDLE_MARKER + rightParity; } static generateBinaryRepresentation(data) { const parityData = this.calculateParity(data); return (this.START_MARKER + parityData + this.END_MARKER); } static generate(data) { const binaryRepresentation = this.generateBinaryRepresentation(data); const arrayRepresentation = convertBinaryStringToArray(binaryRepresentation); const groupedPairs = convertToPairs(arrayRepresentation); const svg = generateSimpleSvg1D(groupedPairs); return svg; } } CODE128.START_MARKER = '11010010000'; // Start Code B CODE128.END_MARKER = '1100011101011'; // Stop Code CODE128.MIDDLE_MARKER = '1100011101011'; // Stop Code CODE128.LEFT_PARITY = { ' ': '11011001100', '!': '11001101100', '"': '11001100110', '#': '10010011000', '$': '10010001100', '%': '10001001100', '&': '10011001000', '\'': '10011000100', '(': '10001100100', ')': '11001001000', '*': '11001000100', '+': '11000100100', ',': '10110011100', '-': '10011011100', '.': '10011001110', '/': '10111001100', '0': '10011101100', '1': '10011100110', '2': '11001110010', '3': '11001011100', '4': '11001001110', '5': '11011100100', '6': '11001110100', '7': '11101101110', '8': '11101001100', '9': '11100101100', ':': '11100100110', ';': '11101100100', '<': '11100110100', '=': '11100110010', '>': '11011011000', '?': '11011000110', '@': '11000110110', 'A': '10100011000', 'B': '10001011000', 'C': '10001000110', 'D': '10110001000', 'E': '10001101000', 'F': '10001100010', 'G': '11010001000', 'H': '11000101000', 'I': '11000100010', 'J': '10110111000', 'K': '10110001110', 'L': '10001101110', 'M': '10111011000', 'N': '10111000110', 'O': '10001110110', 'P': '11101110110', 'Q': '11010001110', 'R': '11000101110', 'S': '11011101000', 'T': '11011100010', 'U': '11011101110', 'V': '11101011000', 'W': '11101000110', 'X': '11100010110', 'Y': '11101101000', 'Z': '11101100010', '[': '11100011010', '\\': '11101111010', ']': '11001000010', '^': '11110001010', '_': '10100110000', '`': '10100001100', 'a': '10010110000', 'b': '10010000110', 'c': '10000101100', 'd': '10000100110', 'e': '10110010000', 'f': '10110000100', 'g': '10011010000', 'h': '10011000010', 'i': '10000110100', 'j': '10000110010', 'k': '11000010010', 'l': '11001010000', 'm': '11110111010', 'n': '11000010100', 'o': '10001111010', 'p': '10100111100', 'q': '10010111100', 'r': '10010011110', 's': '10111100100', 't': '10011110100', 'u': '10011110010', 'v': '11110100100', 'w': '11110010100', 'x': '11110010010', 'y': '11011011110', 'z': '11011110110', '{': '11110110110', '|': '10101111000', '}': '10100011110', '~': '10001011110', }; CODE128.RIGHT_PARITY = { ' ': '1110010', '!': '1100110', '"': '1101100', '#': '1000010', '$': '1011100', '%': '1001110', '&': '1010000', '\'': '1000100', '(': '1001000', ')': '1110100', '*': '1110000', '+': '1110000', ',': '1100000', '-': '1000100', '.': '1011000', '/': '1001000', '0': '1110010', '1': '1100110', '2': '1101100', '3': '1000010', '4': '1011100', '5': '1001110', '6': '1010000', '7': '1000100', '8': '1001000', '9': '1110100', ':': '1110000', ';': '1110000', '<': '1100000', '=': '1000100', '>': '1011000', '?': '1001000', '@': '1001000', 'A': '1000011', 'B': '1000110', 'C': '1001100', 'D': '1100001', 'E': '1100010', 'F': '1100100', 'G': '1010001', 'H': '1010010', 'I': '1010100', 'J': '1101000', 'K': '1000101', 'L': '1000110', 'M': '1001001', 'N': '1001010', 'O': '1001100', 'P': '1010010', 'Q': '1010100', 'R': '1011000', 'S': '1100001', 'T': '1100010', 'U': '1100100', 'V': '1010001', 'W': '1010010', 'X': '1010100', 'Y': '1101000', 'Z': '1000011', '[': '1000110', '\\': '1001001', ']': '1001010', '^': '1001100', '_': '1010010', '`': '1010100', 'a': '1100001', 'b': '1100010', 'c': '1100100', 'd': '1010001', 'e': '1010010', 'f': '1010100', 'g': '1101000', 'h': '1000101', 'i': '1000110', 'j': '1001001', 'k': '1001010', 'l': '1001100', 'm': '1010010', 'n': '1010100', 'o': '1011000', 'p': '1100001', 'q': '1100010', 'r': '1100100', 's': '1010001', 't': '1010010', 'u': '1010100', 'v': '1101000', 'w': '1000101', 'x': '1000110', 'y': '1001001', 'z': '1001010', '{': '1001100', '|': '1010010', '}': '1010100', '~': '1101000', }; export default CODE128;