barcode.js

generate barcode
mail@pastecode.io avatar
unknown
javascript
3 years ago
1.2 kB
6
Indexable
Never
function generateBarcode(input){
    var output_barcode = ''
    var input_list = []
    for(var i = 0; i < input.length; i++){
        input_list = input_list.concat(input[i])
    }
    input = [input_list]
    for(var i = 0; i < input.length; i++){
        var barcode_list = input[i]
        var ganjil = false
        if(barcode_list.length % 2 == 1){
            ganjil = true
        }
        for(var j = 0; j < barcode_list.length; j += 2){
            var barcode = []
            if(ganjil && j == 0){
                barcode.push(barcode_list[j])
            }
            else if(ganjil){
                barcode.push(barcode_list[j-1],barcode_list[j])
            }
            else{
                barcode.push(barcode_list[j],barcode_list[j+1])
            }
            output_barcode += createPattern(barcode)
        }
    }
    return output_barcode
}

function createPattern(barcode_list){
    return '^XA'+preparePattern(barcode_list)+'^XZ'
}

function preparePattern(barcode_list){
    var pattern = ''
    for(var i = 0; i < barcode_list.length; i++){
        var product = barcode_list[i].product_id
        var barcode = barcode_list[i].barcode
        pattern += '^FD'+product+'^FB'+barcode
    }
    return pattern
}