Skip to content

Commit b1b8753

Browse files
authored
Fix create discrete times bug (#7028)
* Add a unit test that will fail. * Update unit test. * fix bugs. * Fixed a bug and improved unit tests. * Update doc.
1 parent d0906be commit b1b8753

File tree

4 files changed

+292
-13
lines changed

4 files changed

+292
-13
lines changed

CHANGES.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
- Update `thredds-catalog-crawler` to `0.0.6`
1111
- `WebMapServiceCatalogItem` will drop problematic query parameters from `url` when calling `GetCapabilities` (eg `"styles","srs","crs","format"`)
1212
- Fixed regression causing explorer window not to display instructions when first opened.
13-
- [The next improvement]
1413
- Enable eslint for typescript: plugin:@typescript-eslint/eslint-recommended
1514
- Fixed a bug where the search box was missing for small screen devices.
1615
- Prevent user adding empty web url
1716
- Fix bug where search results shown in `My Data` tab
17+
- Fix bug in function createDiscreteTimesFromIsoSegments where it might create duplicate timestamps.
18+
- [The next improvement]
1819

1920
#### 8.4.1 - 2023-12-08
2021

lib/Core/createDiscreteTimes.ts

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export default function createDiscreteTimesFromIsoSegments(
7373
// Add intervals starting at start until:
7474
// we go past the stop date, or
7575
// we go past the max limit
76+
//
77+
// The stop date might be included if it is the same as the current.add(duration).
7678
while (
7779
current &&
7880
current.isSameOrBefore(stop) &&
@@ -86,11 +88,14 @@ export default function createDiscreteTimesFromIsoSegments(
8688
++count;
8789
}
8890

91+
current.subtract(duration);
92+
8993
if (count >= maxRefreshIntervals) {
9094
console.warn(
9195
"Interval has more than the allowed number of discrete times. Consider setting `maxRefreshIntervals`."
9296
);
9397
} else if (!current.isSame(stop)) {
98+
// Add stop date if it has not been added yet.
9499
result.push({
95100
time: formatMomentForWms(stop, duration),
96101
tag: undefined

test/Core/createDiscreteTimesSpec.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { Complete } from "../../lib/Core/TypeModifiers";
2+
import createDiscreteTimesFromIsoSegments from "../../lib/Core/createDiscreteTimes";
3+
4+
describe("createDiscreteTimesFromIsoSegments", () => {
5+
it("should not duplicate stop date", () => {
6+
const result: Complete<{
7+
time?: string;
8+
tag?: string;
9+
}>[] = [];
10+
const value = "2018-02-07T00:00:00.000Z/2018-03-09T00:00:00.000Z/P15D";
11+
const isoSegments = value.split("/");
12+
const maxRefreshIntervals = 10;
13+
createDiscreteTimesFromIsoSegments(
14+
result,
15+
isoSegments[0],
16+
isoSegments[1],
17+
isoSegments[2],
18+
maxRefreshIntervals
19+
);
20+
expect(result.length).toBe(3);
21+
expect(result[0].time).toBe("2018-02-07T00:00:00Z");
22+
expect(result[1].time).toBe("2018-02-22T00:00:00Z");
23+
expect(result[2].time).toBe("2018-03-09T00:00:00Z");
24+
});
25+
26+
it("should include a stop date", () => {
27+
const result: Complete<{
28+
time?: string;
29+
tag?: string;
30+
}>[] = [];
31+
const value = "2018-02-07T00:00:00.000Z/2018-03-10T00:00:00.000Z/P15D";
32+
const isoSegments = value.split("/");
33+
const maxRefreshIntervals = 10;
34+
createDiscreteTimesFromIsoSegments(
35+
result,
36+
isoSegments[0],
37+
isoSegments[1],
38+
isoSegments[2],
39+
maxRefreshIntervals
40+
);
41+
expect(result.length).toBe(4);
42+
expect(result[0].time).toBe("2018-02-07T00:00:00Z");
43+
expect(result[1].time).toBe("2018-02-22T00:00:00Z");
44+
expect(result[2].time).toBe("2018-03-09T00:00:00Z");
45+
expect(result[3].time).toBe("2018-03-10T00:00:00Z");
46+
});
47+
48+
it("should limit time number to maxRefreshInterval", () => {
49+
const result: Complete<{
50+
time?: string;
51+
tag?: string;
52+
}>[] = [];
53+
const value = "2018-02-07T00:00:00.000Z/2018-03-09T00:00:00.000Z/P15D";
54+
const isoSegments = value.split("/");
55+
const maxRefreshIntervals = 2;
56+
createDiscreteTimesFromIsoSegments(
57+
result,
58+
isoSegments[0],
59+
isoSegments[1],
60+
isoSegments[2],
61+
maxRefreshIntervals
62+
);
63+
expect(result.length).toBe(maxRefreshIntervals);
64+
expect(result[0].time).toBe("2018-02-07T00:00:00Z");
65+
expect(result[1].time).toBe("2018-02-22T00:00:00Z");
66+
});
67+
});

0 commit comments

Comments
 (0)