1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-21 04:19:37 +02:00
flame/client/src/utility/searchParser.ts

54 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-06-24 12:53:45 +02:00
import { queries } from './searchQueries.json';
2021-09-06 12:24:01 +02:00
import { Query, SearchResult } from '../interfaces';
2021-06-24 12:53:45 +02:00
import { searchConfig } from '.';
2021-09-06 12:24:01 +02:00
export const searchParser = (searchQuery: string): SearchResult => {
const result: SearchResult = {
isLocal: false,
2021-10-06 12:01:07 +02:00
isURL: false,
2021-09-06 12:47:17 +02:00
sameTab: false,
search: '',
query: {
name: '',
prefix: '',
template: '',
},
2021-09-06 12:24:01 +02:00
};
2021-10-06 12:01:07 +02:00
// Check if url or ip was passed
const urlRegex =
/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?|^((http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;
result.isURL = urlRegex.test(searchQuery);
// Match prefix and query
const splitQuery = searchQuery.match(/^\/([a-z]+)[ ](.+)$/i);
2021-09-06 12:24:01 +02:00
const prefix = splitQuery
? splitQuery[1]
: searchConfig('defaultSearchProvider', 'l');
const search = splitQuery
? encodeURIComponent(splitQuery[2])
: encodeURIComponent(searchQuery);
2021-06-24 12:53:45 +02:00
const query = queries.find((q: Query) => q.prefix === prefix);
2021-10-06 12:01:07 +02:00
// If search provider was found
2021-06-24 12:53:45 +02:00
if (query) {
2021-09-06 12:47:17 +02:00
result.query = query;
result.search = search;
2021-06-24 12:53:45 +02:00
2021-09-06 12:24:01 +02:00
if (prefix === 'l') {
result.isLocal = true;
2021-06-24 12:53:45 +02:00
} else {
2021-09-06 12:47:17 +02:00
result.sameTab = searchConfig('searchSameTab', false);
2021-06-24 12:53:45 +02:00
}
2021-09-06 12:24:01 +02:00
return result;
2021-06-24 12:53:45 +02:00
}
2021-09-06 12:24:01 +02:00
return result;
};