{"size": 1550, "ext": "js", "max_stars_repo_path": "main/model/LeagueTable.js", "max_stars_repo_name": "boocockp/leaguetable", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "main/model/LeagueTable.js", "max_issues_repo_name": "boocockp/leaguetable", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "main/model/LeagueTable.js", "max_forks_repo_name": "boocockp/leaguetable", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.0, "max_line_length": 119, "alphanum_fraction": 0.6038709677}
const LeagueTable = memoizeProps(class LeagueTable {
    
    constructor() {
        this.teams = [];
        this.matchResults = [];
        this._listeners = [];
    }

    get leaguePositions() {
        return _.sortBy(this.teams, 'points');
    }

    get teamsByName() {
        return _.sortBy(this.teams, 'name');
    }

    get resultsByTeam() {
        const resultsByHome = _.groupBy(this.matchResults, r => r.home.teamName );
        const resultsByAway = _.groupBy(this.matchResults, r => r.away.teamName );
        function joinArrays(a, b) {
            return a.concat(b);
        }

        return _.mergeWith(resultsByHome, resultsByAway, joinArrays)
    }

    resultsInput(r) {
        const newResults = r.map( it => new MatchResult(it.home.team, it.home.goals, it.away.team, it.away.goals));
        this._addNewTeams(newResults);
        this.matchResults = this.matchResults.concat(newResults);
        PropertyCacheController.version++;
        this._notifyChange(); 
    }

    addChangeListener(listenerFn) {
        this._listeners.push(listenerFn);
        listenerFn()
    }

    _notifyChange() {
        this._listeners.forEach( l => l() );
    }

    _addNewTeams(results) {
        const teamNames = new Set(results.map( it => it.away.teamName ).concat(results.map( it => it.home.teamName )));
        const existingTeamNames = new Set( this.teams.map( it => it.name ));
        existingTeamNames.forEach( it => teamNames.delete(it) );
        teamNames.forEach( n => this.teams.push(new Team(this, n)) );
    }
});