Skip to content

Commit

Permalink
Merge pull request #16 from connectk12/fix/match-name
Browse files Browse the repository at this point in the history
Fix findMatchingRowByName sanitized text
  • Loading branch information
ngnathan authored Sep 23, 2024
2 parents e7fec71 + 72a978b commit de6446d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-bulldogs-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@connectk12/exceljs": patch
---

Fix findMatchingRowByName sanitized text
49 changes: 31 additions & 18 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export const findMatchingRowById = ({
* });
*
*/

export const findMatchingRowByName = ({
worksheet,
startRowNumber,
Expand Down Expand Up @@ -213,8 +214,8 @@ export const findMatchingRowByName = ({
}): ExcelJS.Row | undefined => {
const _findSimilarMatch = opts?.findSimilarMatch ?? true;
const sanitizedNameValues = nameValues.map((nameValue) => ({
lastName: sanitizeText(nameValue.lastName),
firstName: sanitizeText(nameValue.firstName),
lastName: sanitizeText(nameValue.lastName, { uppercase: true }),
firstName: sanitizeText(nameValue.firstName, { uppercase: true }),
}));
const rows = worksheet.getRows(startRowNumber, lastRowNumber);
if (!rows) {
Expand All @@ -230,29 +231,32 @@ export const findMatchingRowByName = ({

if ("lastName" in lookupCols && "firstName" in lookupCols) {
worksheetLastName = sanitizeText(
row.getCell(lookupCols.lastName).value?.toString()
row.getCell(lookupCols.lastName).value?.toString(),
{ uppercase: true }
);
worksheetFirstName = sanitizeText(
row.getCell(lookupCols.firstName).value?.toString()
row.getCell(lookupCols.firstName).value?.toString(),
{ uppercase: true }
);
} else if ("name" in lookupCols) {
const currentRowName = sanitizeText(
row.getCell(lookupCols.name).value?.toString()
row.getCell(lookupCols.name).value?.toString(),
{ uppercase: true }
);
if (currentRowName) {
const nameArray = currentRowName.split(lookupCols.delimiter ?? " ");
if (
nameArray.length > 1 &&
lookupCols.order === "FIRST_NAME LAST_NAME"
) {
worksheetFirstName = nameArray[0]?.trim();
worksheetLastName = nameArray[1]?.trim();
worksheetFirstName = sanitizeText(nameArray[0], { uppercase: true });
worksheetLastName = sanitizeText(nameArray[1], { uppercase: true });
} else if (
nameArray.length > 1 &&
lookupCols.order === "LAST_NAME FIRST_NAME"
) {
worksheetFirstName = nameArray[1]?.trim();
worksheetLastName = nameArray[0]?.trim();
worksheetFirstName = sanitizeText(nameArray[1], { uppercase: true });
worksheetLastName = sanitizeText(nameArray[0], { uppercase: true });
}
}
}
Expand All @@ -261,11 +265,12 @@ export const findMatchingRowByName = ({
return false;
}

return sanitizedNameValues.some(
const match = sanitizedNameValues.some(
(nameValue) =>
worksheetLastName === nameValue.lastName &&
worksheetFirstName === nameValue.firstName
);
return match;
});

// Find similar match if exact match not found
Expand All @@ -275,37 +280,44 @@ export const findMatchingRowByName = ({
let worksheetFirstName = undefined;
if ("lastName" in lookupCols && "firstName" in lookupCols) {
worksheetLastName = sanitizeText(
row.getCell(lookupCols.lastName).value?.toString()
row.getCell(lookupCols.lastName).value?.toString(),
{ uppercase: true }
);
worksheetFirstName = sanitizeText(
row.getCell(lookupCols.firstName).value?.toString()
row.getCell(lookupCols.firstName).value?.toString(),
{ uppercase: true }
);
} else if ("name" in lookupCols) {
const currentRowName = sanitizeText(
row.getCell(lookupCols.name).value?.toString()
row.getCell(lookupCols.name).value?.toString(),
{ uppercase: true }
);
if (currentRowName) {
const nameArray = currentRowName.split(lookupCols.delimiter ?? " ");
if (
nameArray.length > 1 &&
lookupCols.order === "FIRST_NAME LAST_NAME"
) {
worksheetFirstName = nameArray[0]?.trim();
worksheetLastName = nameArray[1]?.trim();
worksheetFirstName = sanitizeText(nameArray[0], {
uppercase: true,
});
worksheetLastName = sanitizeText(nameArray[1], { uppercase: true });
} else if (
nameArray.length > 1 &&
lookupCols.order === "LAST_NAME FIRST_NAME"
) {
worksheetFirstName = nameArray[1]?.trim();
worksheetLastName = nameArray[0]?.trim();
worksheetFirstName = sanitizeText(nameArray[1], {
uppercase: true,
});
worksheetLastName = sanitizeText(nameArray[0], { uppercase: true });
}
}
}
if (!worksheetLastName || !worksheetFirstName) {
return false;
}

return sanitizedNameValues.some(
const match = sanitizedNameValues.some(
(nameValue) =>
nameValue.lastName &&
nameValue.lastName !== "" &&
Expand All @@ -316,6 +328,7 @@ export const findMatchingRowByName = ({
(nameValue.lastName?.includes(worksheetLastName) &&
nameValue.firstName?.includes(worksheetFirstName)))
);
return match;
});
}

Expand Down

0 comments on commit de6446d

Please sign in to comment.