mindee 1.3.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/CHANGELOG.md +47 -18
  2. package/README.md +43 -127
  3. package/bin/mindee +3 -0
  4. package/dist/package.json +70 -0
  5. package/dist/src/api/endpoint.d.ts +29 -0
  6. package/dist/src/api/endpoint.js +148 -0
  7. package/dist/src/api/index.d.ts +2 -0
  8. package/dist/src/api/index.js +18 -0
  9. package/dist/src/api/response.d.ts +52 -0
  10. package/dist/src/api/response.js +107 -0
  11. package/dist/src/cli.d.ts +1 -0
  12. package/dist/src/cli.js +117 -0
  13. package/dist/src/client.d.ts +48 -0
  14. package/dist/src/client.js +134 -0
  15. package/dist/src/constants.d.ts +13 -0
  16. package/dist/src/constants.js +54 -0
  17. package/dist/src/documents/custom.d.ts +14 -0
  18. package/dist/src/documents/custom.js +48 -0
  19. package/dist/src/documents/document.d.ts +38 -0
  20. package/dist/{documents → src/documents}/document.js +15 -35
  21. package/dist/src/documents/documentConfig.d.ts +44 -0
  22. package/dist/src/documents/documentConfig.js +121 -0
  23. package/dist/src/documents/financialDocument.d.ts +32 -0
  24. package/dist/src/documents/financialDocument.js +130 -0
  25. package/dist/src/documents/index.d.ts +12 -0
  26. package/dist/src/documents/index.js +20 -0
  27. package/dist/src/documents/invoice.d.ts +37 -0
  28. package/dist/src/documents/invoice.js +303 -0
  29. package/dist/src/documents/passport.d.ts +36 -0
  30. package/dist/src/documents/passport.js +218 -0
  31. package/dist/src/documents/receipt.d.ts +24 -0
  32. package/dist/src/documents/receipt.js +157 -0
  33. package/dist/src/errors/handler.d.ts +7 -0
  34. package/dist/{errors → src/errors}/handler.js +5 -4
  35. package/dist/src/fields/amount.d.ts +13 -0
  36. package/dist/{documents → src}/fields/amount.js +19 -6
  37. package/dist/src/fields/companyRegistration.d.ts +6 -0
  38. package/dist/src/fields/companyRegistration.js +11 -0
  39. package/dist/src/fields/customDocs.d.ts +42 -0
  40. package/dist/src/fields/customDocs.js +61 -0
  41. package/dist/src/fields/date.d.ts +12 -0
  42. package/dist/src/fields/date.js +25 -0
  43. package/dist/src/fields/field.d.ts +74 -0
  44. package/dist/{documents → src}/fields/field.js +48 -27
  45. package/dist/src/fields/fullText.d.ts +24 -0
  46. package/dist/src/fields/fullText.js +89 -0
  47. package/dist/src/fields/index.d.ts +11 -0
  48. package/dist/src/fields/index.js +27 -0
  49. package/dist/src/fields/locale.d.ts +13 -0
  50. package/dist/{documents → src}/fields/locale.js +20 -5
  51. package/dist/src/fields/orientation.d.ts +10 -0
  52. package/dist/{documents → src}/fields/orientation.js +5 -5
  53. package/dist/src/fields/paymentDetails.d.ts +31 -0
  54. package/dist/{documents → src}/fields/paymentDetails.js +14 -6
  55. package/dist/src/fields/tax.d.ts +30 -0
  56. package/dist/src/fields/tax.js +51 -0
  57. package/dist/src/geometry.d.ts +59 -0
  58. package/dist/src/geometry.js +113 -0
  59. package/dist/src/index.d.ts +2 -0
  60. package/dist/src/index.js +12 -0
  61. package/dist/src/inputs.d.ts +69 -0
  62. package/dist/src/inputs.js +223 -0
  63. package/dist/src/logger.d.ts +20 -0
  64. package/dist/src/logger.js +35 -0
  65. package/package.json +45 -26
  66. package/.eslintrc +0 -24
  67. package/.mocharc.yaml +0 -2
  68. package/.nyc_output/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
  69. package/.nyc_output/processinfo/f57ba3bc-1777-4ecc-81e1-230327c2a401.json +0 -1
  70. package/.nyc_output/processinfo/index.json +0 -1
  71. package/.prettierrc +0 -4
  72. package/.tool-versions +0 -1
  73. package/CODE_OF_CONDUCT.md +0 -63
  74. package/CONTRIBUTING.md +0 -84
  75. package/ISSUE_TEMPLATE.md +0 -45
  76. package/PULL_REQUEST_TEMPLATE.md +0 -45
  77. package/babel.config.json +0 -10
  78. package/coverage/coverage.json +0 -1
  79. package/coverage/lcov-report/base.css +0 -224
  80. package/coverage/lcov-report/block-navigation.js +0 -79
  81. package/coverage/lcov-report/favicon.png +0 -0
  82. package/coverage/lcov-report/index.html +0 -171
  83. package/coverage/lcov-report/prettify.css +0 -1
  84. package/coverage/lcov-report/prettify.js +0 -2
  85. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  86. package/coverage/lcov-report/sorter.js +0 -170
  87. package/coverage/lcov.info +0 -1595
  88. package/coverage.lcov +0 -1595
  89. package/dist/api/financialDocument.js +0 -51
  90. package/dist/api/index.js +0 -13
  91. package/dist/api/invoice.js +0 -59
  92. package/dist/api/object.js +0 -80
  93. package/dist/api/receipt.js +0 -45
  94. package/dist/api/request.js +0 -90
  95. package/dist/api/response.js +0 -91
  96. package/dist/documents/fields/date.js +0 -17
  97. package/dist/documents/fields/index.js +0 -17
  98. package/dist/documents/fields/tax.js +0 -42
  99. package/dist/documents/financialDocument.js +0 -216
  100. package/dist/documents/index.js +0 -13
  101. package/dist/documents/invoice.js +0 -361
  102. package/dist/documents/receipt.js +0 -231
  103. package/dist/index.js +0 -23
  104. package/dist/inputs.js +0 -208
  105. package/dist/logger.js +0 -34
  106. package/examples/.env.example +0 -2
  107. package/examples/documents/invoices/credit_note.pdf +0 -900
  108. package/examples/documents/invoices/invoice.pdf +0 -0
  109. package/examples/documents/invoices/invoice_6p.pdf +0 -0
  110. package/examples/documents/receipts/receipt.jpg +0 -0
  111. package/examples/financialDocument.js +0 -32
  112. package/examples/invoice.js +0 -74
  113. package/examples/receipt.js +0 -45
  114. package/lib/api/financialDocument.js +0 -59
  115. package/lib/api/index.js +0 -7
  116. package/lib/api/invoice.js +0 -73
  117. package/lib/api/object.js +0 -90
  118. package/lib/api/receipt.js +0 -58
  119. package/lib/api/request.js +0 -84
  120. package/lib/api/response.js +0 -92
  121. package/lib/documents/document.js +0 -97
  122. package/lib/documents/fields/amount.js +0 -35
  123. package/lib/documents/fields/date.js +0 -36
  124. package/lib/documents/fields/field.js +0 -102
  125. package/lib/documents/fields/index.js +0 -9
  126. package/lib/documents/fields/locale.js +0 -35
  127. package/lib/documents/fields/orientation.js +0 -33
  128. package/lib/documents/fields/paymentDetails.js +0 -71
  129. package/lib/documents/fields/tax.js +0 -57
  130. package/lib/documents/financialDocument.js +0 -280
  131. package/lib/documents/index.js +0 -7
  132. package/lib/documents/invoice.js +0 -451
  133. package/lib/documents/receipt.js +0 -332
  134. package/lib/errors/handler.js +0 -19
  135. package/lib/index.js +0 -40
  136. package/lib/inputs.js +0 -190
  137. package/lib/logger.js +0 -52
  138. package/mindee/api/financialDocument.js +0 -47
  139. package/mindee/api/index.js +0 -5
  140. package/mindee/api/invoice.js +0 -53
  141. package/mindee/api/object.js +0 -82
  142. package/mindee/api/receipt.js +0 -39
  143. package/mindee/api/request.js +0 -72
  144. package/mindee/api/response.js +0 -80
  145. package/mindee/documents/document.js +0 -69
  146. package/mindee/documents/fields/amount.js +0 -25
  147. package/mindee/documents/fields/date.js +0 -29
  148. package/mindee/documents/fields/field.js +0 -86
  149. package/mindee/documents/fields/index.js +0 -7
  150. package/mindee/documents/fields/locale.js +0 -30
  151. package/mindee/documents/fields/orientation.js +0 -24
  152. package/mindee/documents/fields/paymentDetails.js +0 -52
  153. package/mindee/documents/fields/tax.js +0 -47
  154. package/mindee/documents/financialDocument.js +0 -269
  155. package/mindee/documents/index.js +0 -5
  156. package/mindee/documents/invoice.js +0 -456
  157. package/mindee/documents/receipt.js +0 -287
  158. package/mindee/errors/handler.js +0 -16
  159. package/mindee/index.js +0 -36
  160. package/mindee/inputs.js +0 -158
  161. package/mindee/logger.js +0 -33
@@ -0,0 +1,303 @@
1
+ "use strict";
2
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
+ };
7
+ var _Invoice_instances, _Invoice_initFromApiPrediction, _Invoice_checklist, _Invoice_reconstruct, _Invoice_reconstructTotalTax, _Invoice_reconstructTotalTaxFromTotals, _Invoice_reconstructTotalExcl, _Invoice_reconstructTotalIncl;
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.Invoice = void 0;
10
+ const document_1 = require("./document");
11
+ const fields_1 = require("../fields");
12
+ class Invoice extends document_1.Document {
13
+ /**
14
+ * @param {Object} apiPrediction - Json parsed prediction from HTTP response
15
+ * @param {Input} inputFile - input file given to parse the document
16
+ * @param {number} pageId - Page ID for multi-page document
17
+ * @param {FullText} fullText - full OCR extracted text
18
+ */
19
+ constructor({ apiPrediction, inputFile = undefined, fullText = undefined, pageId = undefined, }) {
20
+ super(inputFile, pageId, fullText);
21
+ _Invoice_instances.add(this);
22
+ this.companyRegistration = [];
23
+ this.taxes = [];
24
+ this.paymentDetails = [];
25
+ this.customerCompanyRegistration = [];
26
+ __classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_initFromApiPrediction).call(this, apiPrediction, pageId);
27
+ __classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_checklist).call(this);
28
+ __classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_reconstruct).call(this);
29
+ }
30
+ toString() {
31
+ const taxes = this.taxes.map((item) => item.toString()).join("\n ");
32
+ const paymentDetails = this.paymentDetails
33
+ .map((item) => item.toString())
34
+ .join("\n ");
35
+ const customerCompanyRegistration = this.customerCompanyRegistration
36
+ .map((item) => item.toString())
37
+ .join("; ");
38
+ const companyRegistration = this.companyRegistration
39
+ .map((item) => item.toString())
40
+ .join("; ");
41
+ const outStr = `-----Invoice data-----
42
+ Filename: ${this.filename}
43
+ Invoice number: ${this.invoiceNumber}
44
+ Total amount including taxes: ${this.totalIncl}
45
+ Total amount excluding taxes: ${this.totalExcl}
46
+ Invoice date: ${this.date}
47
+ Invoice due date: ${this.dueDate}
48
+ Supplier name: ${this.supplier}
49
+ Supplier address: ${this.supplierAddress}
50
+ Customer name: ${this.customerName}
51
+ Customer company registration: ${customerCompanyRegistration}
52
+ Customer address: ${this.customerAddress}
53
+ Payment details: ${paymentDetails}
54
+ Company numbers: ${companyRegistration}
55
+ Taxes: ${taxes}
56
+ Total taxes: ${this.totalTax}
57
+ Locale: ${this.locale}
58
+ ----------------------
59
+ `;
60
+ return Invoice.cleanOutString(outStr);
61
+ }
62
+ taxesMatchTotalIncl() {
63
+ // Check taxes and total include exist
64
+ if (this.taxes.length === 0 || this.totalIncl.value === undefined)
65
+ return false;
66
+ // Reconstruct totalIncl from taxes
67
+ let totalVat = 0;
68
+ let reconstructedTotal = 0;
69
+ this.taxes.forEach((tax) => {
70
+ if (tax.value === undefined || !tax.rate)
71
+ return false;
72
+ totalVat += tax.value;
73
+ reconstructedTotal += tax.value + (100 * tax.value) / tax.rate;
74
+ });
75
+ // Sanity check
76
+ if (totalVat <= 0)
77
+ return false;
78
+ // Crate epsilon
79
+ const eps = 1 / (100 * totalVat);
80
+ if (this.totalIncl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
81
+ reconstructedTotal <= this.totalIncl.value * (1 + eps) + 0.02) {
82
+ this.taxes.forEach((tax) => {
83
+ tax.confidence = 1.0;
84
+ });
85
+ this.totalTax.confidence = 1.0;
86
+ this.totalIncl.confidence = 1.0;
87
+ return true;
88
+ }
89
+ return false;
90
+ }
91
+ /**
92
+ *
93
+ */
94
+ taxesMatchTotalExcl() {
95
+ // Check taxes and total amount exist
96
+ if (this.taxes.length === 0 || this.totalExcl.value === undefined) {
97
+ return false;
98
+ }
99
+ // Reconstruct total_incl from taxes
100
+ let totalVat = 0;
101
+ let reconstructedTotal = 0;
102
+ this.taxes.forEach((tax) => {
103
+ if (tax.value === undefined || !tax.rate) {
104
+ return false;
105
+ }
106
+ totalVat += tax.value;
107
+ reconstructedTotal += (100 * tax.value) / tax.rate;
108
+ });
109
+ // Sanity check
110
+ if (totalVat <= 0)
111
+ return false;
112
+ // Crate epsilon
113
+ const eps = 1 / (100 * totalVat);
114
+ if (this.totalExcl.value * (1 - eps) - 0.02 <= reconstructedTotal &&
115
+ reconstructedTotal <= this.totalExcl.value * (1 + eps) + 0.02) {
116
+ this.taxes.forEach((tax) => {
117
+ tax.confidence = 1.0;
118
+ });
119
+ this.totalTax.confidence = 1.0;
120
+ this.totalExcl.confidence = 1.0;
121
+ return true;
122
+ }
123
+ return false;
124
+ }
125
+ taxesAndTotalExclMatchTotalIncl() {
126
+ if (this.totalExcl.value === undefined ||
127
+ this.taxes.length === 0 ||
128
+ this.totalIncl.value === undefined)
129
+ return false;
130
+ let totalVat = 0;
131
+ this.taxes.forEach((tax) => (totalVat += tax.value || 0));
132
+ const reconstructedTotal = totalVat + this.totalExcl.value;
133
+ if (totalVat <= 0)
134
+ return false;
135
+ if (this.totalIncl.value - 0.01 <= reconstructedTotal &&
136
+ reconstructedTotal <= this.totalIncl.value + 0.01) {
137
+ this.taxes.forEach((tax) => {
138
+ tax.confidence = 1.0;
139
+ });
140
+ this.totalTax.confidence = 1.0;
141
+ this.totalIncl.confidence = 1.0;
142
+ return true;
143
+ }
144
+ return false;
145
+ }
146
+ }
147
+ exports.Invoice = Invoice;
148
+ _Invoice_instances = new WeakSet(), _Invoice_initFromApiPrediction = function _Invoice_initFromApiPrediction(apiPrediction, pageId) {
149
+ this.locale = new fields_1.Locale({
150
+ prediction: apiPrediction.locale,
151
+ valueKey: "language",
152
+ });
153
+ this.documentType = new fields_1.BaseField({
154
+ prediction: apiPrediction.document_type,
155
+ valueKey: "value",
156
+ });
157
+ this.totalIncl = new fields_1.Amount({
158
+ prediction: apiPrediction.total_incl,
159
+ valueKey: "value",
160
+ pageId: pageId,
161
+ });
162
+ this.totalTax = new fields_1.Amount({
163
+ prediction: { value: undefined, confidence: 0.0 },
164
+ valueKey: "value",
165
+ pageId: pageId,
166
+ });
167
+ this.totalExcl = new fields_1.Amount({
168
+ prediction: apiPrediction.total_excl,
169
+ valueKey: "value",
170
+ pageId: pageId,
171
+ });
172
+ this.date = new fields_1.DateField({
173
+ prediction: apiPrediction.date,
174
+ pageId,
175
+ });
176
+ apiPrediction.taxes.map((prediction) => this.taxes.push(new fields_1.TaxField({
177
+ prediction: prediction,
178
+ pageId: pageId,
179
+ valueKey: "value",
180
+ rateKey: "rate",
181
+ codeKey: "code",
182
+ })));
183
+ this.companyRegistration = apiPrediction.company_registration.map(function (prediction) {
184
+ return new fields_1.CompanyRegistration({
185
+ prediction: prediction,
186
+ pageId: pageId,
187
+ });
188
+ });
189
+ this.dueDate = new fields_1.DateField({
190
+ prediction: apiPrediction.due_date,
191
+ pageId: pageId,
192
+ });
193
+ this.invoiceNumber = new fields_1.Field({
194
+ prediction: apiPrediction.invoice_number,
195
+ pageId: pageId,
196
+ });
197
+ this.supplier = new fields_1.Field({
198
+ prediction: apiPrediction.supplier,
199
+ pageId: pageId,
200
+ });
201
+ this.supplierAddress = new fields_1.Field({
202
+ prediction: apiPrediction.supplier_address,
203
+ pageId: pageId,
204
+ });
205
+ this.customerName = new fields_1.Field({
206
+ prediction: apiPrediction.customer,
207
+ pageId: pageId,
208
+ });
209
+ this.customerAddress = new fields_1.Field({
210
+ prediction: apiPrediction.customer_address,
211
+ pageId: pageId,
212
+ });
213
+ apiPrediction.customer_company_registration.map((prediction) => this.customerCompanyRegistration.push(new fields_1.CompanyRegistration({
214
+ prediction: prediction,
215
+ pageId: pageId,
216
+ })));
217
+ apiPrediction.payment_details.map((prediction) => this.paymentDetails.push(new fields_1.PaymentDetails({
218
+ prediction: prediction,
219
+ pageId: pageId,
220
+ })));
221
+ if (pageId !== undefined) {
222
+ this.orientation = new fields_1.Orientation({
223
+ prediction: apiPrediction.orientation,
224
+ pageId: pageId,
225
+ });
226
+ }
227
+ }, _Invoice_checklist = function _Invoice_checklist() {
228
+ this.checklist = {
229
+ taxesMatchTotalIncl: this.taxesMatchTotalIncl(),
230
+ taxesMatchTotalExcl: this.taxesMatchTotalExcl(),
231
+ taxesAndTotalExclMatchTotalIncl: this.taxesAndTotalExclMatchTotalIncl(),
232
+ };
233
+ }, _Invoice_reconstruct = function _Invoice_reconstruct() {
234
+ __classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_reconstructTotalTax).call(this);
235
+ __classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_reconstructTotalExcl).call(this);
236
+ __classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_reconstructTotalIncl).call(this);
237
+ __classPrivateFieldGet(this, _Invoice_instances, "m", _Invoice_reconstructTotalTaxFromTotals).call(this);
238
+ }, _Invoice_reconstructTotalTax = function _Invoice_reconstructTotalTax() {
239
+ if (this.taxes.length > 0) {
240
+ const totalTax = {
241
+ value: this.taxes.reduce((acc, tax) => {
242
+ return tax.value !== undefined ? acc + tax.value : acc;
243
+ }, 0),
244
+ confidence: fields_1.Field.arrayConfidence(this.taxes),
245
+ };
246
+ if (totalTax.value > 0)
247
+ this.totalTax = new fields_1.Amount({
248
+ prediction: totalTax,
249
+ valueKey: "value",
250
+ reconstructed: true,
251
+ });
252
+ }
253
+ }, _Invoice_reconstructTotalTaxFromTotals = function _Invoice_reconstructTotalTaxFromTotals() {
254
+ if (this.totalTax.value !== undefined ||
255
+ this.totalExcl.value === undefined ||
256
+ this.totalIncl.value === undefined) {
257
+ return;
258
+ }
259
+ const totalTax = {
260
+ value: this.totalIncl.value - this.totalExcl.value,
261
+ confidence: this.totalIncl.confidence * this.totalExcl.confidence,
262
+ };
263
+ if (totalTax.value >= 0) {
264
+ this.totalTax = new fields_1.Amount({
265
+ prediction: totalTax,
266
+ valueKey: "value",
267
+ reconstructed: true,
268
+ });
269
+ }
270
+ }, _Invoice_reconstructTotalExcl = function _Invoice_reconstructTotalExcl() {
271
+ if (this.totalIncl.value === undefined ||
272
+ this.taxes.length === 0 ||
273
+ this.totalExcl.value !== undefined) {
274
+ return;
275
+ }
276
+ const totalExcl = {
277
+ value: this.totalIncl.value - fields_1.Field.arraySum(this.taxes),
278
+ confidence: fields_1.Field.arrayConfidence(this.taxes) *
279
+ this.totalIncl.confidence,
280
+ };
281
+ this.totalExcl = new fields_1.Amount({
282
+ prediction: totalExcl,
283
+ valueKey: "value",
284
+ reconstructed: true,
285
+ });
286
+ }, _Invoice_reconstructTotalIncl = function _Invoice_reconstructTotalIncl() {
287
+ if (!(this.totalExcl.value === undefined ||
288
+ this.taxes.length === 0 ||
289
+ this.totalIncl.value !== undefined)) {
290
+ const totalIncl = {
291
+ value: this.totalExcl.value +
292
+ this.taxes.reduce((acc, tax) => {
293
+ return tax.value ? acc + tax.value : acc;
294
+ }, 0.0),
295
+ confidence: fields_1.Field.arrayConfidence(this.taxes) * this.totalExcl.confidence,
296
+ };
297
+ this.totalIncl = new fields_1.Amount({
298
+ prediction: totalIncl,
299
+ valueKey: "value",
300
+ reconstructed: true,
301
+ });
302
+ }
303
+ };
@@ -0,0 +1,36 @@
1
+ import { Document, DocumentConstructorProps } from "./document";
2
+ import { Field, DateField } from "../fields";
3
+ export declare class Passport extends Document {
4
+ #private;
5
+ country: Field;
6
+ idNumber: Field;
7
+ birthDate: DateField;
8
+ expiryDate: DateField;
9
+ issuanceDate: DateField;
10
+ birthPlace: Field;
11
+ gender: Field;
12
+ surname: Field;
13
+ mrz1: Field;
14
+ mrz2: Field;
15
+ givenNames: Field[];
16
+ fullName: Field;
17
+ mrz: Field;
18
+ /**
19
+ * @param {Object} apiPrediction - Json parsed prediction from HTTP response
20
+ * @param {Input} inputFile - input file given to parse the document
21
+ * @param {number} pageId - Page ID for multi-page document
22
+ * @param {FullText} fullText - full OCR extracted text
23
+ */
24
+ constructor({ apiPrediction, inputFile, pageId, }: DocumentConstructorProps);
25
+ static convertMRZDateToDatetime(dateString: string): Date;
26
+ toString(): string;
27
+ isExpired(): boolean;
28
+ private isMRZValid;
29
+ private isBirthDateValid;
30
+ private isExpiryDateValid;
31
+ private isIdNumberValid;
32
+ private isSurnameValid;
33
+ private isCountryValid;
34
+ private constructFullName;
35
+ private constructMRZ;
36
+ }
@@ -0,0 +1,218 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
26
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
27
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
28
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
29
+ };
30
+ var _Passport_instances, _Passport_checklist;
31
+ Object.defineProperty(exports, "__esModule", { value: true });
32
+ exports.Passport = void 0;
33
+ const document_1 = require("./document");
34
+ const fields_1 = require("../fields");
35
+ // @ts-ignore
36
+ const MRZ = __importStar(require("mrz"));
37
+ class Passport extends document_1.Document {
38
+ /**
39
+ * @param {Object} apiPrediction - Json parsed prediction from HTTP response
40
+ * @param {Input} inputFile - input file given to parse the document
41
+ * @param {number} pageId - Page ID for multi-page document
42
+ * @param {FullText} fullText - full OCR extracted text
43
+ */
44
+ constructor({ apiPrediction, inputFile = undefined, pageId = undefined, }) {
45
+ super(inputFile, pageId);
46
+ _Passport_instances.add(this);
47
+ this.givenNames = [];
48
+ this.country = new fields_1.Field({
49
+ prediction: apiPrediction.country,
50
+ pageId: pageId,
51
+ });
52
+ this.idNumber = new fields_1.Field({
53
+ prediction: apiPrediction.id_number,
54
+ pageId: pageId,
55
+ });
56
+ this.birthDate = new fields_1.DateField({
57
+ prediction: apiPrediction.birth_date,
58
+ pageId: pageId,
59
+ });
60
+ this.expiryDate = new fields_1.DateField({
61
+ prediction: apiPrediction.expiry_date,
62
+ pageId: pageId,
63
+ });
64
+ this.issuanceDate = new fields_1.DateField({
65
+ prediction: apiPrediction.issuance_date,
66
+ pageId: pageId,
67
+ });
68
+ this.birthPlace = new fields_1.Field({
69
+ prediction: apiPrediction.birth_place,
70
+ pageId: pageId,
71
+ });
72
+ this.gender = new fields_1.Field({
73
+ prediction: apiPrediction.gender,
74
+ pageId: pageId,
75
+ });
76
+ this.surname = new fields_1.Field({
77
+ prediction: apiPrediction.surname,
78
+ pageId: pageId,
79
+ });
80
+ this.mrz1 = new fields_1.Field({
81
+ prediction: apiPrediction.mrz1,
82
+ pageId: pageId,
83
+ });
84
+ this.mrz2 = new fields_1.Field({
85
+ prediction: apiPrediction.mrz2,
86
+ pageId: pageId,
87
+ });
88
+ apiPrediction.given_names.map((prediction) => this.givenNames.push(new fields_1.Field({
89
+ prediction: prediction,
90
+ pageId: pageId,
91
+ })));
92
+ this.fullName = this.constructFullName(pageId);
93
+ this.mrz = this.constructMRZ(pageId);
94
+ __classPrivateFieldGet(this, _Passport_instances, "m", _Passport_checklist).call(this);
95
+ }
96
+ static convertMRZDateToDatetime(dateString) {
97
+ const year = parseInt(dateString.substring(0, 2));
98
+ const month = parseInt(dateString.substring(2, 4));
99
+ const day = parseInt(dateString.substring(4, 6));
100
+ // month is 0 indexed, day is 1 indexed... because JavaScript ...
101
+ return new Date(year, month - 1, day, 0, 0, 0, 0);
102
+ }
103
+ toString() {
104
+ const outStr = `-----Passport data-----
105
+ Filename: ${this.filename}
106
+ Full name: ${this.fullName}
107
+ Given names: ${this.givenNames.map((name) => name.value).join(" ")}
108
+ Surname: ${this.surname}
109
+ Country: ${this.country}
110
+ ID Number: ${this.idNumber}
111
+ Issuance date: ${this.issuanceDate}
112
+ Birth date: ${this.birthDate}
113
+ Expiry date: ${this.expiryDate}
114
+ MRZ 1: ${this.mrz1}
115
+ MRZ 2: ${this.mrz2}
116
+ MRZ: ${this.mrz}
117
+ ----------------------
118
+ `;
119
+ return Passport.cleanOutString(outStr);
120
+ }
121
+ isExpired() {
122
+ const dateTime = new Date();
123
+ if (this.expiryDate.dateObject) {
124
+ return this.expiryDate.dateObject < dateTime;
125
+ }
126
+ return true;
127
+ }
128
+ isMRZValid(mrz) {
129
+ const check = mrz.valid;
130
+ if (check)
131
+ this.mrz.confidence = 1.0;
132
+ return check;
133
+ }
134
+ isBirthDateValid(mrz) {
135
+ if (this.birthDate.dateObject === undefined || !mrz.fields.birthDate) {
136
+ return false;
137
+ }
138
+ const mrzDate = Passport.convertMRZDateToDatetime(mrz.fields.birthDate);
139
+ const check = fields_1.DateField.compareDates(this.birthDate.dateObject, mrzDate);
140
+ if (check) {
141
+ this.birthDate.confidence = 1.0;
142
+ }
143
+ return check;
144
+ }
145
+ isExpiryDateValid(mrz) {
146
+ if (this.expiryDate.dateObject === undefined ||
147
+ !mrz.fields.expirationDate) {
148
+ return false;
149
+ }
150
+ const mrzDate = Passport.convertMRZDateToDatetime(mrz.fields.expirationDate);
151
+ const check = fields_1.DateField.compareDates(this.expiryDate.dateObject, mrzDate);
152
+ if (check) {
153
+ this.expiryDate.confidence = 1.0;
154
+ }
155
+ return check;
156
+ }
157
+ isIdNumberValid(mrz) {
158
+ const check = mrz.fields.documentNumber &&
159
+ mrz.fields.documentNumber === this.idNumber.value;
160
+ if (check)
161
+ this.idNumber.confidence = 1.0;
162
+ return check;
163
+ }
164
+ isSurnameValid(mrz) {
165
+ const check = mrz.fields.lastName === this.surname.value;
166
+ if (check)
167
+ this.surname.confidence = 1.0;
168
+ return check;
169
+ }
170
+ isCountryValid(mrz) {
171
+ const check = mrz.fields.nationality === this.country.value;
172
+ if (check)
173
+ this.country.confidence = 1.0;
174
+ return check;
175
+ }
176
+ constructFullName(pageNumber) {
177
+ if (this.surname &&
178
+ this.givenNames.length > 0 &&
179
+ this.givenNames[0].value) {
180
+ const fullName = {
181
+ value: `${this.givenNames[0].value} ${this.surname.value}`,
182
+ confidence: fields_1.Field.arrayConfidence([this.surname, this.givenNames[0]]),
183
+ };
184
+ return new fields_1.Field({
185
+ prediction: fullName,
186
+ pageId: pageNumber,
187
+ reconstructed: true,
188
+ });
189
+ }
190
+ }
191
+ constructMRZ(pageNumber) {
192
+ if (this.mrz1.value && this.mrz2.value) {
193
+ const mrz = {
194
+ value: `${this.mrz1.value}${this.mrz2.value}`,
195
+ confidence: fields_1.Field.arrayConfidence([this.mrz1, this.mrz2]),
196
+ };
197
+ return new fields_1.Field({
198
+ prediction: mrz,
199
+ pageId: pageNumber,
200
+ reconstructed: true,
201
+ });
202
+ }
203
+ }
204
+ }
205
+ exports.Passport = Passport;
206
+ _Passport_instances = new WeakSet(), _Passport_checklist = function _Passport_checklist() {
207
+ if (this.mrz1.value && this.mrz2.value) {
208
+ const mrz = MRZ.parse([this.mrz1.value, this.mrz2.value]);
209
+ this.checklist = {
210
+ mrzValid: this.isMRZValid(mrz),
211
+ mrzValidBirthDate: this.isBirthDateValid(mrz),
212
+ mrzValidExpiryDate: this.isExpiryDateValid(mrz),
213
+ mrzValidIdNumber: this.isIdNumberValid(mrz),
214
+ mrzValidSurname: this.isSurnameValid(mrz),
215
+ mrzValidCountry: this.isCountryValid(mrz),
216
+ };
217
+ }
218
+ };
@@ -0,0 +1,24 @@
1
+ import { Document, DocumentConstructorProps } from "./document";
2
+ import { TaxField, Field, Amount, Locale, Orientation, DateField } from "../fields";
3
+ export declare class Receipt extends Document {
4
+ #private;
5
+ locale: Locale;
6
+ totalIncl: Amount;
7
+ date: DateField;
8
+ /** Receipt category as seen on the receipt. */
9
+ category: Field;
10
+ merchantName: Field;
11
+ time: Field;
12
+ orientation: Orientation | undefined;
13
+ totalTax: Amount;
14
+ totalExcl: Amount;
15
+ taxes: TaxField[];
16
+ /**
17
+ * @param {Object} apiPrediction - Json parsed prediction from HTTP response
18
+ * @param {Input} inputFile - input file given to parse the document
19
+ * @param {number} pageId - Page ID for multi-page document
20
+ * @param {FullText} fullText - full OCR extracted text
21
+ */
22
+ constructor({ apiPrediction, inputFile, fullText, pageId, }: DocumentConstructorProps);
23
+ toString(): string;
24
+ }