All files / src/lib binary.ts

95.65% Statements 22/23
88.23% Branches 15/17
100% Functions 3/3
95.65% Lines 22/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 1082x   2x                         2x                                                                           6x   6x       6x 5x     5x 1x   4x       6x   6x 5x 4x     5x     1x                   6x   2x   1x   3x       2x           2x  
import symbology from '../binding'
import BinResult from '../types/BinResult'
import OutputType from '../types/enums/OutputType'
import SymbologyConfig from '../types/SymbologyConfig'
 
/**
 * Calls the given function name from the c++ library wrapper, validates
 * the struct values and passes the arguments sent in symbologyStruct
 * in the correct order.
 *
 * @param {SymbologyConfig} config - symbology config
 * @param {string} barcodeData - primary data to encode
 * @return {BinResult}
 */
function createBuffer (config: SymbologyConfig, barcodeData: string): BinResult {
  return symbology.createStream(
    barcodeData,
    config.symbology,
    config.height,
    config.whitespaceWidth,
    config.borderWidth,
    config.outputOptions,
    config.backgroundColor,
    config.foregroundColor,
    // indicate to the library that we want BMP instead of PNG
    // this is to force zint to return a bitmap array buffer instead PNG binary
    config.fileName?.replace(/\.png$/g, '.bmp'),
    config.scale,
    config.option1,
    config.option2,
    config.option3,
    config.showHumanReadableText ? 1 : 0,
    (config.text || barcodeData),
    config.encoding,
    config.eci,
    config.primary,
    config.rotation,
    config.dotSize
  )
}
 
/**
 * Renders a png, svg, or eps barcode.
 * If PNG, it returns the stream as a base64 string.
 *
 * @note The file will be created in memory and then passed to the returned object.
 *
 * @param {SymbologyConfig} config - symbology config
 * @param {string} barcodeData - primary data to encode
 * @param {OutputType} outputType
 * @returns {Promise<BinResult>} object with resulting props (see docs)
 */
function invoke (config: SymbologyConfig, barcodeData: string, outputType: OutputType): Promise<BinResult> {
  const symbol = { ...config }
 
  Iif (![OutputType.PNG, OutputType.EPS, OutputType.SVG].includes(outputType)) {
    return Promise.reject(`Invalid output type: ${outputType}`)
  }
 
  if (outputType !== OutputType.PNG) {
    symbol.fileName = `out.${outputType}`
 
    // apply option 8 (suppress stdout)
    if (symbol.outputOptions) {
      symbol.outputOptions += 8
    } else {
      symbol.outputOptions = 8
    }
  }
 
  const res = binary.createBuffer(symbol, barcodeData)
 
  if (res.code <= 2) {
    if (res.code === 0) {
      res.message = 'Symbology successfully created.'
    }
 
    return Promise.resolve(res)
  }
 
  return Promise.reject(res.message)
}
 
/**
 * Determines the OutputType of the given file name by its extension. Defaults to PNG.
 *
 * @param {string} fileName
 * @returns {OutputType}
 */
function getOutputType (fileName: string): OutputType {
  switch (fileName.toLowerCase().substr(-3)) {
    case 'svg':
      return OutputType.SVG
    case 'eps':
      return OutputType.EPS
    default:
      return OutputType.PNG
  }
}
 
const binary = {
  createBuffer,
  invoke,
  getOutputType
}
 
export default binary