refactor: share unique sorted list normalization helper (#1484)

This commit is contained in:
Elie Habib
2026-03-12 13:22:36 +04:00
committed by GitHub
parent 5841acd06d
commit eb7e63fdb1
4 changed files with 9 additions and 6 deletions

View File

@@ -0,0 +1,3 @@
export function toUniqueSortedLimited(values: string[], limit: number): string[] {
return Array.from(new Set(values)).sort().slice(0, limit);
}

View File

@@ -8,6 +8,7 @@ import type {
import { getCachedJsonBatch, cachedFetchJson } from '../../../_shared/redis';
import { CHROME_UA } from '../../../_shared/constants';
import { ISO2_TO_ISO3 } from './_shared';
import { toUniqueSortedLimited } from '../../../_shared/normalize-list';
const REDIS_CACHE_KEY = 'conflict:humanitarian:v1';
const REDIS_CACHE_TTL = 21600;
@@ -100,8 +101,7 @@ export async function getHumanitarianSummaryBatch(
const normalized = req.countryCodes
.map((c) => c.trim().toUpperCase())
.filter((c) => ISO2_PATTERN.test(c));
const uniqueSorted = Array.from(new Set(normalized)).sort();
const limitedList = uniqueSorted.slice(0, 25);
const limitedList = toUniqueSortedLimited(normalized, 25);
const results: Record<string, HumanitarianCountrySummary> = {};
const toFetch: string[] = [];

View File

@@ -7,6 +7,7 @@ import type {
} from '../../../../src/generated/server/worldmonitor/economic/v1/service_server';
import { getCachedJsonBatch, cachedFetchJson } from '../../../_shared/redis';
import { toUniqueSortedLimited } from '../../../_shared/normalize-list';
const FRED_API_BASE = 'https://api.stlouisfed.org/fred';
const REDIS_CACHE_KEY = 'economic:fred:v1';
@@ -73,8 +74,7 @@ export async function getFredSeriesBatch(
const normalized = req.seriesIds
.map((id) => id.trim().toUpperCase())
.filter((id) => ALLOWED_SERIES.has(id));
const uniqueSorted = Array.from(new Set(normalized)).sort();
const limitedList = uniqueSorted.slice(0, 10);
const limitedList = toUniqueSortedLimited(normalized, 10);
const limit = req.limit > 0 ? Math.min(req.limit, 1000) : 120;
const results: Record<string, FredSeries> = {};

View File

@@ -8,6 +8,7 @@ import type {
import { mapWingbitsDetails } from './_shared';
import { CHROME_UA } from '../../../_shared/constants';
import { getCachedJsonBatch, cachedFetchJson } from '../../../_shared/redis';
import { toUniqueSortedLimited } from '../../../_shared/normalize-list';
interface CachedAircraftDetails {
details: AircraftDetails | null;
@@ -25,8 +26,7 @@ export async function getAircraftDetailsBatch(
const normalized = req.icao24s
.map((id) => id.trim().toLowerCase())
.filter((id) => id.length > 0);
const uniqueSorted = Array.from(new Set(normalized)).sort();
const limitedList = uniqueSorted.slice(0, 10);
const limitedList = toUniqueSortedLimited(normalized, 10);
// Redis shared cache — batch GET all keys in a single pipeline round-trip
const SINGLE_KEY = 'military:aircraft:v1';