export interface FlatToken { name: string; id: string; value: string; colorSchemeAware: boolean; } export interface FilterOptions { search: string; showInternal: boolean; showFigmaOnly?: boolean; isFigmaToken?: (name: string) => boolean; allowedGroups?: string[]; groupBySegment?: number; } export function filterTokens(allTokens: FlatToken[], options: FilterOptions): FlatToken[] { const { search, showInternal, showFigmaOnly, isFigmaToken, allowedGroups, groupBySegment } = options; let result = allTokens; if (!showInternal) { result = result.filter( t => !t.name.startsWith('dsInternalPrimitive.') && !t.name.startsWith('exp.'), ); } if (showFigmaOnly && isFigmaToken) { result = result.filter(t => isFigmaToken(t.name)); } if (search) { const term = search.toLowerCase(); result = result.filter(t => { const figma = t.name.replace(/\./g, '/'); const less = `@${t.name.replace(/\./g, '-')}`; const scss = `$${t.name.replace(/\./g, '-')}`; return ( t.name.toLowerCase().includes(term) || t.value.toLowerCase().includes(term) || t.id.toLowerCase().includes(term) || figma.toLowerCase().includes(term) || less.toLowerCase().includes(term) || scss.toLowerCase().includes(term) ); }); } if (allowedGroups && groupBySegment !== undefined) { const allowed = new Set(allowedGroups); result = [...result].sort((a, b) => { const aGroup = a.name.split('.')[groupBySegment] || ''; const bGroup = b.name.split('.')[groupBySegment] || ''; const aAllowed = allowed.has(aGroup); const bAllowed = allowed.has(bGroup); if (aAllowed && !bAllowed) return -1; if (!aAllowed && bAllowed) return 1; return 0; }); } return result; }