-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
131 lines (112 loc) · 4.04 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Regular expression which takes an Active Worlds coordinates formatted string and matches the X, Y, Z, and Yaw parts.
*/
let coordinateRegex = /^(\w+){1}\s+(\d*\.?\d+[NS]){1}\s+(\d*\.?\d+[EW]){1}(\s+\d*\.?\d+[A])?(\s+\d+)?/i;
/**
* Enumeration of Regex group indices.
*/
const groups = {
CAPTURE: 0,
WORLD: 1,
NS: 2,
EW: 3,
ALT: 4,
YAW: 5
};
/**
* Tests that given only N and W values (no decimals), the X and Z are matched accordingly.
*/
it('coordinateRegex_northAndWestNoDecimals_parsesSuccessfully', () => {
let coordinates = parseCoordinates('AW 120N 130W');
expect(coordinates.world.toLowerCase()).toBe('aw');
expect(coordinates.x).toBe(130);
expect(coordinates.y).toBe(0);
expect(coordinates.z).toBe(120);
expect(coordinates.yaw).toBe(0);
});
/**
* Tests that given only S and E values (no decimals), the X and Z are matched accordingly.
*/
it('coordinateRegex_southAndEastNoDecimals_parsesSuccessfully', () => {
let coordinates = parseCoordinates('AWTeen 120S 130E');
expect(coordinates.world.toLowerCase()).toBe('awteen');
expect(coordinates.x).toBe(-130);
expect(coordinates.y).toBe(0);
expect(coordinates.z).toBe(-120);
expect(coordinates.yaw).toBe(0);
});
/**
* Tests that given only N and W values (with decimals), the X and Z are matched accordingly.
*/
it('coordinateRegex_northAndWestWithDecimals_parsesSuccessfully', () => {
let coordinates = parseCoordinates('Yellow 120.58N 4.62W');
expect(coordinates.world.toLowerCase()).toBe('yellow');
expect(coordinates.x).toBe(4.62);
expect(coordinates.y).toBe(0);
expect(coordinates.z).toBe(120.58);
expect(coordinates.yaw).toBe(0);
});
/**
* Tests that given all values (with no decimals), the X, Y, Z, and Yaw are matched accordingly.
*/
it('coordinateRegex_allValuesNoDecimals_parsesSuccessfully', () => {
let coordinates = parseCoordinates('Droog 120N 4W 0A 3599');
expect(coordinates.world.toLowerCase()).toBe('droog');
expect(coordinates.x).toBe(4);
expect(coordinates.y).toBe(0);
expect(coordinates.z).toBe(120);
expect(coordinates.yaw).toBe(3599);
});
/**
* Tests that given all value (each with decimals) the X, Y, Z, and Yaw are matched accordingly.
*/
it('coordinateRegex_allValuesWithDecimals_parsesSuccessfully', () => {
let coordinates = parseCoordinates('AWGames 145.76S 175.34E 120.44A 2700');
expect(coordinates.world.toLowerCase()).toBe('awgames');
expect(coordinates.x).toBe(-175.34);
expect(coordinates.y).toBe(120.44);
expect(coordinates.z).toBe(-145.76);
expect(coordinates.yaw).toBe(2700);
});
/**
* Calling getValueFromCoordinatePiece with a properly formatted portion of coordinates
* parses the value out successfully.
*/
it('getValueFromCoordinatePiece_validCoordinatePiece_parsesSuccessfully', () => {
let value = getValueFromCoordinatePiece('120W');
expect(value).toBe(120);
});
/**
* Takes a given coordinate string value and uses the coordinate regex
* to parse out the pieces.
* @param {String} value The value from which the coordinates are parsed.
* @returns An object containing the X, Y, Z, and Yaw valuesW
* parsed from the given coordinate string.
*/
function parseCoordinates(value) {
let execResults = coordinateRegex.exec(value);
return {
world: execResults[groups.WORLD],
x: getValueFromCoordinatePiece(execResults[groups.EW]),
y: getValueFromCoordinatePiece(execResults[groups.ALT]),
z: getValueFromCoordinatePiece(execResults[groups.NS]),
yaw: getValueFromCoordinatePiece(execResults[groups.YAW])
};
}
/**
* Parses the numeric portion of a coordinate string.
* @param {String} value The value from which numbers are parsed.
* @returns The coordinate value from the formatted string.
*/
function getValueFromCoordinatePiece(value) {
let matches = /^\s*\d*\.?\d+/.exec(value);
let dValue = 0;
if (matches) {
dValue = parseFloat(matches[0]);
if(/[SE]/i.exec(value))
{
dValue *= -1;
}
}
return dValue;
}