GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

lib/mower.js   A
last analyzed

Complexity

Total Complexity 15
Complexity/F 1.67

Size

Lines of Code 126
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 0
c 2
b 0
f 1
nc 2
dl 0
loc 126
rs 10
wmc 15
mnd 1
bc 15
fnc 9
bpm 1.6666
cpm 1.6666
noi 1

7 Functions

Rating   Name   Duplication   Size   Complexity  
A mower.js ➔ Mower 0 9 1
A Mower.start 0 6 1
B Mower.isValid 0 14 5
A mower.js ➔ isGrid 0 3 1
A mower.js ➔ isPosition 0 3 1
A mower.js ➔ isInstructions 0 3 1
A Mower.runStep 0 12 1
1
'use strict';
2
3
var _ = require('lodash'),
4
    Promise = require('bluebird'),
0 ignored issues
show
Comprehensibility introduced by
You are shadowing the built-in type Promise. This makes code hard to read, consider using a different name.
Loading history...
5
    Move = require('./move'),
6
    Position = require('./position'),
7
    Grid = require('./grid');
8
9
// Expose `Mower`
10
11
module.exports = Mower;
12
13
/**
14
 * Set up Mower with `options`
15
 *
16
 * Parameters:
17
 *
18
 *   - `id` a valid number
19
 *   - `grid` a valid Grid object
20
 *   - `position` a valid Position object
21
 *   - `instructions` a valid Array that contains only the following instructions : 'A', 'G' and 'D'.
22
 *
23
 * @param {Number} id
24
 * @param {Object} grid
25
 * @param {Object} position
26
 * @param {Array} instructions
27
 * @api public
28
 */
29
30
function Mower(id, grid, position, instructions) {
31
    this.id = id;
32
    this.grid = grid;
33
    this.position = position;
34
    this.instructions = instructions;
35
    this.move = new Move(this, grid);
36
37
    this.isValid();
38
}
39
40
/**
41
 * Start mower's instructions
42
 *
43
 * @api public
44
 */
45
46
Mower.prototype.start = function() {
47
    var self = this;
48
    return new Promise(function(resolve) {
49
        self.runStep(0, resolve); // run first step
50
    });
51
};
52
53
/**
54
 * Types validator
55
 *
56
 * @api public
57
 */
58
59
Mower.prototype.isValid = function() {
60
    if(!_.isInteger(this.id)) {
61
        throw new Error('Id is not valid');
62
    }
63
    if(!isGrid(this.grid)) {
64
        throw new Error('Grid is not valid');
65
    }
66
    if(!isPosition(this.position)) {
67
        throw new Error('Position is not valid');
68
    }
69
    if(!isInstructions(this.instructions)) {
70
        throw new Error('Instructions are not valid');
71
    }
72
};
73
74
/**
75
 * Run one step then pass to next instruction or resolve promise when instructions are finished
76
 *
77
 * @param {Number} step
78
 * @param {Function} end
79
 * @api public
80
 */
81
82
Mower.prototype.runStep = function(step, end) {
83
    var self = this;
84
    this.move.runInstruction(this.instructions[step]).then(function() {
85
        var nexStep = step+1;
86
        if(nexStep >= 0 && nexStep <= self.instructions.length-1){
87
            self.runStep(nexStep, end);
88
        }
89
        else {
90
            end(self);
91
        }
92
    })
93
};
94
95
/**
96
 * Grid validator helper
97
 *
98
 * @param {Object} grid
99
 * @api protected
100
 */
101
102
function isGrid(grid) {
103
    return grid instanceof Grid;
104
}
105
106
/**
107
 * Position validator helper
108
 *
109
 * @param {Object} position
110
 * @api protected
111
 */
112
113
function isPosition(position) {
114
    return position instanceof Position;
115
}
116
117
/**
118
 * Instructions validator helper
119
 *
120
 * @param {Object} instructions
121
 * @api protected
122
 */
123
124
function isInstructions(instructions) {
125
    return _.isArray(instructions);
126
}