Files
vistassh-py/frontend/src/routes/lookup/+page.js
2024-03-02 00:45:21 -05:00

24 lines
1.1 KiB
JavaScript

import { redirect } from '@sveltejs/kit';
import { get_api_lookup } from '$lib/backend.js';
/** @type {import('./$types').PageLoad} */
export async function load({ url, fetch }) {
let query = (url.searchParams.get('q') || '').replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ');
let ordinal = parseInt(url.searchParams.get('ordinal') || '');
let name = (url.searchParams.get('name') || '').replace(/^\s+|\s+$/g, '').replace(/\s+/g, ' ').toUpperCase();
let items = query ? await get_api_lookup({ fetch, query }) : [];
if(ordinal) items = items.filter(row => row.ordinal == ordinal);
else if(name) items = items.filter(row => row.name.startsWith(name));
let detail, match;
if((items.length == 1) && (url.searchParams.get('rd'))) {
detail = await get_api_lookup({ fetch, query, ordinal: (items[0].ordinal || '0'), force: url.searchParams.get('force') });
if(match = /(^[^\r\n;]+);(?:\([^\)]*?\))? (?:(\d+) )?(\d{3}-\d{2}-\d{4}P?) (.+?)\s*$/m.exec(detail)) {
if(match[2]) throw redirect(302, '/chart/' + match[2]);
if(match[3]) throw redirect(302, '/chart/' + match[3].replace(/[^\dP]/g, ''));
}
}
return {
query, ordinal, name, items, detail
};
}