cardinal-direction
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

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.

Param Type Required Description
degree number yes A number between 0 and 360.
subset CardinalSubset no Restricts 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.

Param Type Required Description
cardinal CardinalDirection yes One 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.

Param Type Required Description
cardinal string/number yes One 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.

Value Description
Full Enables all possible cardinal directions to be retuned. Default
Basic Restricts the possible returned cardinals to N,E,S,W.
Ordinal Extends Basic to include NE,SE,SW,NW,.
Intercardinal Extends 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

Integer Abbreviation Title
0 N North
1 NbE North by East
2 NNE North Northeast
3 NEbN Northeast by North
4 NE Northeast
5 NEbE Northeast by East
6 ENE East Northeast
7 EbN East by North
8 E East
9 EbS East by South
10 ESE East Southeast
11 SEbE Southeast by East
12 SE Southeast
13 SEbS Southeast by South
14 SSE South Southeast
15 SbE South by East
16 S South
17 SbW South by West
18 SSW South Southwest
19 SWbS Southwest by South
20 SW Southwest
21 SWbW Southwest by West
22 WSW West Southwest
23 WbS West by South
24 W West
25 Wbs West by North
26 WNW West Northwest
27 NWbW Northwest by West
28 NW Northwest
29 NWbN Northwest by North
30 NNW North Northwest
31 NbW North by West

Package Sidebar

Install

npm i cardinal-direction

Weekly Downloads

185

Version

1.1.1

License

MIT

Unpacked Size

20.1 kB

Total Files

5

Last publish

Collaborators

  • mudlabs