| | |
| | |
| | |
| |
|
| | export function getLangName(lang) { |
| | |
| | |
| | if (!lang) return 'Unknown'; |
| | return lang; |
| | } |
| |
|
| | export function escapeHtml(str) { |
| | if (!str) return ''; |
| | return str |
| | .replace(/&/g, '&') |
| | .replace(/</g, '<') |
| | .replace(/>/g, '>') |
| | .replace(/"/g, '"') |
| | .replace(/'/g, '''); |
| | } |
| |
|
| | export function truncate(text, maxLength) { |
| | if (!text || text.length <= maxLength) return text; |
| | return text.slice(0, maxLength).trim() + '…'; |
| | } |
| |
|
| | export async function handleApiResponse(response, context = 'request') { |
| | if (response.ok) return response; |
| |
|
| | if (response.status === 429) { |
| | const retryAfter = response.headers.get('Retry-After') || '60'; |
| | throw new Error(`Too many requests. Please wait ${retryAfter} seconds and try again.`); |
| | } |
| | if (response.status === 404) { |
| | throw new Error(`Word not found in the database`); |
| | } |
| | if (response.status >= 500) { |
| | throw new Error(`Server is temporarily unavailable. Please try again in a moment.`); |
| | } |
| | throw new Error(`Failed to complete ${context}`); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | export function checkNoEtymology(data) { |
| | if (data && data.no_etymology === true) { |
| | return { noEtymology: true, lexeme: data.lexeme || '' }; |
| | } |
| | return null; |
| | } |
| |
|
| | export function buildNodeLabel(node) { |
| | const langName = node.lang_name || getLangName(node.lang); |
| | const displayWord = node.lexeme || node.id; |
| | |
| | return displayWord + '\n(' + langName.toLowerCase() + ')'; |
| | } |
| |
|