Created
April 12, 2024 09:59
-
-
Save Arkmind/060cd0cade274d803388b6a1096d1721 to your computer and use it in GitHub Desktop.
Recherche d'équipe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const teams = []; | |
/** | |
* Retrieves all teams in the specified divisions | |
* @param divisions ("OPEN" | "INTERMEDIATE" | "MAIN" | "ADVANCED")[] | |
* @returns Team[] | |
*/ | |
const fetchTeamDivisions = async (divisions) => { | |
// Transform every divisions in fetch and wait for all of them to finish | |
const response = await Promise.all( | |
// Map every division to a fetch request | |
divisions.map(async (division) => { | |
// Fetch the teams in the division | |
const request = await fetch( | |
`https://api.kocs.fr/${division}/teams` | |
); | |
// Return the json response | |
return request.json(); | |
}) | |
).catch((error) => { | |
// TODO > Handle error; | |
console.error(error); | |
}); | |
// Flatten the response to have a single array of teams | |
return response.flat(); | |
}; | |
/** | |
* Search for a team or a player in the teams list | |
* @param query string | |
* @returns Team[] | |
*/ | |
const search = async (query) => { | |
// If the teams list is empty, fetch the teams in the divisions | |
if (teams.length <= 0) { | |
// Fetch all divisions | |
const responseTeams = await fetchTeamDivisions([ | |
"OPEN", | |
"INTERMEDIATE", | |
"MAIN", | |
"ADVANCED", | |
]); | |
teams.push(...responseTeams); | |
} | |
// Return the teams that match the query | |
return teams.filter( | |
(team) => | |
// Check if the team name match the query | |
team.name?.toLowerCase().includes(query.toLowerCase()) || | |
// Check if the team roster contains a player with the nickname matching the query | |
team.roster?.some((player) => | |
player?.nickname.toLowerCase().includes(query.toLowerCase()) | |
) | |
); | |
}; | |
// Example usage | |
(async () => { | |
const response = await search("k1z"); | |
console.log(response); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment