@stacksjs/qrx
Version:
QR & Bar Code generating & reading. Lightweight & powerful.
102 lines (82 loc) • 2.27 kB
TypeScript
export declare class UPC extends Barcode {
displayValue: boolean
fontSize: number
guardHeight: number
constructor(data: string, options: any) {
if (data.search(/^\d{11}$/) !== -1) {
data += checksum(data)
}
super(data, options)
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.data.search(/^\d{12}$/) !== -1
&& Number(this.data[11]) === checksum(this.data)
}
encode(): any {
if (this.options.flat) {
return this.flatEncoding()
}
return this.guardedEncoding()
}
flatEncoding(): { data: string, text: string } {
let result = ''
result += '101'
result += encode(this.data.substr(0, 6), 'LLLLLL')
result += '01010'
result += encode(this.data.substr(6, 6), 'RRRRRR')
result += '101'
return {
data: result,
text: this.text,
}
}
guardedEncoding(): any {
const result = []
if (this.displayValue) {
result.push({
data: '00000000',
text: this.text.substr(0, 1),
options: { textAlign: 'left', fontSize: this.fontSize },
})
}
result.push({
data: `101${encode(this.data[0], 'L')}`,
options: { height: this.guardHeight },
})
result.push({
data: encode(this.data.substr(1, 5), 'LLLLL'),
text: this.text.substr(1, 5),
options: { fontSize: this.fontSize },
})
result.push({
data: '01010',
options: { height: this.guardHeight },
})
result.push({
data: encode(this.data.substr(6, 5), 'RRRRR'),
text: this.text.substr(6, 5),
options: { fontSize: this.fontSize },
})
result.push({
data: `${encode(this.data[11], 'R')}101`,
options: { height: this.guardHeight },
})
if (this.displayValue) {
result.push({
data: '00000000',
text: this.text.substr(11, 1),
options: { textAlign: 'right', fontSize: this.fontSize },
})
}
return result
}
}
export declare function checksum(number: string): number;