library-index.js (1661B)
1 import { packageIndex, packageReleases } from './packages.js'; 2 export function libraryIndex(input) { 3 const releases = packageIndex(input), rows = input.releases; 4 return releases.map((release, i) => { 5 const row = rows[i]; 6 if (typeof row['description'] !== 'string' || row['description'].length > 2048 || 7 typeof row['alias'] !== 'string' || !/^[A-Za-z_][A-Za-z0-9_]{0,127}$/u.test(row['alias']) || 8 typeof row['reference'] !== 'string' || !/^libraries\/[A-Za-z0-9._-]+\.md$/u.test(row['reference']) || 9 (row['usage'] !== undefined && (typeof row['usage'] !== 'string' || row['usage'].length > 8192)) || 10 (row['example'] !== undefined && (typeof row['example'] !== 'string' || row['example'].length > 32768)) || 11 !Array.isArray(row['names']) || row['names'].length > 4096 || row['names'].some(n => typeof n !== 'string' || n.length > 512)) 12 throw new Error('Invalid library reference'); 13 return { ...release, description: row['description'], names: row['names'], alias: row['alias'], reference: row['reference'], 14 ...(typeof row['usage'] === 'string' ? { usage: row['usage'] } : {}), ...(typeof row['example'] === 'string' ? { example: row['example'] } : {}) }; 15 }); 16 } 17 export function latestLibraries(rows) { 18 return [...new Set(rows.map(r => r.name))].map(name => packageReleases(rows, name)[0]); 19 } 20 export function matchesLibrary(row, query) { 21 const words = query.toLowerCase().trim().split(/\s+/u); 22 const text = `${row.title} ${row.description} ${row.names.join(' ')}`.toLowerCase(); 23 return words.every(word => text.includes(word)); 24 }