web-search
Version:
A package to provide search URLs for websites from a search term.
67 lines (66 loc) • 2.23 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.webSearchData = void 0;
var prepend_https_1 = __importDefault(require("./prepend-https"));
var data_json_1 = __importDefault(require("./data.json"));
var data = data_json_1["default"];
/**
* Get search URL in a website from a search term.
* @param websiteURL The website to make the search
* @param searchQuery The search term
* @returns The complete URL to the search on website
*
* @example ```ts
* // Allow compact/human-friendly URL
* webSearch("google.com", "how to make a carrot cake");
* => "https://google.com/search?q=how%20to%20make%20a%20carrot%20cake"
*
* // Allow complete URL, just get the host name
* webSearch("https://duckduckgo.com/?justGet=theHostName", "dogecoin");
* => "https://duckduckgo.com/?q=dogecoin"
*
* // Returns undefined with a unknown website
* webSearch("ea6194cb-47e1-4165-9090-0fa80539e82a.com", "what is uuid?");
* => undefined
* ```
*/
function webSearch(websiteURL, searchQuery) {
var targetHost = new URL(prepend_https_1["default"](websiteURL)).hostname.replace("www.", "");
var matchedWebsite = data.websites.find(function (w) {
var currentHost = new URL(prepend_https_1["default"](w.url_prefix)).hostname.replace("www.", "");
return targetHost === currentHost;
});
return matchedWebsite
? "" + matchedWebsite.url_prefix + encodeURIComponent(searchQuery) + (matchedWebsite.url_suffix || "")
: undefined;
}
exports["default"] = webSearch;
/**
* @returns The complete data of `web-search`,
* with the name and URLs of each website.
*
* @example ```ts
* webSearchData();
* // => {
* // websites: [
* // {
* // "name": "Example Domain",
* // "url_prefix": "https://example.com/search?q="
* // },
* // ...
* // ]
* // }
```
*/
function webSearchData() {
return data;
}
exports.webSearchData = webSearchData;
// For `require` statement
exports = webSearch;
module.exports = webSearch;
exports.webSearchData = webSearchData;
module.exports.webSearchData = webSearchData;
;