forked from ok2cqr/sfle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-grid-locator.html
More file actions
107 lines (90 loc) · 4.45 KB
/
test-grid-locator.html
File metadata and controls
107 lines (90 loc) · 4.45 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Test Grid Locator Validation</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>Maidenhead Locator Validation Tests</h1>
<div id="test-results"></div>
<script>
// Validate Maidenhead locator format (#IO84NJ)
// Supports 2, 4, 6, or 8 character locators (up to 4 pairs)
function isGridLocator(str) {
if (!str || typeof str !== 'string') {
return false;
}
// Must start with #
if (!str.startsWith('#')) {
return false;
}
// Remove the # prefix for validation
const locator = str.substring(1);
// Must be 2, 4, 6, or 8 characters (1-4 pairs)
if (locator.length < 2 || locator.length > 8 || locator.length % 2 !== 0) {
return false;
}
// Validate the pattern: pairs of letters, then numbers, then letters, then numbers
// First pair: 2 letters (field)
if (!/^[A-Ra-r]{2}/.test(locator)) {
return false;
}
// Second pair (if present): 2 digits (square)
if (locator.length >= 4 && !/^[A-Ra-r]{2}[0-9]{2}/.test(locator)) {
return false;
}
// Third pair (if present): 2 letters (subsquare)
if (locator.length >= 6 && !/^[A-Ra-r]{2}[0-9]{2}[A-Xa-x]{2}/.test(locator)) {
return false;
}
// Fourth pair (if present): 2 digits (extended square)
if (locator.length >= 8 && !/^[A-Ra-r]{2}[0-9]{2}[A-Xa-x]{2}[0-9]{2}/.test(locator)) {
return false;
}
return true;
}
// Test cases
const tests = [
// Valid cases
{ input: '#IO', expected: true, description: '2 characters (field only)' },
{ input: '#IO84', expected: true, description: '4 characters (field + square)' },
{ input: '#IO84NJ', expected: true, description: '6 characters (field + square + subsquare)' },
{ input: '#IO84NJ12', expected: true, description: '8 characters (full extended)' },
{ input: '#io84nj', expected: true, description: 'lowercase should work' },
{ input: '#Io84Nj', expected: true, description: 'mixed case should work' },
// Invalid cases
{ input: 'IO84NJ', expected: false, description: 'missing # prefix' },
{ input: '#I', expected: false, description: 'only 1 character' },
{ input: '#IO8', expected: false, description: 'odd number of characters (3)' },
{ input: '#IO84N', expected: false, description: 'odd number of characters (5)' },
{ input: '#IO84NJ1', expected: false, description: 'odd number of characters (7)' },
{ input: '#IO84NJ123', expected: false, description: 'too many characters (9)' },
{ input: '#12', expected: false, description: 'first pair must be letters' },
{ input: '#IOAA', expected: false, description: 'second pair must be digits' },
{ input: '#IO8412', expected: false, description: 'third pair must be letters' },
{ input: '#IO84NJAA', expected: false, description: 'fourth pair must be digits' },
{ input: '#SO84NJ', expected: false, description: 'first letter out of range (S > R)' },
{ input: '#IO84YJ', expected: false, description: 'third letter out of range (Y > X)' },
{ input: '', expected: false, description: 'empty string' },
{ input: null, expected: false, description: 'null value' },
];
let passed = 0;
let failed = 0;
let results = '<h2>Test Results</h2><ul>';
tests.forEach(test => {
const result = isGridLocator(test.input);
const status = result === test.expected ? 'PASS' : 'FAIL';
const color = status === 'PASS' ? 'green' : 'red';
if (status === 'PASS') {
passed++;
} else {
failed++;
}
results += `<li style="color: ${color}">${status}: ${test.description} - Input: "${test.input}" - Expected: ${test.expected}, Got: ${result}</li>`;
});
results += '</ul>';
results += `<h3>Summary: ${passed} passed, ${failed} failed out of ${tests.length} tests</h3>`;
$('#test-results').html(results);
</script>
</body>
</html>