Comparing version 1.2.0 to 1.3.0
{ | ||
"name": "windrose", | ||
"main": "windrose.js", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"homepage": "https://github.com/rogeriopvl/windrose", | ||
@@ -6,0 +6,0 @@ "authors": [ |
{ | ||
"name": "windrose", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "Convert compass degrees into points of the compass and vice versa", | ||
@@ -5,0 +5,0 @@ "main": "windrose.js", |
@@ -41,8 +41,20 @@ # Windrose [![Build Status](https://secure.travis-ci.org/rogeriopvl/windrose.png?branch=master)](http://travis-ci.org/rogeriopvl/windrose) | ||
### Windrose.getPoint(degrees) | ||
### Windrose.getPoint(degrees, opts) | ||
* `degrees` (number) the degrees to convert to point (only valid if >= 0 or <= 360) | ||
* `opts` (object) options hash [optional] | ||
* `depth` (integer) the depth of search it can be a value between 0 and 3. | ||
### Windrose.getDegrees(name) | ||
With value `0`, it only returns the 4 main compass points (N, E, S, W). | ||
With value `1` returns the main 8 compass points (N, NE, E, SE, S, SW, W, NW). | ||
With value `2` returns the main 16 compass points (N, NNE, NE, ENE, E, ESE, SE, SSE, S, SSW, SW, WSW, W, WNW, NW, NNW). | ||
With value `3` returns the 32 points of the compass. | ||
Any other value will return `undefined`. | ||
### Windrose.getDegrees(name, opts) | ||
* `name` (string) the name or symbol of the compass point to convert to degrees | ||
@@ -49,0 +61,0 @@ |
140
test/main.js
@@ -16,39 +16,121 @@ var expect = require('chai').expect; | ||
it('should return north when passing 0 degrees', function (done) { | ||
var res = Windrose.getPoint(0); | ||
expect(res).to.be.an('object'); | ||
expect(res.symbol).to.equal('N'); | ||
done(); | ||
}); | ||
describe('getPoint', function () { | ||
it('should return north when passing 0 degrees', function (done) { | ||
var res = Windrose.getPoint(0); | ||
expect(res).to.be.an('object'); | ||
expect(res.symbol).to.equal('N'); | ||
done(); | ||
}); | ||
it('should return North when passing 360 degrees', function (done) { | ||
var res = Windrose.getPoint(359.8); | ||
expect(res).to.be.an('object'); | ||
expect(res.symbol).to.equal('N'); | ||
done(); | ||
}); | ||
it('should return North when passing 360 degrees', function (done) { | ||
var res = Windrose.getPoint(359.8); | ||
expect(res).to.be.an('object'); | ||
expect(res.symbol).to.equal('N'); | ||
done(); | ||
}); | ||
it('should return undefined when passing -1 degrees', function (done) { | ||
var res = Windrose.getPoint(-1); | ||
expect(res).to.be.an('undefined'); | ||
done(); | ||
}); | ||
it('should return undefined when passing -1 degrees', function (done) { | ||
var res = Windrose.getPoint(-1); | ||
expect(res).to.be.an('undefined'); | ||
done(); | ||
}); | ||
it('should return undefined when passing 361 degrees', function (done) { | ||
var res = Windrose.getPoint(361); | ||
expect(res).to.be.an('undefined'); | ||
done(); | ||
it('should return undefined when passing 361 degrees', function (done) { | ||
var res = Windrose.getPoint(361); | ||
expect(res).to.be.an('undefined'); | ||
done(); | ||
}); | ||
it('should return N when passing 15 degrees with 0 depth', function (done) { | ||
var res = Windrose.getPoint(15, { depth: 0 }); | ||
expect(res).to.be.an('object'); | ||
expect(res.symbol).to.equal('N'); | ||
done(); | ||
}); | ||
it('should return S when passing 200 degrees with 0 depth', function (done) { | ||
var res = Windrose.getPoint(200, { depth: 0 }); | ||
expect(res).to.be.an('object'); | ||
expect(res.symbol).to.equal('S'); | ||
done(); | ||
}); | ||
it('should return SW when passing 220 degrees with 1 depth', function (done) { | ||
var res = Windrose.getPoint(220, { depth: 1 }); | ||
expect(res).to.be.an('object'); | ||
expect(res.symbol).to.equal('SW'); | ||
done(); | ||
}); | ||
it('should return WNW when passing 293 degrees with 2 depth', function (done) { | ||
var res = Windrose.getPoint(293, { depth: 2 }); | ||
expect(res).to.be.an('object'); | ||
expect(res.symbol).to.equal('WNW'); | ||
done(); | ||
}); | ||
it('should return undefined when passing invalid depth', function (done) { | ||
var res = Windrose.getPoint(15, { depth: 0 }); | ||
expect(res).to.be.an('object'); | ||
expect(res.symbol).to.equal('N'); | ||
done(); | ||
}); | ||
}); | ||
it('should return 0 when passing North or N', function (done) { | ||
var res = Windrose.getDegrees('North'); | ||
expect(res).to.be.a('number'); | ||
expect(res).to.equal(0); | ||
describe('getDegrees', function () { | ||
it('should return 0 when passing North or N', function (done) { | ||
var res = Windrose.getDegrees('North'); | ||
expect(res).to.be.a('number'); | ||
expect(res).to.equal(0); | ||
var res2 = Windrose.getDegrees('N'); | ||
expect(res2).to.be.a('number'); | ||
expect(res2).to.equal(0); | ||
var res2 = Windrose.getDegrees('N'); | ||
expect(res2).to.be.a('number'); | ||
expect(res2).to.equal(0); | ||
done(); | ||
done(); | ||
}); | ||
it('should return undefined when passing unknown point name', function (done) { | ||
var res = Windrose.getDegrees('LOL'); | ||
expect(res).to.be.an('undefined'); | ||
done(); | ||
}); | ||
xit('should return 270 when passing W point with 0 depth', function (done) { | ||
var res = Windrose.getDegrees('W', { depth: 0 }); | ||
expect(res).to.be.a('number'); | ||
expect(res).to.equal(270); | ||
done(); | ||
}); | ||
xit('should return 225 when passing SW point with 1 depth', function (done) { | ||
var res = Windrose.getDegrees('SW', { depth: 1 }); | ||
expect(res).to.be.a('number'); | ||
expect(res).to.equal(225); | ||
done(); | ||
}); | ||
xit('should return 292.5 when passing WNW point with 2 depth', function (done) { | ||
var res = Windrose.getDegrees('WNW', { depth: 2 }); | ||
expect(res).to.be.a('number'); | ||
expect(res).to.equal(292.5); | ||
done(); | ||
}); | ||
xit('should return undefined when passing invalid depth', function (done) { | ||
var res = Windrose.getDegrees('WNW', { depth: 9 }); | ||
expect(res).to.be.an('undefined'); | ||
done(); | ||
}); | ||
}); | ||
}); |
@@ -5,6 +5,19 @@ /** | ||
* This is a simple module that converts compass degress into compass points | ||
* and points to degrees. There are 32 points in the compass, and each one | ||
* has a length of 11.25 degrees. This is the value that is used to | ||
* calculate the points. | ||
* and points to degrees. | ||
* | ||
* You can pass an { depth: ... } hash to the getPoint. | ||
* | ||
* Passing a depth: 0 will limit the search to the 4 | ||
* main compass points: N, E, S, W. | ||
* | ||
* Passing a depth: 1 will limit the search to the 8 | ||
* main compass points: N, NE, E, SE, S, SW, W, NW | ||
* | ||
* Passing a depth: 2 will limit the search to the 16 | ||
* main compass points: N, NNE, NE, ENE, E, ESE, SE, SSE, | ||
* S, SSW, SW, WSW, W, WNW, NW, NNW. | ||
* | ||
* Passing a depth: 3 (default) will do the search for the | ||
* 32 points of the compass. | ||
* | ||
* @author rogeriopvl <http://github.com/rogeriopvl> | ||
@@ -23,35 +36,36 @@ * @license MIT | ||
} (this, function () { | ||
var DEPTHS_AREA = [ 90, 45, 22.5, 11.25 ]; | ||
var COMPASS_POINTS = [ | ||
{ symbol: 'N', name: 'North' }, | ||
{ symbol: 'NbE', name: 'North by East' }, | ||
{ symbol: 'NNE', name: 'North North East' }, | ||
{ symbol: 'NEbN', name: 'North East by North' }, | ||
{ symbol: 'NE', name: 'North East' }, | ||
{ symbol: 'NEbE', name: 'North East by East' }, | ||
{ symbol: 'ENE', name: 'East North East' }, | ||
{ symbol: 'EbN', name: 'East by North' }, | ||
{ symbol: 'E', name: 'East' }, | ||
{ symbol: 'EbS', name: 'East by South' }, | ||
{ symbol: 'ESE', name: 'East South East' }, | ||
{ symbol: 'SEbE', name: 'South East by East' }, | ||
{ symbol: 'SE', name: 'South East' }, | ||
{ symbol: 'SEbS', name: 'South East by South' }, | ||
{ symbol: 'SSE', name: 'South South East' }, | ||
{ symbol: 'SbE', name: 'South by East' }, | ||
{ symbol: 'S', name: 'South' }, | ||
{ symbol: 'SbW', name: 'South by West' }, | ||
{ symbol: 'SSW', name: 'South South West' }, | ||
{ symbol: 'SWbS', name: 'South West by South' }, | ||
{ symbol: 'SW', name: 'South West' }, | ||
{ symbol: 'SWbW', name: 'South West by West' }, | ||
{ symbol: 'WSW', name: 'West South West' }, | ||
{ symbol: 'WbS', name: 'West by South' }, | ||
{ symbol: 'W', name: 'West' }, | ||
{ symbol: 'WbN', name: 'West by North' }, | ||
{ symbol: 'WNW', name: 'West North West' }, | ||
{ symbol: 'NWbW', name: 'North West by West' }, | ||
{ symbol: 'NW', name: 'North West' }, | ||
{ symbol: 'NWbN', name: 'North West by North' }, | ||
{ symbol: 'NNW', name: 'North North West' }, | ||
{ symbol: 'NbW', name: 'North by West' } | ||
{ symbol: 'N', name: 'North', depth: 0 }, | ||
{ symbol: 'NbE', name: 'North by East', depth: 3 }, | ||
{ symbol: 'NNE', name: 'North North East', depth: 2 }, | ||
{ symbol: 'NEbN', name: 'North East by North', depth: 3 }, | ||
{ symbol: 'NE', name: 'North East', depth: 1 }, | ||
{ symbol: 'NEbE', name: 'North East by East', depth: 3 }, | ||
{ symbol: 'ENE', name: 'East North East', depth: 2 }, | ||
{ symbol: 'EbN', name: 'East by North', depth: 3 }, | ||
{ symbol: 'E', name: 'East', depth: 0 }, | ||
{ symbol: 'EbS', name: 'East by South', depth: 3 }, | ||
{ symbol: 'ESE', name: 'East South East', depth: 2 }, | ||
{ symbol: 'SEbE', name: 'South East by East', depth: 3 }, | ||
{ symbol: 'SE', name: 'South East', depth: 1 }, | ||
{ symbol: 'SEbS', name: 'South East by South', depth: 3 }, | ||
{ symbol: 'SSE', name: 'South South East', depth: 2 }, | ||
{ symbol: 'SbE', name: 'South by East', depth: 3 }, | ||
{ symbol: 'S', name: 'South', depth: 0 }, | ||
{ symbol: 'SbW', name: 'South by West', depth: 3 }, | ||
{ symbol: 'SSW', name: 'South South West', depth: 2 }, | ||
{ symbol: 'SWbS', name: 'South West by South', depth: 3 }, | ||
{ symbol: 'SW', name: 'South West', depth: 1 }, | ||
{ symbol: 'SWbW', name: 'South West by West', depth: 3 }, | ||
{ symbol: 'WSW', name: 'West South West', depth: 2 }, | ||
{ symbol: 'WbS', name: 'West by South', depth: 3 }, | ||
{ symbol: 'W', name: 'West', depth: 0 }, | ||
{ symbol: 'WbN', name: 'West by North', depth: 3 }, | ||
{ symbol: 'WNW', name: 'West North West', depth: 2 }, | ||
{ symbol: 'NWbW', name: 'North West by West', depth: 3 }, | ||
{ symbol: 'NW', name: 'North West', depth: 1 }, | ||
{ symbol: 'NWbN', name: 'North West by North', depth: 3 }, | ||
{ symbol: 'NNW', name: 'North North West', depth: 2 }, | ||
{ symbol: 'NbW', name: 'North by West', depth: 3 } | ||
]; | ||
@@ -65,10 +79,15 @@ | ||
* @param {number} degrees - the degrees in the compass to convert | ||
* @param {object} opts - (optional) hash containing options | ||
* opts.depth - valid from 0 to 3 | ||
* @return {object} the compass point of the given degrees. If degrees are | ||
* invalid (< 0 || > 360), then undefined is returned. | ||
*/ | ||
getPoint: function (degrees) { | ||
getPoint: function (degrees, opts) { | ||
if (degrees < 0 || degrees > 360) { return; } | ||
var idx = Math.round(degrees / 11.25); | ||
opts = opts || {}; | ||
opts.depth = opts.hasOwnProperty('depth') ? opts.depth : 3; | ||
var idx = Math.round(degrees / DEPTHS_AREA[opts.depth]); | ||
// 360 === 0 aka North | ||
@@ -78,3 +97,5 @@ if (idx === COMPASS_POINTS.length) { | ||
} | ||
return COMPASS_POINTS[idx]; | ||
return COMPASS_POINTS.filter(function (pt) { | ||
return pt.depth <= opts.depth; | ||
})[idx]; | ||
}, | ||
@@ -91,3 +112,3 @@ | ||
if (name === item.name || name === item.symbol) { | ||
found = idx * 11.25; | ||
found = idx * DEPTHS_AREA[3]; | ||
return; | ||
@@ -94,0 +115,0 @@ } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14200
248
63
0