Code coverage report for lib\PinPadModel.js

Statements: 96.3% (52 / 54)      Branches: 77.78% (14 / 18)      Functions: 100% (10 / 10)      Lines: 96.3% (52 / 54)      Ignored: none     

All files » lib/ » PinPadModel.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86          1 1   1 2     1 1 1 1     1 5 5 5 5 4 2 2   4 8     5 1   4 2   2 2   4 4       1 1 1 1 1 2 2   1 1   1   1 1 1   1 1     1           1 1 1 1 1 1   1 1   1     1
/*jslint node: true */
/*jshint laxbreak: true */
/*jshint laxcomma: true*/
'use strict';
 
var _ = require('underscore');
var CategoryModel = require('./CategoryModel');
 
var addCategory = function(pinPadModel, catTitle, ordering) {
    return new CategoryModel(catTitle, ordering);
};
 
var PinPadModel = function(ordering) {
    var pinPadModel = this;
    pinPadModel.ordering = ordering;
    pinPadModel.categories = [];
};
 
PinPadModel.prototype.addElement = function(element) {
    var pinPadModel = this;
    var category;
    var catIndex = -1;
    var elem = _.find(pinPadModel.categories, function(cat, counter) {
        if (cat.title === element.category) {
            category = cat;
            catIndex = counter;
        }
        return _.find(cat.elements, function(el) {
            return element.id === el.id;
        });
    });
    if (elem) {
        return {error: 'duplication'};
    } else {
        if (!category || (catIndex === -1)) {
            category = addCategory
                (pinPadModel, element.category, pinPadModel.ordering);
            pinPadModel.categories.push(category);
            catIndex = pinPadModel.categories.length - 1;
        }
        var elIndex = category.addElement(element);
        return {catIndex: catIndex, elIndex: elIndex};
    }
};
 
PinPadModel.prototype.removeElement = function(elId) {
    var pinPadModel = this;
    var catIndex = -1, elIndex = -1;
    _.find(pinPadModel.categories, function(cat, counter) {
        var element = _.find(cat.elements, function(el, index) {
            elIndex = index;
            return el.id === elId;
        });
        Eif (element) {
            catIndex = counter;
        }
        return catIndex !== -1;
    });
    Eif ((catIndex !== -1) && (elIndex !== -1)) {
        var removedFromCategory = pinPadModel.categories[catIndex].title;
        var removedElement = pinPadModel.categories[catIndex]
            .elements[elIndex];
        pinPadModel.categories[catIndex].removeElement(elIndex);
        Iif (pinPadModel.categories[catIndex].elements.length === 0) {
            pinPadModel.categories.splice(catIndex, 1);
        }
        return {catIndex: catIndex, elIndex: elIndex
            , category: removedFromCategory, element: removedElement};
    }
    return {error: 'unknown'};
};
 
PinPadModel.prototype.removeCategory = function(catTitle) {
    var pinPadModel = this;
    var catIndex = -1;
    var category = _.find(pinPadModel.categories, function(cat, index) {
        catIndex = index;
        return cat.title === catTitle;
    });
    Eif (category) {
        pinPadModel.categories.splice(catIndex, 1);
    }
    return {catIndex: catIndex};
};
 
module.exports = PinPadModel;