utilitas 1998.2.37 → 1998.2.39

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.37",
4
+ "version": "1998.2.39",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",
package/lib/shot.mjs CHANGED
@@ -5,6 +5,7 @@ import { sha256 } from './encryption.mjs';
5
5
  import { distillHtml } from './web.mjs';
6
6
 
7
7
  import {
8
+ ensureInt,
8
9
  ensureString, extract, ignoreErrFunc, inBrowser, parseJson, parseVersion,
9
10
  throwError, which
10
11
  } from './utilitas.mjs';
@@ -160,66 +161,28 @@ const initSearch = (options) => {
160
161
  };
161
162
 
162
163
  const search = async (query, options) => {
164
+ const [key, cx, min, max]
165
+ = [options?.apiKey || googleApiKey, options?.cx || googleCx, 1, 10];
163
166
  assert(query, 'Query is required.');
164
- const [GOOGLE, DUCKDUCKGO] = ['GOOGLE', 'DUCKDUCKGO'];
165
- const provider = ensureString(
166
- options?.provider || searchProvider || DUCKDUCKGO, { case: 'UP' }
167
- );
168
- let apiKey = options?.apiKey || googleApiKey, cx = options?.cx || googleCx,
169
- url, parser;
170
- switch (provider) {
171
- case GOOGLE:
172
- assert(apiKey, 'API key is required.');
173
- assert(cx, 'CX is required.');
174
- url = 'https://www.googleapis.com/customsearch/v1'
175
- + `?key=${encodeURIComponent(apiKey)}`
176
- + `&cx=${encodeURIComponent(cx)}`
177
- + `&q=${encodeURIComponent(query)}`;
178
- break;
179
- case DUCKDUCKGO:
180
- url = 'https://api.duckduckgo.com/'
181
- + `?q=${encodeURIComponent(query)}&format=json&skip_disambig=1`;
182
- parser = x => x.FirstURL ? {
183
- title: x.FirstURL.replace(/^.*\/([^\/]*)$/, '$1').replace(/_/g, ' '),
184
- link: x.FirstURL, snippet: x.Text,
185
- image: `https://duckduckgo.com${x.Icon.URL}`, source: null,
186
- } : null;
187
- break;
188
- default:
189
- throwError(`Invalid provider: ${provider}.`);
190
- }
167
+ assert(key, 'API key is required.');
168
+ assert(cx, 'CX is required.');
169
+ const num = ensureInt(options?.num || max, { min, max });
170
+ const start = ensureInt(options?.start || min, { min });
171
+ assert(start + num <= 100, 'Reached maximum search limit.');
172
+ const url = 'https://www.googleapis.com/customsearch/v1'
173
+ + `?key=${encodeURIComponent(key)}&cx=${encodeURIComponent(cx)}`
174
+ + `&q=${encodeURIComponent(query)}&num=${num}&start=${start}`
175
+ + (options?.image ? `&searchType=image` : '');
191
176
  const resp = await get(url, { encode: _JSON, ...options || {} });
192
- let result = [];
193
- if (options?.raw) {
194
- result = resp.content;
195
- } else if (provider === GOOGLE) {
196
- result.push(...resp?.content?.items.map(x => ({
197
- title: x.title, link: x.link, snippet: x.snippet,
198
- image: x.pagemap?.cse_image?.[0]?.src || null,
199
- })));
200
- } else if (provider === DUCKDUCKGO) {
201
- const cnt = resp?.content;
202
- if (cnt?.Abstract) {
203
- result.push({
204
- title: cnt?.Heading + (cnt?.Entity ? ` (${cnt.Entity})` : ''),
205
- link: cnt?.AbstractURL, snippet: cnt?.AbstractText,
206
- image: cnt?.Image ? `https://duckduckgo.com${cnt.Image}` : null,
207
- source: cnt?.AbstractSource,
208
- });
209
- if (cnt?.Results) {
210
- result.push(...cnt.Results.map(x => ({
211
- title: x.Text, link: x.FirstURL, snippet: null,
212
- image: x.Icon ? `https://duckduckgo.com${x.Icon.URL}` : null,
213
- source: null,
214
- })));
215
- }
216
- } else if (cnt?.RelatedTopics?.[0] && !options?.no_recurse) {
217
- result = await search(cnt.RelatedTopics[0].FirstURL.replace(
218
- /^.*\/([^\/]*)$/, '$1'
219
- ).replace(/_/g, ' '), { ...options, no_recurse: true });
220
- }
221
- }
222
- return result;
177
+ return options?.raw ? resp.content : {
178
+ totalResults: resp?.content?.searchInformation?.totalResults || 0,
179
+ startIndex: resp?.content?.queries?.request?.[0]?.startIndex || 1,
180
+ items: resp?.content?.items.map(x => ({
181
+ title: x.title, link: options?.image ? null : x.link,
182
+ snippet: x.snippet,
183
+ image: (options?.image ? x.link : x.pagemap?.cse_image?.[0]?.src) || null,
184
+ })),
185
+ };
223
186
  };
224
187
 
225
188
  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.37",
4
+ "version": "1998.2.39",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",