All files / src/lib symbologySettings.ts

100% Statements 74/74
98.08% Branches 51/52
100% Functions 23/23
100% Lines 71/71

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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 37716x             32x                                 367x 344x 344x 344x 344x 344x 344x           16x 151x                 16x 20x   20x           16x 7x                 16x 7x   7x               16x 10x                     16x 5x 5x 2x     5x                       16x 5x 13x   5x 1x     5x                   16x 57x                 27x     27x 20x     24x     7x 6x     27x                 16x     32x 21x   30x     11x 10x     32x                   16x 57x                 27x     27x 20x     24x     7x 6x     27x                 16x     32x 21x   30x     11x 10x     32x     16x 47x                 16x 42x           16x 42x         16x                                                                                                                                                                                                                                                        
import { Barcode } from "./barcode";
 
/**
 * A symbology-specific configuration object.
 *
 * See https://docs.scandit.com/stable/c_api/symbologies.html for more details.
 */
export class SymbologySettings {
  private readonly symbology: Barcode.Symbology;
  private enabled: boolean;
  private colorInvertedEnabled: boolean;
  private activeSymbolCounts: number[];
  private extensions: Set<SymbologySettings.Extension>;
  private checksums: Set<SymbologySettings.Checksum>;
 
  /**
   * @hidden
   *
   * Create a SymbologySettings instance.
   *
   * @param symbology The symbology for which to create the settings.
   * @param enabled <div class="tsd-signature-symbol">Default =&nbsp;false</div>
   * Whether the symbology is enabled for recognition.
   */
  constructor(symbology: Barcode.Symbology, enabled: boolean = false) {
    this.symbology = symbology;
    this.enabled = enabled;
    this.colorInvertedEnabled = false;
    this.activeSymbolCounts = SymbologySettings.defaultActiveSymbolCounts[symbology] ?? [];
    this.extensions = new Set(SymbologySettings.defaultExtensions[symbology] ?? []);
    this.checksums = new Set(SymbologySettings.defaultChecksums[symbology] ?? []);
  }
 
  /**
   * @returns Whether the symbology enabled for recognition.
   */
  public isEnabled(): boolean {
    return this.enabled;
  }
 
  /**
   * Enable or disable recognition of the symbology.
   *
   * @param enabled Whether the symbology is enabled for recognition.
   * @returns The updated [[SymbologySettings]] object.
   */
  public setEnabled(enabled: boolean): SymbologySettings {
    this.enabled = enabled;
 
    return this;
  }
 
  /**
   * @returns Whether color inverted recognition is enabled.
   */
  public isColorInvertedEnabled(): boolean {
    return this.colorInvertedEnabled;
  }
 
  /**
   * Enable or disable recognition of inverted-color symbology (in addition to normal colors).
   *
   * @param enabled Whether color inverted recognition is enabled.
   * @returns The updated [[SymbologySettings]] object.
   */
  public setColorInvertedEnabled(enabled: boolean): SymbologySettings {
    this.colorInvertedEnabled = enabled;
 
    return this;
  }
 
  /**
   * Get the current list of active symbol counts.
   *
   * @returns The list of active symbol counts.
   */
  public getActiveSymbolCounts(): number[] {
    return this.activeSymbolCounts;
  }
 
  /**
   * Set the list of active symbol counts.
   *
   * If an empty or invalid symbol count range is given, the range will be set to its default value.
   *
   * @param activeSymbolCounts The list of active symbol counts.
   * @returns The updated [[SymbologySettings]] object.
   */
  public setActiveSymbolCounts(activeSymbolCounts: number[]): SymbologySettings {
    this.activeSymbolCounts = activeSymbolCounts;
    if (this.activeSymbolCounts.length === 0) {
      this.activeSymbolCounts = SymbologySettings.defaultActiveSymbolCounts[this.symbology] ?? [];
    }
 
    return this;
  }
 
  /**
   * Set the (inclusive) range of active symbol counts.
   *
   * If an empty or invalid symbol count range is given, the range will be set to its default value.
   *
   * @param minCount The minimum accepted number of symbols.
   * @param maxCount The maximum accepted number of symbols.
   * @returns The updated [[SymbologySettings]] object.
   */
  public setActiveSymbolCountsRange(minCount: number, maxCount: number): SymbologySettings {
    this.activeSymbolCounts = Array.from({ length: maxCount - minCount + 1 }, (_, k) => {
      return k + minCount;
    });
    if (this.activeSymbolCounts.length === 0) {
      this.activeSymbolCounts = SymbologySettings.defaultActiveSymbolCounts[this.symbology] ?? [];
    }
 
    return this;
  }
 
  /**
   * Get the current set of enabled optional extensions.
   *
   * Note that the external Scandit Engine library will also use any applicable mandatory extension for the symbology.
   *
   * @returns The set of enabled extensions.
   */
  public getEnabledExtensions(): Set<SymbologySettings.Extension> {
    return this.extensions;
  }
 
  /**
   * Enable an optional extension or list/set of optional extensions
   *
   * @param extension The single extension or list/set of extensions to enable.
   * @returns The updated [[SymbologySettings]] object.
   */
  public enableExtensions(
    extension: SymbologySettings.Extension | SymbologySettings.Extension[] | Set<SymbologySettings.Extension>
  ): SymbologySettings {
    if (typeof extension === "object") {
      this.extensions = new Set([
        ...this.extensions,
        ...Array.from(extension).filter((e) => {
          return this.isValidExtension(e);
        }),
      ]);
    } else if (this.isValidExtension(extension)) {
      this.extensions.add(extension);
    }
 
    return this;
  }
 
  /**
   * Disable an optional extension or list/set of optional extensions.
   *
   * @param extension The single extension or list/set of extensions to disable.
   * @returns The updated [[SymbologySettings]] object.
   */
  public disableExtensions(
    extension: SymbologySettings.Extension | SymbologySettings.Extension[] | Set<SymbologySettings.Extension>
  ): SymbologySettings {
    if (typeof extension === "object") {
      this.extensions = new Set(
        [...this.extensions].filter((x) => {
          return extension instanceof Array ? !extension.includes(x) : !extension.has(x);
        })
      );
    } else if (this.isValidExtension(extension)) {
      this.extensions.delete(extension);
    }
 
    return this;
  }
 
  /**
   * Get the current set of enabled optional checksums.
   *
   * Note that the external Scandit Engine library will also use any applicable mandatory checksum for the symbology.
   *
   * @returns The set of enabled checksums.
   */
  public getEnabledChecksums(): Set<SymbologySettings.Checksum> {
    return this.checksums;
  }
 
  /**
   * Enable an optional checksum or list/set of optional checksums.
   *
   * @param checksum The single checksum or list/set of checksums to enable.
   * @returns The updated [[SymbologySettings]] object.
   */
  public enableChecksums(
    checksum: SymbologySettings.Checksum | SymbologySettings.Checksum[] | Set<SymbologySettings.Checksum>
  ): SymbologySettings {
    if (typeof checksum === "object") {
      this.checksums = new Set([
        ...this.checksums,
        ...Array.from(checksum).filter((c) => {
          return this.isValidChecksum(c);
        }),
      ]);
    } else if (this.isValidChecksum(checksum)) {
      this.checksums.add(checksum);
    }
 
    return this;
  }
 
  /**
   * Disable an optional checksum or list/set of optional checksums.
   *
   * @param checksum The single checksum or list/set of checksums to disable.
   * @returns The updated [[SymbologySettings]] object.
   */
  public disableChecksums(
    checksum: SymbologySettings.Checksum | SymbologySettings.Checksum[] | Set<SymbologySettings.Checksum>
  ): SymbologySettings {
    if (typeof checksum === "object") {
      this.checksums = new Set(
        [...this.checksums].filter((x) => {
          return checksum instanceof Array ? !checksum.includes(x) : !checksum.has(x);
        })
      );
    } else if (this.isValidChecksum(checksum)) {
      this.checksums.delete(checksum);
    }
 
    return this;
  }
 
  protected toJSON(): object {
    return {
      enabled: this.enabled,
      colorInvertedEnabled: this.colorInvertedEnabled,
      activeSymbolCounts: this.activeSymbolCounts.length === 0 ? undefined : this.activeSymbolCounts,
      extensions: Array.from(this.extensions),
      checksums: Array.from(this.checksums),
    };
  }
 
  private isValidExtension(extension: string | SymbologySettings.Extension): boolean {
    return (
      extension in SymbologySettings.Extension ||
      (<string[]>Object.values(SymbologySettings.Extension)).includes(extension.toLowerCase())
    );
  }
 
  private isValidChecksum(checksum: string | SymbologySettings.Checksum): boolean {
    return (
      checksum in SymbologySettings.Checksum ||
      (<string[]>Object.values(SymbologySettings.Checksum)).includes(checksum.toLowerCase())
    );
  }
}
 
// istanbul ignore next
export namespace SymbologySettings {
  /**
   * Symbology extensions for particular functionalities, only applicable to specific barcodes.
   * See: https://docs.scandit.com/stable/c_api/symbologies.html.
   */
  export enum Extension {
    /**
     * Improve scan performance when reading direct part marked (DPM) Data Matrix codes.
     * Enabling this extension comes at the cost of increased frame processing times.
     */
    DIRECT_PART_MARKING_MODE = "direct_part_marking_mode",
    /**
     * Interpret the Code 39 / Code 93 code data using two symbols per output character to encode all ASCII characters.
     */
    FULL_ASCII = "full_ascii",
    /**
     * Enable scanning codes that have quiet zones (white area before and after the code) significantly smaller
     * than what's allowed by the symbology specification.
     */
    RELAXED_SHARP_QUIET_ZONE_CHECK = "relaxed_sharp_quiet_zone_check",
    /**
     * Remove the leading zero digit from the result.
     */
    REMOVE_LEADING_ZERO = "remove_leading_zero",
    /**
     * Remove the leading zero digit from the result if the UPC-A representation extension "RETURN_AS_UPCA" is enabled.
     */
    REMOVE_LEADING_UPCA_ZERO = "remove_leading_upca_zero",
    /**
     * Transform the UPC-E result into its UPC-A representation.
     */
    RETURN_AS_UPCA = "return_as_upca",
    /**
     * Remove the leading FNC1 character that indicates a GS1 code.
     */
    STRIP_LEADING_FNC1 = "strip_leading_fnc1",
  }
 
  /**
   * Checksum algorithms, only applicable to specific barcodes.
   * See: https://docs.scandit.com/stable/c_api/symbologies.html.
   */
  export enum Checksum {
    /**
     * Modulo 10 checksum.
     */
    MOD_10 = "mod10",
    /**
     * Modulo 11 checksum.
     */
    MOD_11 = "mod11",
    /**
     * Modulo 16 checksum.
     */
    MOD_16 = "mod16",
    /**
     * Modulo 43 checksum.
     */
    MOD_43 = "mod43",
    /**
     * Modulo 47 checksum.
     */
    MOD_47 = "mod47",
    /**
     * Modulo 103 checksum.
     */
    MOD_103 = "mod103",
    /**
     * Two modulo 10 checksums.
     */
    MOD_1010 = "mod1010",
    /**
     * Modulo 11 and modulo 10 checksum.
     */
    MOD_1110 = "mod1110",
  }
 
  /**
   * @hidden
   */
  export const defaultActiveSymbolCounts: Partial<Record<Barcode.Symbology, number[]>> = {
    [Barcode.Symbology.CODABAR]: [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    [Barcode.Symbology.CODE11]: [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    [Barcode.Symbology.CODE128]: [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    [Barcode.Symbology.CODE25]: [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    [Barcode.Symbology.CODE32]: [8],
    [Barcode.Symbology.CODE39]: [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    [Barcode.Symbology.CODE93]: [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22],
    [Barcode.Symbology.EAN13]: [12],
    [Barcode.Symbology.EAN8]: [8],
    [Barcode.Symbology.FIVE_DIGIT_ADD_ON]: [5],
    [Barcode.Symbology.GS1_DATABAR_EXPANDED]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
    [Barcode.Symbology.GS1_DATABAR_LIMITED]: [1],
    [Barcode.Symbology.GS1_DATABAR]: [2],
    [Barcode.Symbology.IATA_2_OF_5]: [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    [Barcode.Symbology.INTERLEAVED_2_OF_5]: [8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
    [Barcode.Symbology.KIX]: [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],
    [Barcode.Symbology.LAPA4SC]: [16],
    [Barcode.Symbology.MSI_PLESSEY]: [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    [Barcode.Symbology.RM4SCC]: [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24],
    [Barcode.Symbology.TWO_DIGIT_ADD_ON]: [2],
    [Barcode.Symbology.UPCA]: [12],
    [Barcode.Symbology.UPCE]: [6],
  };
 
  /**
   * @hidden
   */
  export const defaultExtensions: Partial<Record<Barcode.Symbology, Extension[]>> = {
    [Barcode.Symbology.CODE128]: [Extension.STRIP_LEADING_FNC1],
    [Barcode.Symbology.DATA_MATRIX]: [Extension.STRIP_LEADING_FNC1],
  };
 
  /**
   * @hidden
   */
  export const defaultChecksums: Partial<Record<Barcode.Symbology, Checksum[]>> = {
    [Barcode.Symbology.MSI_PLESSEY]: [Checksum.MOD_10],
    [Barcode.Symbology.CODE11]: [Checksum.MOD_11],
  };
}