29 lines
843 B
TypeScript
29 lines
843 B
TypeScript
export type SerperAgeFilter = {
|
|
maxAgeDays?: number
|
|
minAgeDays?: number
|
|
}
|
|
|
|
const normalizeDays = (value?: number) => {
|
|
if (typeof value !== 'number') return undefined
|
|
if (!Number.isFinite(value)) return undefined
|
|
if (value <= 0) return undefined
|
|
return Math.floor(value)
|
|
}
|
|
|
|
export function appendSerperAgeModifiers(query: string, filters?: SerperAgeFilter): string {
|
|
if (!filters) return query
|
|
const modifiers: string[] = []
|
|
const normalizedMax = normalizeDays(filters.maxAgeDays)
|
|
const normalizedMin = normalizeDays(filters.minAgeDays)
|
|
|
|
if (typeof normalizedMax === 'number') {
|
|
modifiers.push(`newer_than:${normalizedMax}d`)
|
|
}
|
|
if (typeof normalizedMin === 'number') {
|
|
modifiers.push(`older_than:${normalizedMin}d`)
|
|
}
|
|
|
|
if (modifiers.length === 0) return query
|
|
return `${query} ${modifiers.join(' ')}`
|
|
}
|