Skip to content

Commit

Permalink
Added basic suffix support
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilkinson22 committed Jul 17, 2023
1 parent 9e86679 commit 2570f69
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/constants/nameSuffixes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default ["jr", "jnr", "sr", "snr"];
52 changes: 42 additions & 10 deletions src/controllers/rugby/peopleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const SlugRedirect = mongoose.model("slugRedirect");

//Constants
const { fetchPeopleLimit, localTeam } = require("~/config/keys");
import nameSuffixes from "~/constants/nameSuffixes";

//Images
import PersonImageCard from "~/images/PersonImageCard";
Expand Down Expand Up @@ -305,7 +306,26 @@ export async function parsePlayerList(req, res) {
const { names, gender } = req.body;

//Regex to normalise names
const regEx = new RegExp("[^a-zA-Z]", "gi");
const suffixRegex = new RegExp("\\s(" + nameSuffixes.join("|") + ").?$", "i");
const extractSuffix = name => {
const match = name.match(suffixRegex);
return match
? match[0]
.replace(".", "")
.trim()
.toLowerCase()
// this allows us to match jnr to jr, etc
.replace("n", "")
: null;
};
const normaliseName = name =>
name
.toLowerCase()
//Remove special characters
.replace(/[^a-z ]/gi, "")
//Replace Suffix
.replace(suffixRegex, "")
.trim();

//Get full people list
let people = await Person.find({ gender }, "name isPlayer isCoach isReferee").lean();
Expand All @@ -314,26 +334,38 @@ export async function parsePlayerList(req, res) {
return {
...p,
name,
filteredName: name.replace(regEx, "").toLowerCase()
filteredName: normaliseName(name)
};
});

//Get Matches
const matches = [];
for (const unfilteredName of names) {
const name = unfilteredName.replace(regEx, "").toLowerCase();
const name = normaliseName(unfilteredName);

const exact = people.filter(p => p.filteredName === name);
let exact = people.filter(p => p.filteredName === name);
if (exact.length) {
matches.push({ exact: exact.length == 1 && exact[0].isPlayer, results: exact });
let isExact = exact.length == 1;

// Check for an exact match with a suffix
const submittedSuffix = extractSuffix(unfilteredName);
if (submittedSuffix) {
const matchesWithSameSuffix = exact.filter(person => {
const en = extractSuffix(person.name);
return en == submittedSuffix;
});

isExact = matchesWithSameSuffix.length == 1;
if (isExact) {
exact = matchesWithSameSuffix;
}
}

matches.push({ exact: isExact && exact[0].isPlayer, results: exact });
} else {
//Create a Regex that matches first initial and last name
const firstInitial = name.substr(0, 1);
const lastName = unfilteredName
.split(" ")
.pop()
.replace(regEx, "")
.toLowerCase();
const lastName = normaliseName(unfilteredName.split(" ").pop());
const approxRegex = new RegExp(`^${firstInitial}.+ ${lastName}$`, "ig");

//Find anyone who matches the regex
Expand Down

0 comments on commit 2570f69

Please sign in to comment.