| 1 | export type StyleCaseItem = { |
| 2 | styleCase?: string |
| 3 | } |
| 4 | |
| 5 | export type StyleCaseOption = { |
| 6 | label: string |
| 7 | count: number |
| 8 | } |
| 9 | |
| 10 | export function parseStyleCases(styleCase?: string): string[] { |
| 11 | if (!styleCase) return [] |
| 12 | return Array.from( |
| 13 | new Set( |
| 14 | styleCase |
| 15 | .split(/[、,,;;\n]/) |
| 16 | .map((item) => item.trim()) |
| 17 | .filter(Boolean) |
| 18 | ) |
| 19 | ) |
| 20 | } |
| 21 | |
| 22 | export function buildStyleCaseOptions(items: StyleCaseItem[]): StyleCaseOption[] { |
| 23 | const counts = new Map<string, number>() |
| 24 | for (const item of items) { |
| 25 | for (const styleCase of parseStyleCases(item.styleCase)) { |
| 26 | counts.set(styleCase, (counts.get(styleCase) || 0) + 1) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | return Array.from(counts, ([label, count]) => ({ label, count })).sort( |
| 31 | (a, b) => b.count - a.count || a.label.localeCompare(b.label, 'zh-CN') |
| 32 | ) |
| 33 | } |
| 34 | |
| 35 | export function filterByStyleCase<T extends StyleCaseItem>(items: T[], styleCase: string): T[] { |
| 36 | if (!styleCase) return items |
| 37 | return items.filter((item) => parseStyleCases(item.styleCase).includes(styleCase)) |
| 38 | } |
| 39 | |
| 40 | export type StyleSearchItem = { |
| 41 | label?: string |
| 42 | description?: string |
| 43 | styleCase?: string |
| 44 | } |
| 45 | |
| 46 | /** 按关键词模糊过滤风格(匹配名称、描述、用途)。空关键词返回全部。 */ |
| 47 | export function filterByStyleKeyword<T extends StyleSearchItem>(items: T[], query: string): T[] { |
| 48 | const keyword = query.trim().toLowerCase() |
| 49 | if (!keyword) return items |
| 50 | return items.filter((item) => |
| 51 | [item.label, item.description, item.styleCase] |
| 52 | .filter((value): value is string => typeof value === 'string' && value.length > 0) |
| 53 | .join(' ') |
| 54 | .toLowerCase() |
| 55 | .includes(keyword) |
| 56 | ) |
| 57 | } |
| 58 |