Total Complexity | 7 |
Complexity/F | 1.4 |
Lines of Code | 63 |
Function Count | 5 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
1 | "use strict"; |
||
2 | |||
3 | var _ = require('lodash'); |
||
4 | |||
5 | // Expose `Position` |
||
6 | |||
7 | module.exports = Position; |
||
8 | |||
9 | var cardinalPoints = ['N','E','S','W']; |
||
10 | |||
11 | /** |
||
12 | * Set up Position with `coordinate` and `cardinal` |
||
13 | |||
14 | * @param {Object} coordinate |
||
15 | * @param {String} cardinal |
||
16 | * @api public |
||
17 | */ |
||
18 | |||
19 | function Position(coordinate, cardinal) { |
||
20 | this.x = coordinate.x; |
||
21 | this.y = coordinate.y; |
||
22 | this.c = cardinal; |
||
23 | |||
24 | this.isValid(); |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * Types validator |
||
29 | * |
||
30 | * @api public |
||
31 | */ |
||
32 | |||
33 | Position.prototype.isValid = function() { |
||
34 | if(!isCoordinates(this.x,this.y)) { |
||
35 | throw new Error('Coordinates are not valid'); |
||
36 | } |
||
37 | if(!isCardinal(this.c)) { |
||
38 | throw new Error('Cardinal point is not valid'); |
||
39 | } |
||
40 | }; |
||
41 | |||
42 | /** |
||
43 | * Coordinates validator helper |
||
44 | * |
||
45 | * @param {Number} x |
||
46 | * @param {Number} y |
||
47 | * @api protected |
||
48 | */ |
||
49 | |||
50 | function isCoordinates(x,y) { |
||
51 | return x === parseInt(x, 10) && y === parseInt(y, 10); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Cardinal validator helper |
||
56 | * |
||
57 | * @param {String} cardinal |
||
58 | * @api protected |
||
59 | */ |
||
60 | |||
61 | function isCardinal(cardinal) { |
||
62 | return _.isString(cardinal) && !_.isUndefined( _.find(cardinalPoints, function(c) { return c === cardinal; }) ); |
||
63 | } |
||
64 |