UNPKG

@gowiz/searchbar

Version:

Different search bars powered by Gowiz search engine technology

128 lines 5.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.api_key_is_valid = exports.searchcontainer_has_valid_props = void 0; const string_util_1 = require("./string_util"); function query_is_valid(query) { const max_url_length = 2048; const base_URL = 'https://gowiz.eu/search/'; const request_url = base_URL + encodeURIComponent(query); if (request_url.length > max_url_length) { throw new Error('The entered query is too long'); } if (string_util_1.string_contains_html_tags(query)) { throw new Error(query + ' is not a valid query as it contains HTML tags'); } } function placeholder_is_valid(placeholder) { const encoded_component = encodeURIComponent(placeholder); if (encoded_component.length > 150) { throw new Error('Entered placeholder it too long. Maximum placeholder size is 150 characters'); } else if (string_util_1.string_contains_html_tags(placeholder)) { throw new Error(placeholder + ' is not a valid placeholder as it contains HTML tags'); } } function search_suggestions_are_valid(searchSuggestions) { if (searchSuggestions.length > 25) { throw new Error('The maximum number of search suggestions can be 25. Currently ' + searchSuggestions.length + ' search suggestions have been given'); } let search_suggestions_count = {}; for (let i = 0; i < searchSuggestions.length; i++) { const el = searchSuggestions[i]; if (el) { const result = search_suggestions_count[el] !== undefined; if (result) { throw new Error('All search suggestions need to be unique. The first duplicate search suggestion is ' + el); } search_suggestions_count[el] = 1; if (string_util_1.string_contains_html_tags(el)) { throw new Error(el + ' is not a valid search suggestion as it contains HTML tags'); } if (el.length > 150) { const error_index = i + 1; throw new Error('The ' + string_util_1.format_index(error_index) + ' search suggestion is too long. The maximum search suggestion size is 150 characters'); } } else { const error_index = i + 1; throw new Error('Search suggestion can not be empty. The ' + string_util_1.format_index(error_index) + ' search suggestion is empty'); } } } function search_domains_are_valid(searchDomains) { if (searchDomains.length > 10) { throw new Error('The maximum number of domains search can be restricted to is 10. Currently ' + searchDomains.length + ' search domains have been entered'); } let search_domains_count = {}; for (let i = 0; i < searchDomains.length; i++) { let el = searchDomains[i]; if (el) { if (el != el.trim()) { const error_index = i + 1; throw new Error('The ' + string_util_1.format_index(error_index) + ' search domain should be trimmed'); } el = string_util_1.domain_to_host(el); const result = search_domains_count[el] !== undefined; if (result) { throw new Error('All search domains need to be unique. The first duplicate search domain is ' + searchDomains[i]); } search_domains_count[el] = 1; if (!string_util_1.url_is_valid(el)) { throw new Error(el + ' is not a valid search domain'); } } else { const error_index = i + 1; throw new Error('The ' + string_util_1.format_index(error_index) + ' search domain is empty'); } } } function max_results_is_valid(maxResults) { if (maxResults < 0) { throw new Error('Maximum results size can not be negative'); } else if (maxResults > 25) { throw new Error('Maximum results size can not be more than 25. Currently entered max results size is ' + maxResults); } } function max_results_and_searchsuggestions(maxResults, searchSuggestions) { if (searchSuggestions.length > 0 && maxResults == 0) { throw new Error('Maximum results size can not be zero if search suggestions are provided'); } } function searchcontainer_has_valid_props(props) { const { query = '', searchSuggestions = [], searchDomains = [], placeholder = 'Search on Gowiz', maxResults = 10, } = props; query_is_valid(query); search_suggestions_are_valid(searchSuggestions); search_domains_are_valid(searchDomains); placeholder_is_valid(placeholder); max_results_is_valid(maxResults); max_results_and_searchsuggestions(maxResults, searchSuggestions); } exports.searchcontainer_has_valid_props = searchcontainer_has_valid_props; function api_key_is_valid(key) { if (key === null) { throw new Error('API_KEY can not be null'); } if (key === undefined) { throw new Error('API_KEY can not be undefined'); } if (key.length == 0) { throw new Error('API_KEY can not be empty'); } if (key !== key.trim()) { throw new Error('API_KEY needs to be trimmed'); } const nr_of_words = key.split(' ').length; if (nr_of_words > 1) { throw new Error('API_KEY can not be multiple words'); } } exports.api_key_is_valid = api_key_is_valid; //# sourceMappingURL=component_validation.js.map