UNPKG

uview-next

Version:

基于uView UI 2.0,110+高质量组件库,支持vue2和vue3,支持鸿蒙,支持多语言,搭配便捷工具助力,让开发更得心应手

58 lines (44 loc) 976 B
// Encoding documentation: // http://www.barcodeisland.com/ean8.phtml import EAN from './EAN'; // Calculate the checksum digit const checksum = (number) => { const res = number .substr(0, 7) .split('') .map((n) => +n) .reduce((sum, a, idx) => ( idx % 2 ? sum + a : sum + a * 3 ), 0); return (10 - (res % 10)) % 10; }; class EAN8 extends EAN { constructor(data, options) { // Add checksum if it does not exist if (data.search(/^[0-9]{7}$/) !== -1) { data += checksum(data); } super(data, options); } valid() { return ( this.data.search(/^[0-9]{8}$/) !== -1 && +this.data[7] === checksum(this.data) ); } leftText() { return super.leftText(0, 4); } leftEncode() { const data = this.data.substr(0, 4); return super.leftEncode(data, 'LLLL'); } rightText() { return super.rightText(4, 4); } rightEncode() { const data = this.data.substr(4, 4); return super.rightEncode(data, 'RRRR'); } } export default EAN8;