Skip to content

Commit e7790fc

Browse files
committed
refactor: Move all cache timestamp utilities into their new module
1 parent 0912a59 commit e7790fc

File tree

5 files changed

+17
-16
lines changed

5 files changed

+17
-16
lines changed

harmonizer/timestamp.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import { isDefined } from '@/utils/predicate.ts';
2+
import { assert } from 'std/assert/assert.ts';
23
import type { ReleaseInfo } from './types.ts';
34

5+
/** Asserts that the given number is a valid timestamp. */
6+
export function assertTimestamp(ts: number) {
7+
assert(Number.isSafeInteger(ts) && ts >= 0, 'Timestamp has to be a non-negative integer');
8+
}
9+
410
/** Formats the given timestamp as ISO YYYY-MM-DD date. */
511
export function formatTimestampAsISODate(unixSeconds: number): string {
612
return new Date(unixSeconds * 1000).toISOString().substring(0, 10);
713
}
814

15+
/** Formats the given timestamp as ISO YYYY-MM-DDTHH:mm:ssZ string. */
16+
export function formatTimestampAsISOString(unixSeconds: number): string {
17+
return new Date(unixSeconds * 1000).toISOString().replace('.000', '');
18+
}
19+
920
/** Returns the date and time when the whole data from all providers was cached (in seconds since the UNIX epoch). */
1021
export function getMaxCacheTimestamp(info: ReleaseInfo): number | undefined {
1122
const cacheTimestamps = info.providers.map((provider) => provider.cacheTime).filter(isDefined);

server/components/ProviderList.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ProviderIcon } from './ProviderIcon.tsx';
22

3-
import { formatTimestampAsISOString } from '@/utils/time.ts';
3+
import { formatTimestampAsISOString } from '@/harmonizer/timestamp.ts';
44
import type { ProviderInfo } from '@/harmonizer/types.ts';
55

66
export function ProviderList({ providers }: { providers: ProviderInfo[] }) {

server/permalink.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getMaxCacheTimestamp } from '@/harmonizer/timestamp.ts';
12
import type { ReleaseInfo } from '@/harmonizer/types.ts';
23
import { isDefined } from '@/utils/predicate.ts';
34

@@ -9,7 +10,6 @@ export function encodeReleaseLookupState(info: ReleaseInfo): URLSearchParams {
910
);
1011
const providersUsedAsTemplate = info.providers.filter((provider) => provider.isTemplate);
1112
const usedRegion = info.providers.map((provider) => provider.lookup.region).find(isDefined);
12-
const cacheTimestamps = info.providers.map((provider) => provider.cacheTime).filter(isDefined);
1313

1414
// Add provider IDs for all providers which were looked up by ID or URL.
1515
const state = new URLSearchParams(
@@ -41,8 +41,8 @@ export function encodeReleaseLookupState(info: ReleaseInfo): URLSearchParams {
4141
}
4242

4343
// Maximum timestamp can be used to load the latest snapshot up to this timestamp for each provider.
44-
const maxTimestamp = Math.max(...cacheTimestamps);
45-
if (Number.isSafeInteger(maxTimestamp)) {
44+
const maxTimestamp = getMaxCacheTimestamp(info);
45+
if (maxTimestamp) {
4646
state.append('ts', maxTimestamp.toFixed(0));
4747
}
4848

server/state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { ReleaseLookupParameters } from '@/lookup.ts';
2+
import { assertTimestamp } from '@/harmonizer/timestamp.ts';
23
import type { CountryCode, ProviderNameAndId, ReleaseOptions } from '@/harmonizer/types.ts';
34
import { providers } from '@/providers/mod.ts';
45
import { ensureValidGTIN } from '@/utils/gtin.ts';
56
import { isNotEmpty } from '@/utils/predicate.ts';
67
import { assertCountryCode } from '@/utils/regions.ts';
7-
import { assertTimestamp } from '@/utils/time.ts';
88
import { getCookies } from 'std/http/cookie.ts';
99

1010
/** Default regions which will be used for lookups if no regions could be obtained otherwise. */

utils/time.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { assert } from 'std/assert/assert.ts';
2-
1+
/** Formats milliseconds as colon-separated duration string. */
32
export function formatDuration(ms: number | undefined, { showMs }: {
43
showMs?: boolean;
54
} = {}): string | undefined {
@@ -36,15 +35,6 @@ export function formatDuration(ms: number | undefined, { showMs }: {
3635
return duration;
3736
}
3837

39-
export function formatTimestampAsISOString(unixSeconds: number): string {
40-
return new Date(unixSeconds * 1000).toISOString().replace('.000', '');
41-
}
42-
43-
/** Asserts that the given number is a valid timestamp. */
44-
export function assertTimestamp(ts: number) {
45-
assert(Number.isSafeInteger(ts) && ts >= 0, 'Timestamp has to be a non-negative integer');
46-
}
47-
4838
/** Converts an ISO-8601 duration (e.g. PT41M5S) to milliseconds. */
4939
export function parseISODuration(duration: string): number {
5040
const match = duration.match(/PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?/);

0 commit comments

Comments
 (0)