relative-placement.js 1.74 KB
/**
 * Created by Techniv on 30/11/2016.
 */
(function (define) {
    // TODO define exhaustive module platform
    module.exports = define();
})(function(){

    /**
     *
     * @constructor
     * @property {Candidate[]} candidateList
     * @property {Number} numberOfCandidates
     *
     */
    function RelativePlacement() {
        Object.defineProperties(this, {
            candidateList:      {value: {}},
            numberOfCandidates: {get: () => Object.keys(this.candidateList).length}
        });
    }

    /**
     * @name addCandidate
     * @param {String} candidateName
     * @methodOf RelativePlacement
     * @this RelativePlacement
     */
    RelativePlacement.prototype.addCandidate = function addCandidate(candidateName){
        if(this.candidateList[candidateName]) throw new Error('"' +candidateName+ '" already exist in candidate list.');
        this.candidateList[candidateName] = new Candidate(candidateName);
    };

    /**
     * @name addCandidates
     * @param {String[]} candidateNames
     * @methodOf RelativePlacement
     * @this RelativePlacement
     */
    RelativePlacement.prototype.addCandidates = function (candidateNames) {
        for(let i = 0; i < candidateNames.length; i++){
            this.addCandidate(candidateNames[i]);
        }
    };

    RelativePlacement.prototype.addVote = function () {};
    RelativePlacement.prototype.addVotes = function () {};
    RelativePlacement.prototype.getResult = function () {};

    function Candidate(name) {
        Object.defineProperties(this, {
            name:       {value: name,   enumerable: true},
            votes:      {value:[],      enumerable: true},
            placement:  {value:[],      enumerable: false}
        });
    }

    return RelativePlacement;
});