1 | import { ParserResult } from "./parserResult";
|
2 | import { Scanner } from "./scanner";
|
3 |
|
4 | // Deprecated but useful function for easy UTF-8 handling
|
5 | /**
|
6 | * @hidden
|
7 | */
|
8 | declare function escape(s: string): string;
|
9 |
|
10 | /**
|
11 | * A data string parser.
|
12 | *
|
13 | * Parsers are capable of parsing one particular data format, which is passed to them during construction.
|
14 | *
|
15 | * The parser is created through [[BarcodePicker.createParserForFormat]] or [[Scanner.createParserForFormat]].
|
16 | * Note that you need to have a valid license with the parsing feature enabled to use the parser functionalities.
|
17 | *
|
18 | * For documentation on the available formats, check the official parser library documentation here:
|
19 | * https://docs.scandit.com/parser/formats.html.
|
20 | */
|
21 | export class Parser {
|
22 | private options?: object;
|
23 |
|
24 | /**
|
25 | * @hidden
|
26 | *
|
27 | * @param scanner The [[Scanner]] object used to interact with the external Scandit library.
|
28 | * @param dataFormat The data format for this parser.
|
29 | */
|
30 | constructor(private readonly scanner: Scanner, private readonly dataFormat: Parser.DataFormat) {}
|
31 |
|
32 | /**
|
33 | * Apply the option map to the parser, allowing the user to fine-tune the behaviour of the parser.
|
34 | * Available options depend on the data format and are specified in the respective documentation.
|
35 | *
|
36 | * @param options The new options to be applied (replacing previous ones, if any).
|
37 | */
|
38 | public setOptions(options?: object): void {
|
39 | this.options = options;
|
40 | }
|
41 |
|
42 | /**
|
43 | * Process the given raw data with the parser, retrieving the result as a [[ParserResult]] object.
|
44 | *
|
45 | * Multiple requests done without waiting for previous results will be queued and handled in order.
|
46 | *
|
47 | * If parsing of the data fails the returned promise is rejected with a `ScanditEngineError` error.
|
48 | *
|
49 | * @param dataRaw The raw data to be parsed.
|
50 | * @returns A promise resolving to the [[ParserResult]] object.
|
51 | */
|
52 | public parseRawData(dataRaw: Uint8Array): Promise<ParserResult> {
|
53 | let dataString: string = "";
|
54 | try {
|
55 | dataRaw.forEach((byte: number) => {
|
56 | dataString += String.fromCharCode(byte);
|
57 | });
|
58 | dataString = decodeURIComponent(escape(dataString));
|
59 | } catch {
|
60 | dataString = "";
|
61 | }
|
62 |
|
63 | return this.scanner.parseString(this.dataFormat, dataString, this.options);
|
64 | }
|
65 |
|
66 | /**
|
67 | * Process the given string data with the parser, retrieving the result as a [[ParserResult]] object.
|
68 | *
|
69 | * Multiple requests done without waiting for previous results will be queued and handled in order.
|
70 | *
|
71 | * If parsing of the data fails the returned promise is rejected with a `ScanditEngineError` error.
|
72 | *
|
73 | * @param dataString The string to be parsed.
|
74 | * @returns A promise resolving to the [[ParserResult]] object.
|
75 | */
|
76 | public parseString(dataString: string): Promise<ParserResult> {
|
77 | return this.scanner.parseString(this.dataFormat, dataString, this.options);
|
78 | }
|
79 | }
|
80 |
|
81 | // istanbul ignore next
|
82 | export namespace Parser {
|
83 | /**
|
84 | * Data format of a string to be parsed into a set of key-value mappings by the Scandit Parser Library.
|
85 | *
|
86 | * See https://docs.scandit.com/parser/formats.html for more details.
|
87 | */
|
88 | export enum DataFormat {
|
89 | /**
|
90 | * GS1 Application Identifier (AI).
|
91 | *
|
92 | * See: http://www.gs1.org/docs/barcodes/GS1_General_Specifications.pdf.
|
93 | */
|
94 | GS1_AI = 1,
|
95 | /**
|
96 | * Health Industry Bar Code (HIBC).
|
97 | *
|
98 | * See: http://www.hibcc.org.
|
99 | */
|
100 | HIBC = 2,
|
101 | /**
|
102 | * AAMVA Driver License/Identification (DL/ID).
|
103 | *
|
104 | * See: http://www.aamva.org.
|
105 | */
|
106 | DLID = 3,
|
107 | /**
|
108 | * ICAO Machine Readable Travel Document (MRTD).
|
109 | *
|
110 | * See: https://www.icao.int.
|
111 | */
|
112 | MRTD = 4,
|
113 | /**
|
114 | * Swiss QR ISO 20022.
|
115 | *
|
116 | * See: https://www.paymentstandards.ch.
|
117 | */
|
118 | SWISSQR = 5
|
119 | }
|
120 | }
|