Last active
September 21, 2016 06:09
-
-
Save CarlosLanderas/d1a90a67bcce626c370ef31f6c383957 to your computer and use it in GitHub Desktop.
Functional typescript
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
export function and<T>(conditions : Array<(T) => boolean>) : (obj:T) => boolean { | |
return (e: T) => conditions.every( c=> c(e)); | |
} |
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
//We want to create an Instance of Video Game Manager and find video games in store base on multiple predicates | |
import {VideoGame} from "./model/video_game"; | |
import {VideoGameStoreService} from "./services/video_game_store_service"; | |
import {VideoGameManager} from "./services/video_game_manager"; | |
import {VideoGameGenre} from "./enum/video_game_genre"; | |
var videoGameManager = new VideoGameManager(new VideoGameStoreService()); | |
var filtered= videoGameManager.find([ s=> s.Genre === VideoGameGenre.Terror, | |
s=>s.Pegi>= 18]); | |
//Output | |
//[ VideoGame { Name: 'Silent Hill', Genre: 2, Pegi: 18 }, | |
// VideoGame { Name: 'Resident Evil HD', Genre: 2, Pegi: 18 } ] |
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
import {VideoGameStoreService} from "../services/video_game_store_service"; | |
import {VideoGame} from "../model/video_game"; | |
import {VideoGameGenre} from "../enum/video_game_genre"; | |
import {and} from "../shared/condition_evaluator"; | |
type Predicate = (v: VideoGame) => boolean; | |
export class VideoGameManager { | |
private videoGameCollection: Array<VideoGame>; | |
constructor(private videoGameStoreService: VideoGameStoreService) { | |
this.videoGameCollection = videoGameStoreService.getStore(); | |
} | |
getStore(): Array<VideoGame> { | |
return this.videoGameCollection; | |
} | |
inGenre(genre: VideoGameGenre) { | |
return this.videoGameCollection.filter(v => v.Genre === genre); | |
} | |
find(conditions: Predicate[]): Array<VideoGame> { | |
return this.videoGameCollection.filter(and(conditions)); | |
} | |
} |
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
import {VideoGame} from "../model/video_game"; | |
import {VideoGameGenre} from "../enum/video_game_genre"; | |
export var videoGameCollection : Array<VideoGame> = [ | |
new VideoGame("Conan", VideoGameGenre.Action, 12), | |
new VideoGame("Call of duty Modern Warfare 2", VideoGameGenre.Shooter, 16), | |
new VideoGame("Silent Hill", VideoGameGenre.Terror,18), | |
new VideoGame("Resident Evil HD", VideoGameGenre.Terror,18) | |
] |
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
import {VideoGame} from "../model/video_game"; | |
import {VideoGameGenre} from "../enum/video_game_genre"; | |
import {videoGameCollection} from "../mock/video_game_store_mock"; | |
export class VideoGameStoreService { | |
getStore(): Array<VideoGame> { | |
return videoGameCollection; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment