utilitas 1998.2.21 → 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.21",
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
@@ -1,8 +1,8 @@
1
- import { distillHtml } from './web.mjs';
2
1
  import { fileTypeFromBuffer } from 'file-type';
3
- import { join } from 'path';
4
2
  import { promises as fs } from 'fs';
3
+ import { join } from 'path';
5
4
  import { sha256 } from './encryption.mjs';
5
+ import { distillHtml } from './web.mjs';
6
6
 
7
7
  import {
8
8
  ensureString, extract, ignoreErrFunc, inBrowser, parseJson, parseVersion,
@@ -148,6 +148,62 @@ const get = async (url, options) => {
148
148
  };
149
149
  };
150
150
 
151
+ const search = async (query, options) => {
152
+ assert(query, 'Query is required.');
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;
205
+ };
206
+
151
207
  export default get;
152
208
  export {
153
209
  checkVersion,
@@ -157,4 +213,5 @@ export {
157
213
  getJson,
158
214
  getParsedHtml,
159
215
  getVersionOnNpm,
216
+ search
160
217
  };
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.21",
4
+ "version": "1998.2.23",
5
5
  "private": false,
6
6
  "homepage": "https://github.com/Leask/utilitas",
7
7
  "main": "index.mjs",