Skip to content

Commit 2cadcac

Browse files
committed
feat: add verification for dates
1 parent eba6389 commit 2cadcac

File tree

1 file changed

+91
-98
lines changed

1 file changed

+91
-98
lines changed

js/util.js

Lines changed: 91 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -20,50 +20,99 @@ export function createPendingPromise() {
2020
* @returns {Object} An object with key-value pairs of the extracted fields.
2121
*/
2222
export function extractDocumentFields(result) {
23-
const parseResultInfo = {};
24-
if (!result.exception) {
25-
const type = result.getFieldValue("documentCode");
26-
const documentNumber = type === "P" ? "passportNumber" : "documentNumber";
27-
const documentType = JSON.parse(result.jsonString).CodeType;
28-
const birthYear = result.getFieldValue("birthYear");
29-
const birthYearBase = parseInt(birthYear) > new Date().getFullYear() % 100 ? "19" : "20";
30-
const fullBirthYear = `${birthYearBase}${birthYear}`;
31-
32-
const expiryYear = result.getFieldValue("expiryYear");
33-
const expiryYearBase = parseInt(expiryYear) >= 60 ? "19" : "20";
34-
const fullExpiryYear = `${expiryYearBase}${expiryYear}`;
35-
36-
parseResultInfo["Document Type"] = documentType;
37-
parseResultInfo["Surname"] = {
38-
text: result.getFieldValue("primaryIdentifier"),
39-
status: result.getFieldValidationStatus("primaryIdentifier"),
40-
};
41-
parseResultInfo["Given Name"] = {
42-
text: result.getFieldValue("secondaryIdentifier"),
43-
status: result.getFieldValidationStatus("secondaryIdentifier"),
44-
};
45-
parseResultInfo["Nationality"] = {
46-
text: result.getFieldValue("nationality"),
47-
status: result.getFieldValidationStatus("nationality"),
48-
};
49-
parseResultInfo["Document Number"] = {
50-
text: result.getFieldValue(documentNumber),
51-
status: result.getFieldValidationStatus(documentNumber),
52-
};
53-
parseResultInfo["Issuing State"] = {
54-
text: result.getFieldValue("issuingState"),
55-
status: result.getFieldValidationStatus("issuingState"),
56-
};
57-
parseResultInfo["Sex"] = {
58-
text: result.getFieldValue("sex"),
59-
status: result.getFieldValidationStatus("sex"),
23+
if (!result || result.exception) {
24+
return {};
25+
}
26+
27+
const fieldWithStatus = (fieldName) => ({
28+
text: result.getFieldValue(fieldName),
29+
status: result.getFieldValidationStatus(fieldName),
30+
});
31+
32+
const parseDate = (yearField, monthField, dayField) => {
33+
const year = result.getFieldValue(yearField);
34+
const currentYear = new Date().getFullYear() % 100;
35+
const baseYear =
36+
yearField === "expiryYear" ? (parseInt(year) >= 60 ? "19" : "20") : parseInt(year) > currentYear ? "19" : "20";
37+
38+
return {
39+
text: `${baseYear}${year}-${result.getFieldValue(monthField)}-${result.getFieldValue(dayField)}`,
40+
status: [yearField, monthField, dayField].every((field) => result.getFieldValidationStatus(field))
41+
? Dynamsoft.DCP.EnumValidationStatus.VS_SUCCEEDED
42+
: Dynamsoft.DCP.EnumValidationStatus.VS_FAILED,
6043
};
61-
parseResultInfo["Date of Birth (YYYY-MM-DD)"] =
62-
fullBirthYear + "-" + result.getFieldValue("birthMonth") + "-" + result.getFieldValue("birthDay");
63-
parseResultInfo["Date of Expiry (YYYY-MM-DD)"] =
64-
fullExpiryYear + "-" + result.getFieldValue("expiryMonth") + "-" + result.getFieldValue("expiryDay");
44+
};
45+
46+
const documentType = result.getFieldValue("documentCode");
47+
const documentNumberField = documentType === "P" ? "passportNumber" : "documentNumber";
48+
49+
return {
50+
Surname: fieldWithStatus("primaryIdentifier"),
51+
"Given Name": fieldWithStatus("secondaryIdentifier"),
52+
Nationality: fieldWithStatus("nationality"),
53+
"Document Number": fieldWithStatus(documentNumberField),
54+
"Issuing State": fieldWithStatus("issuingState"),
55+
Sex: fieldWithStatus("sex"),
56+
"Date of Birth (YYYY-MM-DD)": parseDate("birthYear", "birthMonth", "birthDay"),
57+
"Date of Expiry (YYYY-MM-DD)": parseDate("expiryYear", "expiryMonth", "expiryDay"),
58+
"Document Type": JSON.parse(result.jsonString).CodeType,
59+
};
60+
}
61+
62+
/**
63+
* Create an HTML paragraph element containing the document field name and value.
64+
*
65+
* @param {string} field - The document field name.
66+
* @param {string} value - The document field value.
67+
* @returns {HTMLElement} The paragraph element containing the formatted document field name and value.
68+
*/
69+
export function resultToHTMLElement(field, value) {
70+
const p = document.createElement("p");
71+
p.className = "parsed-filed";
72+
const spanFieldName = document.createElement("span");
73+
spanFieldName.className = "field-name";
74+
const spanValue = document.createElement("span");
75+
spanValue.className = "field-value";
76+
const statusIcon = document.createElement("span");
77+
statusIcon.className = "status-icon";
78+
79+
// Define success and failed icons
80+
const icons = {
81+
success: `<svg class="w-4 h-4" viewBox="0 0 20 20" fill="currentColor">
82+
<path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"/>
83+
</svg>`,
84+
failed: `<svg class="w-4 h-4" viewBox="0 0 20 20" fill="currentColor">
85+
<path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"/>
86+
</svg>`,
87+
};
88+
89+
// Handle validation status based on EnumValidationStatus
90+
switch (value?.status) {
91+
case Dynamsoft.DCP.EnumValidationStatus.VS_SUCCEEDED:
92+
statusIcon.innerHTML = icons.success;
93+
statusIcon.className += " status-success";
94+
statusIcon.title = "Validation passed";
95+
break;
96+
case Dynamsoft.DCP.EnumValidationStatus.VS_FAILED:
97+
statusIcon.innerHTML = icons.failed;
98+
statusIcon.className += " status-failed";
99+
statusIcon.title = "Validation failed";
100+
break;
101+
case Dynamsoft.DCP.EnumValidationStatus.VS_NONE:
102+
default:
103+
// Don't add any icon for VS_NONE
104+
statusIcon.style.display = "none";
105+
break;
65106
}
66-
return parseResultInfo;
107+
108+
spanFieldName.innerText = `${field}`;
109+
spanFieldName.append(statusIcon);
110+
spanValue.innerText = `${value?.text || value || "Not detected"}`;
111+
112+
p.appendChild(spanFieldName);
113+
p.appendChild(spanValue);
114+
115+
return p;
67116
}
68117

69118
/**
@@ -129,62 +178,6 @@ export function getVisibleRegionOfVideo() {
129178
return regionInPixels;
130179
}
131180

132-
/**
133-
* Create an HTML paragraph element containing the document field name and value.
134-
*
135-
* @param {string} field - The document field name.
136-
* @param {string} value - The document field value.
137-
* @returns {HTMLElement} The paragraph element containing the formatted document field name and value.
138-
*/
139-
export function resultToHTMLElement(field, value) {
140-
const p = document.createElement("p");
141-
p.className = "parsed-filed";
142-
const spanFieldName = document.createElement("span");
143-
spanFieldName.className = "field-name";
144-
const spanValue = document.createElement("span");
145-
spanValue.className = "field-value";
146-
const statusIcon = document.createElement("span");
147-
statusIcon.className = "status-icon";
148-
149-
// Define success and failed icons
150-
const icons = {
151-
success: `<svg class="w-4 h-4" viewBox="0 0 20 20" fill="currentColor">
152-
<path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"/>
153-
</svg>`,
154-
failed: `<svg class="w-4 h-4" viewBox="0 0 20 20" fill="currentColor">
155-
<path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd"/>
156-
</svg>`,
157-
};
158-
159-
// Handle validation status based on EnumValidationStatus
160-
switch (value?.status) {
161-
case Dynamsoft.DCP.EnumValidationStatus.VS_SUCCEEDED:
162-
statusIcon.innerHTML = icons.success;
163-
statusIcon.className += " status-success";
164-
statusIcon.title = "Validation passed";
165-
break;
166-
case Dynamsoft.DCP.EnumValidationStatus.VS_FAILED:
167-
statusIcon.innerHTML = icons.failed;
168-
statusIcon.className += " status-failed";
169-
statusIcon.title = "Validation failed";
170-
break;
171-
case Dynamsoft.DCP.EnumValidationStatus.VS_NONE:
172-
default:
173-
// Don't add any icon for VS_NONE
174-
statusIcon.style.display = "none";
175-
break;
176-
}
177-
178-
spanFieldName.innerText = `${field}`;
179-
spanFieldName.append(statusIcon);
180-
spanValue.innerText = `${value?.text || value || "Not detected"}`;
181-
182-
p.appendChild(spanFieldName);
183-
p.appendChild(spanValue);
184-
185-
return p;
186-
}
187-
188181
/** Check if current resolution is Full HD or HD
189182
* @params {Object} currentResolution - an object with `width` and `height` to represent the current resolution of the camera
190183
* @returns {string} Either "HD" or "Full HD" depending of the resolution of the screen

0 commit comments

Comments
 (0)