tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

library-index.ts (1947B)


      1 import { packageIndex, packageReleases, type Release } from './packages.js';
      2 export interface Library extends Release { description: string; names: string[]; alias: string; reference: string; usage?: string; example?: string; }
      3 export function libraryIndex(input: unknown): Library[] {
      4   const releases = packageIndex(input), rows = (input as { releases: Record<string, unknown>[] }).releases;
      5   return releases.map((release, i) => {
      6     const row = rows[i]!;
      7     if (typeof row['description'] !== 'string' || row['description'].length > 2048 ||
      8         typeof row['alias'] !== 'string' || !/^[A-Za-z_][A-Za-z0-9_]{0,127}$/u.test(row['alias']) ||
      9         typeof row['reference'] !== 'string' || !/^libraries\/[A-Za-z0-9._-]+\.md$/u.test(row['reference']) ||
     10         (row['usage'] !== undefined && (typeof row['usage'] !== 'string' || row['usage'].length > 8192)) ||
     11         (row['example'] !== undefined && (typeof row['example'] !== 'string' || row['example'].length > 32768)) ||
     12         !Array.isArray(row['names']) || row['names'].length > 4096 || row['names'].some(n => typeof n !== 'string' || n.length > 512)) throw new Error('Invalid library reference');
     13     return { ...release, description: row['description'], names: row['names'] as string[], 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: Library[]): Library[] {
     18   return [...new Set(rows.map(r => r.name))].map(name => packageReleases(rows, name)[0] as Library);
     19 }
     20 export function matchesLibrary(row: { title: string; description: string; names: string[] }, query: string): boolean {
     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 }