utilitas 1998.2.22 → 1998.2.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/manifest.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  const manifest = {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "1998.2.22",
4
+ "version": "1998.2.23",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
package/lib/shot.mjs CHANGED
@@ -150,16 +150,58 @@ const get = async (url, options) => {
150
150
 
151
151
  const search = async (query, options) => {
152
152
  assert(query, 'Query is required.');
153
- assert(options?.apiKey, 'API key is required.');
154
- assert(options?.cx, 'CX is required.');
155
- const resp = await get(
156
- `https://www.googleapis.com/customsearch/v1?key=${options.apiKey}&cx=${options.cx}&q=${encodeURIComponent(query)}`,
157
- { encode: _JSON, ...options || {} }
158
- )
159
- return options?.raw ? resp.content : resp.content.items.map(x => ({
160
- title: x.title, link: x.link, snippet: x.snippet,
161
- image: x.pagemap?.cse_image?.[0]?.src,
162
- }));
153
+ const [GOOGLE, DUCKDUCKGO] = ['GOOGLE', 'DUCKDUCKGO'];
154
+ const provider = ensureString(options?.provider, { case: 'UP' }) || DUCKDUCKGO;
155
+ let url, parser;
156
+ switch (provider) {
157
+ case GOOGLE:
158
+ assert(options?.apiKey, 'API key is required.');
159
+ assert(options?.cx, 'CX is required.');
160
+ url = `https://www.googleapis.com/customsearch/v1?key=${options.apiKey}&cx=${options.cx}&q=${encodeURIComponent(query)}`;
161
+ break;
162
+ case DUCKDUCKGO:
163
+ url = `https://api.duckduckgo.com/?q=${encodeURIComponent(query)}&format=json&skip_disambig=1`;
164
+ parser = x => x.FirstURL ? {
165
+ title: x.FirstURL.replace(/^.*\/([^\/]*)$/, '$1').replace(/_/g, ' '),
166
+ link: x.FirstURL, snippet: x.Text,
167
+ image: `https://duckduckgo.com${x.Icon.URL}`, source: null,
168
+ } : null;
169
+ break;
170
+ default:
171
+ throwError(`Invalid provider: ${provider}.`);
172
+ }
173
+ const resp = await get(url, { encode: _JSON, ...options || {} });
174
+ let result = [];
175
+ if (options?.raw) {
176
+ result = resp.content;
177
+ } else if (provider === GOOGLE) {
178
+ result.push(...resp?.content?.items.map(x => ({
179
+ title: x.title, link: x.link, snippet: x.snippet,
180
+ image: x.pagemap?.cse_image?.[0]?.src || null,
181
+ })));
182
+ } else if (provider === DUCKDUCKGO) {
183
+ const cnt = resp?.content;
184
+ if (cnt?.Abstract) {
185
+ result.push({
186
+ title: cnt?.Heading + (cnt?.Entity ? ` (${cnt.Entity})` : ''),
187
+ link: cnt?.AbstractURL, snippet: cnt?.AbstractText,
188
+ image: cnt?.Image ? `https://duckduckgo.com${cnt.Image}` : null,
189
+ source: cnt?.AbstractSource,
190
+ });
191
+ if (cnt?.Results) {
192
+ result.push(...cnt.Results.map(x => ({
193
+ title: x.Text, link: x.FirstURL, snippet: null,
194
+ image: x.Icon ? `https://duckduckgo.com${x.Icon.URL}` : null,
195
+ source: null,
196
+ })));
197
+ }
198
+ } else if (cnt?.RelatedTopics?.[0] && !options?.no_recurse) {
199
+ result = await search(cnt.RelatedTopics[0].FirstURL.replace(
200
+ /^.*\/([^\/]*)$/, '$1'
201
+ ).replace(/_/g, ' '), { ...options, no_recurse: true });
202
+ }
203
+ }
204
+ return result;
163
205
  };
164
206
 
165
207
  export default get;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "utilitas",
3
3
  "description": "Just another common utility for JavaScript.",
4
- "version": "1998.2.22",
4
+ "version": "1998.2.23",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",