UNPKG

@stacksjs/qrx

Version:

QR & Bar Code generating & reading. Lightweight & powerful.

114 lines (95 loc) 3.03 kB
declare const EXPANSIONS: Array<'XX00000XXX' | 'XX10000XXX' | 'XX20000XXX' | 'XXX00000XX' | 'XXXX00000X' | 'XXXXX00005' | 'XXXXX00006' | 'XXXXX00007' | 'XXXXX00008' | 'XXXXX00009'>; declare const PARITIES: Array<Array<'EEEOOO' | 'OOOEEE'> | Array<'EEOEOO' | 'OOEOEE'> | Array<'EEOOEO' | 'OOEEOE'> | Array<'EEOOOE' | 'OOEEEO'> | Array<'EOEEOO' | 'OEOOEE'> | Array<'EOOEEO' | 'OEEOOE'> | Array<'EOOOEE' | 'OEEEOO'> | Array<'EOEOEO' | 'OEOEOE'> | Array<'EOEOOE' | 'OEOEEO'> | Array<'EOOEOE' | 'OEEOEO'>>; export declare class UPCE extends Barcode { isValid: boolean middleDigits!: string upcA!: string displayValue!: boolean fontSize!: number guardHeight!: number constructor(data: string, options: any) { super(data, options) this.isValid = false if (data.search(/^\d{6}$/) !== -1) { this.middleDigits = data } else if (data.search(/^[01]\d{7}$/) !== -1) { this.middleDigits = data.substring(1, data.length - 1) this.upcA = expandToUPCA(this.middleDigits, data[0]) if (this.upcA[this.upcA.length - 1] === data[data.length - 1]) { this.isValid = true } else { return } } else { return } this.displayValue = options.displayValue if (options.fontSize > options.width * 10) { this.fontSize = options.width * 10 } else { this.fontSize = options.fontSize } this.guardHeight! = options.height + this.fontSize / 2 + options.textMargin } valid(): boolean { return this.isValid } encode(): { text: string, data: string } { if (this.options.flat) { return this.flatEncoding() } return this.guardedEncoding() } flatEncoding(): { text: string, data: string } { let result = '' result += '101' result += this.encodeMiddleDigits() result += '010101' return { data: result, text: this.text, } } guardedEncoding(): any { const result = [] if (this.displayValue) { result.push({ data: '00000000', text: this.text[0], options: { textAlign: 'left', fontSize: this.fontSize }, }) } result.push({ data: '101', options: { height: this.guardHeight }, }) result.push({ data: this.encodeMiddleDigits(), text: this.text.substring(1, 7), options: { fontSize: this.fontSize }, }) result.push({ data: '010101', options: { height: this.guardHeight }, }) if (this.displayValue) { result.push({ data: '00000000', text: this.text[7], options: { textAlign: 'right', fontSize: this.fontSize }, }) } return result } encodeMiddleDigits(): string { const numberSystem = this.upcA[0] const checkDigit = this.upcA[this.upcA.length - 1] const parity = PARITIES[Number.parseInt(checkDigit)][Number.parseInt(numberSystem)] return encode(this.middleDigits, parity) } } declare function expandToUPCA(middleDigits: string, numberSystem: string): string;