refactor to adapters
This commit is contained in:
41
testing/fakes/media/DemoImageServiceAdapter.ts
Normal file
41
testing/fakes/media/DemoImageServiceAdapter.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import type { ImageServicePort } from '@gridpilot/media';
|
||||
|
||||
const MALE_DEFAULT_AVATAR = '/images/avatars/male-default-avatar.jpg';
|
||||
const FEMALE_DEFAULT_AVATAR = '/images/avatars/female-default-avatar.jpeg';
|
||||
|
||||
export class DemoImageServiceAdapter implements ImageServicePort {
|
||||
getDriverAvatar(driverId: string): string {
|
||||
const numericSuffixMatch = driverId.match(/(\d+)$/);
|
||||
if (numericSuffixMatch) {
|
||||
const numericSuffixString = numericSuffixMatch[1] ?? '';
|
||||
const numericSuffix = Number.parseInt(numericSuffixString, 10);
|
||||
return numericSuffix % 2 === 0 ? FEMALE_DEFAULT_AVATAR : MALE_DEFAULT_AVATAR;
|
||||
}
|
||||
|
||||
const seed = stableHash(driverId);
|
||||
return seed % 2 === 0 ? FEMALE_DEFAULT_AVATAR : MALE_DEFAULT_AVATAR;
|
||||
}
|
||||
|
||||
getTeamLogo(teamId: string): string {
|
||||
const seed = stableHash(teamId);
|
||||
return `https://picsum.photos/seed/team-${seed}/256/256`;
|
||||
}
|
||||
|
||||
getLeagueCover(leagueId: string): string {
|
||||
const seed = stableHash(leagueId);
|
||||
return `https://picsum.photos/seed/league-cover-${seed}/1200/280?blur=2`;
|
||||
}
|
||||
|
||||
getLeagueLogo(leagueId: string): string {
|
||||
const seed = stableHash(leagueId);
|
||||
return `https://picsum.photos/seed/league-logo-${seed}/160/160`;
|
||||
}
|
||||
}
|
||||
|
||||
function stableHash(value: string): number {
|
||||
let hash = 0;
|
||||
for (let i = 0; i < value.length; i += 1) {
|
||||
hash = (hash * 31 + value.charCodeAt(i)) | 0;
|
||||
}
|
||||
return Math.abs(hash);
|
||||
}
|
||||
Reference in New Issue
Block a user