1.1.1 • Published 3 years ago

cardinal-direction v1.1.1

Weekly downloads
35
License
MIT
Repository
github
Last release
3 years ago

npm size support

Cardinal Direction

Each of the 4 main compass quadrants (N,E,S,W), contain 7 sub-regions. This tool can be used to convert a degree ranging from 0-360 to a correlated cardinal direction.


Example of North Quadrant Cardinal Directions

See CardinalDirection for the full list.

  • N – North
  • NbE – North by East
  • NNE – North Northeast
  • NEbN – Northeast by North
  • NE – Northeast
  • NEbE – Northeast by East
  • ENE – East Northeast
  • EbN – East by North

Install

npm install cardinal-direction

Usage

const Compass = require("cardinal-direction");

cardinalFromDegree

Converts a compass degree value into its correlated cardinal value.

ParamTypeRequiredDescription
degreenumberyesA number between 0 and 360.
subsetCardinalSubsetnoRestricts the return value to a subset of the possible cardinal directions. The default is Full. See CardinalSubset for more.
/*
 * Basic usage.
 * - returns "SW"
 */
Compass.cardinalFromDegree(221);

/*
 * Restricting results with subset.
 * - returns "W"
 */
Compass.cardinalFromDegree(221, Compass.CardinalSubset.Basic);

degreeFromCardinal

Converts a cardinal direction into its correlated degree value.

ParamTypeRequiredDescription
cardinalCardinalDirectionyesOne of the possible cardinal directions. See CardinalDirection for more.
/*
 * These all return 90.
 */
Compass.degreeFromCardinal("E");
Compass.degreeFromCardinal(Compass.CardinalDirection["E"]);
Compass.degreeFromCardinal(Compass.CardinalDirection.E);
Compass.degreeFromCardinal(Compass.CardinalDirection[8]);
Compass.degreeFromCardinal(8);

cardinalConverter

Converts a cardinal value to either its abbreviation or full title.

ParamTypeRequiredDescription
cardinalstring/numberyesOne of the CardinalDirection enum member values, or its full title. See Cardinal Table for context.
Compass.cardinalConverter(3): // NEbN
Compass.cardinalConverter("NEbN"): // Northeast by North
Compass.cardinalConverter("Northeast by North"); // NEbN

CardinalSubset enum

This subset enum can be used to fine tune the return value of cardinalFromDegree. When used the specified degree will be pushed to the nearest accepted cardinal direction, based on your restriction.

ValueDescription
FullEnables all possible cardinal directions to be retuned. Default
BasicRestricts the possible returned cardinals to N,E,S,W.
OrdinalExtends Basic to include NE,SE,SW,NW,.
IntercardinalExtends Ordinal to include NNE,ENE,ESE,SSE,SSW,WSW,WNW,NNW.

CardinalDirection enum

There are 32 cardinal directions on a compass. But this numeric enum is 0 indexed.

Compass.CardinalDirection[0]; // "N"
Compass.CardinalDirection["N"]; // 0
Compass.CardinalDirection.N; // 0

Cardinal Table

IntegerAbbreviationTitle
0NNorth
1NbENorth by East
2NNENorth Northeast
3NEbNNortheast by North
4NENortheast
5NEbENortheast by East
6ENEEast Northeast
7EbNEast by North
8EEast
9EbSEast by South
10ESEEast Southeast
11SEbESoutheast by East
12SESoutheast
13SEbSSoutheast by South
14SSESouth Southeast
15SbESouth by East
16SSouth
17SbWSouth by West
18SSWSouth Southwest
19SWbSSouthwest by South
20SWSouthwest
21SWbWSouthwest by West
22WSWWest Southwest
23WbSWest by South
24WWest
25WbsWest by North
26WNWWest Northwest
27NWbWNorthwest by West
28NWNorthwest
29NWbNNorthwest by North
30NNWNorth Northwest
31NbWNorth by West