/** The focus keywords of a post are stored comma-separated, the first one first. */ export const KEYWORDS_MAX = 5; export const parseKeywords = (value: string): string[] => value .split(',') .map(keyword => keyword.trim()) .filter(keyword => keyword !== ''); /** * The stored list with a new keyword leading it: the old first one goes, * the secondary ones stay (minus the newcomer, if it was among them), and * the list keeps to its five. */ export const leadKeyword = (stored: string, keyword: string): string => { const lead = keyword.trim(); if (lead === '') return stored; const rest = parseKeywords(stored) .slice(1) .filter(entry => entry.toLowerCase() !== lead.toLowerCase()); return [lead, ...rest].slice(0, KEYWORDS_MAX).join(', '); }; /** * The stored list with one of its keywords moved to the front: the others * keep their order behind it. */ export const promoteKeyword = (stored: string, index: number): string => { const list = parseKeywords(stored); if (index <= 0 || index >= list.length) return list.join(', '); return [list[index], ...list.filter((_, at) => at !== index)].join(', '); };