Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
node_modules
node_modules/
dist/
.env
.env.funder
*.pem
__pycache__/
.vitest
coverage/
*.log
.DS_Store
90 changes: 90 additions & 0 deletions src/services/hero-data.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Hero Data Service
// Manages hero information retrieval with flexible, extensible design

export interface Hero {
id: number;
name: string;
realName: string;
description: string;
powers: string[];
}

export class HeroDataService {
private heroes: Hero[] = [
{
id: 1,
name: 'spiderman',
realName: 'Peter Parker',
description: 'Friendly neighborhood Spider-Man',
powers: ['web-slinging', 'spider-sense', 'wall-crawling']
},
{
id: 2,
name: 'ironman',
realName: 'Tony Stark',
description: 'Genius, billionaire, philanthropist',
powers: ['powered armor', 'advanced technology', 'flight']
},
{
id: 3,
name: 'captainamerica',
realName: 'Steve Rogers',
description: 'Super soldier and Avengers leader',
powers: ['enhanced strength', 'tactical genius', 'shield mastery']
}
];

/**
* Get hero by name (case-insensitive)
* @param name Hero name to search
* @returns Hero object or undefined
*/
getHeroByName(name: string): Hero | undefined {
if (!name) {
throw new Error('Hero name is required');
}

const normalizedName = name.toLowerCase().replace(/\s/g, '');
return this.heroes.find(
hero => hero.name.toLowerCase() === normalizedName
);
}

/**
* Get all heroes
* @returns Array of heroes
*/
getAllHeroes(): Hero[] {
return [...this.heroes];
}

/**
* Add a new hero to the collection
* @param hero Hero object to add
* @returns Added hero
* @throws Error if hero already exists or is invalid
*/
addHero(hero: Hero): Hero {
// Validate hero
if (!hero.name || !hero.realName) {
throw new Error('Hero name and real name are required');
}

// Check for existing hero
const existingHero = this.getHeroByName(hero.name);
if (existingHero) {
throw new Error(`Hero with name ${hero.name} already exists`);
}

// Assign new ID if not provided
if (!hero.id) {
hero.id = this.heroes.length + 1;
}

this.heroes.push(hero);
return hero;
}
}

// Export singleton instance
export const heroDataService = new HeroDataService();