opencode-search 1.0.0-next.9 → 1.1.0-next.0
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/README.md +27 -27
- package/dist/{search.d.ts → search.d.mts} +1 -2
- package/dist/search.mjs +947 -0
- package/package.json +22 -16
- package/dist/package.json +0 -68
- package/dist/search.d.ts.map +0 -1
- package/dist/search.js +0 -20
- package/dist/search.js.map +0 -1
- package/dist/src/cache.d.ts +0 -3
- package/dist/src/cache.d.ts.map +0 -1
- package/dist/src/cache.js +0 -43
- package/dist/src/cache.js.map +0 -1
- package/dist/src/format.d.ts +0 -20
- package/dist/src/format.d.ts.map +0 -1
- package/dist/src/format.js +0 -46
- package/dist/src/format.js.map +0 -1
- package/dist/src/html.d.ts +0 -8
- package/dist/src/html.d.ts.map +0 -1
- package/dist/src/html.js +0 -28
- package/dist/src/html.js.map +0 -1
- package/dist/src/normalize-blurb.d.ts +0 -2
- package/dist/src/normalize-blurb.d.ts.map +0 -1
- package/dist/src/normalize-blurb.js +0 -21
- package/dist/src/normalize-blurb.js.map +0 -1
- package/dist/src/opencode/notify.d.ts +0 -10
- package/dist/src/opencode/notify.d.ts.map +0 -1
- package/dist/src/opencode/notify.js +0 -8
- package/dist/src/opencode/notify.js.map +0 -1
- package/dist/src/parse-results.d.ts +0 -2
- package/dist/src/parse-results.d.ts.map +0 -1
- package/dist/src/parse-results.js +0 -20
- package/dist/src/parse-results.js.map +0 -1
- package/dist/src/providers/brave-search.d.ts +0 -4
- package/dist/src/providers/brave-search.d.ts.map +0 -1
- package/dist/src/providers/brave-search.js +0 -29
- package/dist/src/providers/brave-search.js.map +0 -1
- package/dist/src/providers/bsky-search.d.ts +0 -31
- package/dist/src/providers/bsky-search.d.ts.map +0 -1
- package/dist/src/providers/bsky-search.js +0 -61
- package/dist/src/providers/bsky-search.js.map +0 -1
- package/dist/src/providers/ddg-search.d.ts +0 -7
- package/dist/src/providers/ddg-search.d.ts.map +0 -1
- package/dist/src/providers/ddg-search.js +0 -88
- package/dist/src/providers/ddg-search.js.map +0 -1
- package/dist/src/providers/gnews-search.d.ts +0 -9
- package/dist/src/providers/gnews-search.d.ts.map +0 -1
- package/dist/src/providers/gnews-search.js +0 -50
- package/dist/src/providers/gnews-search.js.map +0 -1
- package/dist/src/providers/mdn-search.d.ts +0 -22
- package/dist/src/providers/mdn-search.d.ts.map +0 -1
- package/dist/src/providers/mdn-search.js +0 -59
- package/dist/src/providers/mdn-search.js.map +0 -1
- package/dist/src/providers/rfc-search.d.ts +0 -9
- package/dist/src/providers/rfc-search.d.ts.map +0 -1
- package/dist/src/providers/rfc-search.js +0 -35
- package/dist/src/providers/rfc-search.js.map +0 -1
- package/dist/src/providers/w3c-search.d.ts +0 -6
- package/dist/src/providers/w3c-search.d.ts.map +0 -1
- package/dist/src/providers/w3c-search.js +0 -42
- package/dist/src/providers/w3c-search.js.map +0 -1
- package/dist/src/providers/whatwg-search.d.ts +0 -8
- package/dist/src/providers/whatwg-search.d.ts.map +0 -1
- package/dist/src/providers/whatwg-search.js +0 -31
- package/dist/src/providers/whatwg-search.js.map +0 -1
- package/dist/src/providers/wiki-search.d.ts +0 -25
- package/dist/src/providers/wiki-search.d.ts.map +0 -1
- package/dist/src/providers/wiki-search.js +0 -50
- package/dist/src/providers/wiki-search.js.map +0 -1
- package/dist/src/renderers.d.ts +0 -18
- package/dist/src/renderers.d.ts.map +0 -1
- package/dist/src/renderers.js +0 -43
- package/dist/src/renderers.js.map +0 -1
- package/dist/src/safe.d.ts +0 -13
- package/dist/src/safe.d.ts.map +0 -1
- package/dist/src/safe.js +0 -17
- package/dist/src/safe.js.map +0 -1
- package/dist/src/search.d.ts +0 -13
- package/dist/src/search.d.ts.map +0 -1
- package/dist/src/search.js +0 -304
- package/dist/src/search.js.map +0 -1
- package/dist/src/strategies.d.ts +0 -13
- package/dist/src/strategies.d.ts.map +0 -1
- package/dist/src/strategies.js +0 -48
- package/dist/src/strategies.js.map +0 -1
- package/dist/src/user-agent.d.ts +0 -2
- package/dist/src/user-agent.d.ts.map +0 -1
- package/dist/src/user-agent.js +0 -3
- package/dist/src/user-agent.js.map +0 -1
- package/dist/src/v1-message.d.ts +0 -2
- package/dist/src/v1-message.d.ts.map +0 -1
- package/dist/src/v1-message.js +0 -11
- package/dist/src/v1-message.js.map +0 -1
- package/search.ts +0 -22
- package/src/cache.test.ts +0 -14
- package/src/cache.ts +0 -61
- package/src/format.test.ts +0 -175
- package/src/format.ts +0 -72
- package/src/html.ts +0 -39
- package/src/normalize-blurb.test.ts +0 -67
- package/src/normalize-blurb.ts +0 -22
- package/src/opencode/notify.ts +0 -16
- package/src/parse-results.test.ts +0 -75
- package/src/parse-results.ts +0 -22
- package/src/providers/brave-search.ts +0 -39
- package/src/providers/bsky-search.test.ts +0 -16
- package/src/providers/bsky-search.ts +0 -88
- package/src/providers/ddg-search.test.ts +0 -15
- package/src/providers/ddg-search.ts +0 -117
- package/src/providers/gnews-search.ts +0 -72
- package/src/providers/mdn-search.test.ts +0 -17
- package/src/providers/mdn-search.ts +0 -83
- package/src/providers/rfc-search.test.ts +0 -22
- package/src/providers/rfc-search.ts +0 -57
- package/src/providers/w3c-search.test.ts +0 -22
- package/src/providers/w3c-search.ts +0 -67
- package/src/providers/whatwg-search.test.ts +0 -21
- package/src/providers/whatwg-search.ts +0 -55
- package/src/providers/wiki-search.test.ts +0 -16
- package/src/providers/wiki-search.ts +0 -72
- package/src/renderers.test.ts +0 -263
- package/src/renderers.ts +0 -62
- package/src/safe.test.ts +0 -87
- package/src/safe.ts +0 -27
- package/src/search.ts +0 -369
- package/src/strategies.test.ts +0 -92
- package/src/strategies.ts +0 -62
- package/src/user-agent.ts +0 -3
- package/src/v1-message.test.ts +0 -17
- package/src/v1-message.ts +0 -10
package/src/renderers.test.ts
DELETED
|
@@ -1,263 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test'
|
|
2
|
-
import {
|
|
3
|
-
renderBskyPost,
|
|
4
|
-
renderDdgResult,
|
|
5
|
-
renderGnewsItem,
|
|
6
|
-
renderMdnDoc,
|
|
7
|
-
renderWebResult,
|
|
8
|
-
renderWikiPage,
|
|
9
|
-
} from './renderers.ts'
|
|
10
|
-
|
|
11
|
-
describe('renderDdgResult', () => {
|
|
12
|
-
it('returns three lines: title, url, abstract', () => {
|
|
13
|
-
const result = { title: 'TypeScript', url: 'https://typescriptlang.org', abstract: 'A typed superset of JavaScript' }
|
|
14
|
-
const lines = renderDdgResult(result, 0).split('\n')
|
|
15
|
-
expect(lines).toHaveLength(3)
|
|
16
|
-
expect(lines[0]).toContain('1. TypeScript')
|
|
17
|
-
expect(lines[1]).toContain('https://typescriptlang.org')
|
|
18
|
-
expect(lines[2]).toContain('A typed superset of JavaScript')
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
it('uses 1-based index', () => {
|
|
22
|
-
const result = { title: 'Test', url: 'https://example.com', abstract: 'Abstract' }
|
|
23
|
-
const lines = renderDdgResult(result, 4).split('\n')
|
|
24
|
-
expect(lines[0]).toStartWith('5. ')
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
it('indents url and abstract lines', () => {
|
|
28
|
-
const result = { title: 'Test', url: 'https://example.com', abstract: 'Abstract' }
|
|
29
|
-
const lines = renderDdgResult(result, 0).split('\n')
|
|
30
|
-
expect(lines[1]).toStartWith(' ')
|
|
31
|
-
expect(lines[2]).toStartWith(' ')
|
|
32
|
-
})
|
|
33
|
-
})
|
|
34
|
-
|
|
35
|
-
describe('renderWebResult', () => {
|
|
36
|
-
it('returns three lines: title, url, abstract', () => {
|
|
37
|
-
const result = { title: 'TypeScript', url: 'https://typescriptlang.org', abstract: 'A typed superset of JavaScript' }
|
|
38
|
-
const lines = renderWebResult(result, 0).split('\n')
|
|
39
|
-
expect(lines).toHaveLength(3)
|
|
40
|
-
expect(lines[0]).toContain('1. TypeScript')
|
|
41
|
-
expect(lines[1]).toContain('https://typescriptlang.org')
|
|
42
|
-
expect(lines[2]).toContain('A typed superset of JavaScript')
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
it('uses 1-based index', () => {
|
|
46
|
-
const result = { title: 'Test', url: 'https://example.com', abstract: 'Abstract' }
|
|
47
|
-
const lines = renderWebResult(result, 4).split('\n')
|
|
48
|
-
expect(lines[0]).toStartWith('5. ')
|
|
49
|
-
})
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
describe('renderGnewsItem', () => {
|
|
53
|
-
const item = {
|
|
54
|
-
title: 'Rust 2.0 announced',
|
|
55
|
-
url: 'https://news.google.com/rss/articles/CBMiabc',
|
|
56
|
-
abstract: '',
|
|
57
|
-
source: 'The Register',
|
|
58
|
-
date: 'Fri, 18 Sep 2026 16:55:50 GMT',
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
it('returns three lines: title, source and date, url', () => {
|
|
62
|
-
const lines = renderGnewsItem(item, 0).split('\n')
|
|
63
|
-
|
|
64
|
-
expect(lines).toHaveLength(3)
|
|
65
|
-
expect(lines[0]).toContain('1. Rust 2.0 announced')
|
|
66
|
-
expect(lines[1]).toContain('The Register')
|
|
67
|
-
expect(lines[2]).toContain('news.google.com')
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
it('omits the separator when there is no date', () => {
|
|
71
|
-
const lines = renderGnewsItem({ ...item, date: '' }, 0).split('\n')
|
|
72
|
-
|
|
73
|
-
expect(lines[1]).not.toContain('—')
|
|
74
|
-
})
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
describe('renderBskyPost', () => {
|
|
78
|
-
const post = {
|
|
79
|
-
uri: 'at://did:plc:test/app.bsky.feed.post/abc',
|
|
80
|
-
cid: 'cid123',
|
|
81
|
-
author: {
|
|
82
|
-
did: 'did:plc:test',
|
|
83
|
-
handle: 'alice.bsky.social',
|
|
84
|
-
displayName: 'Alice',
|
|
85
|
-
},
|
|
86
|
-
record: {
|
|
87
|
-
text: 'Hello world from Bluesky!',
|
|
88
|
-
createdAt: '2025-06-15T14:30:00.000Z',
|
|
89
|
-
},
|
|
90
|
-
likeCount: 42,
|
|
91
|
-
repostCount: 7,
|
|
92
|
-
replyCount: 3,
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
it('returns three lines', () => {
|
|
96
|
-
const lines = renderBskyPost(post, 0).split('\n')
|
|
97
|
-
expect(lines).toHaveLength(3)
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
it('includes index and author handle on first line', () => {
|
|
101
|
-
const lines = renderBskyPost(post, 0).split('\n')
|
|
102
|
-
expect(lines[0]).toContain('1. @alice.bsky.social')
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
it('uses 1-based index', () => {
|
|
106
|
-
const lines = renderBskyPost(post, 4).split('\n')
|
|
107
|
-
expect(lines[0]).toStartWith('5. @')
|
|
108
|
-
})
|
|
109
|
-
|
|
110
|
-
it('includes formatted date with time on first line', () => {
|
|
111
|
-
const lines = renderBskyPost(post, 0).split('\n')
|
|
112
|
-
expect(lines[0]).toContain('Jun')
|
|
113
|
-
expect(lines[0]).toContain('2025')
|
|
114
|
-
expect(lines[0]).toMatch(/AM|PM/)
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
it('includes post text on second line', () => {
|
|
118
|
-
const lines = renderBskyPost(post, 0).split('\n')
|
|
119
|
-
expect(lines[1]).toContain('Hello world from Bluesky!')
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
it('includes engagement counts on third line', () => {
|
|
123
|
-
const lines = renderBskyPost(post, 0).split('\n')
|
|
124
|
-
expect(lines[2]).toContain('Likes: 42')
|
|
125
|
-
expect(lines[2]).toContain('Reposts: 7')
|
|
126
|
-
expect(lines[2]).toContain('Replies: 3')
|
|
127
|
-
})
|
|
128
|
-
|
|
129
|
-
it('indents second and third lines', () => {
|
|
130
|
-
const lines = renderBskyPost(post, 0).split('\n')
|
|
131
|
-
expect(lines[1]).toStartWith(' ')
|
|
132
|
-
expect(lines[2]).toStartWith(' ')
|
|
133
|
-
})
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
describe('renderWikiPage', () => {
|
|
137
|
-
it('includes title and wikipedia url', () => {
|
|
138
|
-
const result = renderWikiPage({
|
|
139
|
-
id: 1,
|
|
140
|
-
key: 'TypeScript',
|
|
141
|
-
title: 'TypeScript',
|
|
142
|
-
excerpt: 'A programming language',
|
|
143
|
-
description: 'Open-source language',
|
|
144
|
-
matched_title: null,
|
|
145
|
-
thumbnail: null,
|
|
146
|
-
}, 0)
|
|
147
|
-
expect(result).toContain('1. TypeScript')
|
|
148
|
-
expect(result).toContain('https://en.wikipedia.org/wiki/TypeScript')
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
it('includes description when present', () => {
|
|
152
|
-
const result = renderWikiPage({
|
|
153
|
-
id: 1,
|
|
154
|
-
key: 'TypeScript',
|
|
155
|
-
title: 'TypeScript',
|
|
156
|
-
excerpt: 'A programming language',
|
|
157
|
-
description: 'Open-source language',
|
|
158
|
-
matched_title: null,
|
|
159
|
-
thumbnail: null,
|
|
160
|
-
}, 0)
|
|
161
|
-
expect(result).toContain('Open-source language')
|
|
162
|
-
})
|
|
163
|
-
|
|
164
|
-
it('omits description when null', () => {
|
|
165
|
-
const result = renderWikiPage({
|
|
166
|
-
id: 2,
|
|
167
|
-
key: 'JavaScript',
|
|
168
|
-
title: 'JavaScript',
|
|
169
|
-
excerpt: 'A scripting language',
|
|
170
|
-
description: null,
|
|
171
|
-
matched_title: null,
|
|
172
|
-
thumbnail: null,
|
|
173
|
-
}, 0)
|
|
174
|
-
const lines = result.split('\n')
|
|
175
|
-
expect(lines).toHaveLength(3)
|
|
176
|
-
expect(lines[0]).toContain('1. JavaScript')
|
|
177
|
-
expect(lines[1]).toContain('https://en.wikipedia.org/wiki/JavaScript')
|
|
178
|
-
expect(lines[2]).toContain('A scripting language')
|
|
179
|
-
})
|
|
180
|
-
|
|
181
|
-
it('returns four lines when description is present', () => {
|
|
182
|
-
const lines = renderWikiPage({
|
|
183
|
-
id: 1,
|
|
184
|
-
key: 'TypeScript',
|
|
185
|
-
title: 'TypeScript',
|
|
186
|
-
excerpt: 'A programming language',
|
|
187
|
-
description: 'Open-source language',
|
|
188
|
-
matched_title: null,
|
|
189
|
-
thumbnail: null,
|
|
190
|
-
}, 0).split('\n')
|
|
191
|
-
expect(lines).toHaveLength(4)
|
|
192
|
-
})
|
|
193
|
-
|
|
194
|
-
it('uses 1-based index', () => {
|
|
195
|
-
const lines = renderWikiPage({
|
|
196
|
-
id: 1,
|
|
197
|
-
key: 'Test',
|
|
198
|
-
title: 'Test',
|
|
199
|
-
excerpt: 'Excerpt',
|
|
200
|
-
description: null,
|
|
201
|
-
matched_title: null,
|
|
202
|
-
thumbnail: null,
|
|
203
|
-
}, 2).split('\n')
|
|
204
|
-
expect(lines[0]).toStartWith('3. ')
|
|
205
|
-
})
|
|
206
|
-
|
|
207
|
-
it('indents all lines after the title', () => {
|
|
208
|
-
const lines = renderWikiPage({
|
|
209
|
-
id: 1,
|
|
210
|
-
key: 'Test',
|
|
211
|
-
title: 'Test',
|
|
212
|
-
excerpt: 'Excerpt',
|
|
213
|
-
description: 'Desc',
|
|
214
|
-
matched_title: null,
|
|
215
|
-
thumbnail: null,
|
|
216
|
-
}, 0).split('\n')
|
|
217
|
-
for (let i = 1; i < lines.length; i++) {
|
|
218
|
-
expect(lines[i]).toStartWith(' ')
|
|
219
|
-
}
|
|
220
|
-
})
|
|
221
|
-
})
|
|
222
|
-
|
|
223
|
-
describe('renderMdnDoc', () => {
|
|
224
|
-
const doc = {
|
|
225
|
-
mdn_url: '/en-US/docs/Web/API/Fetch_API',
|
|
226
|
-
title: 'Fetch API',
|
|
227
|
-
summary: 'The Fetch API provides an interface for fetching resources.',
|
|
228
|
-
slug: 'Web/API/Fetch_API',
|
|
229
|
-
locale: 'en-US',
|
|
230
|
-
popularity: 0.5,
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
it('returns three lines: title, url, summary', () => {
|
|
234
|
-
const lines = renderMdnDoc(doc, 0).split('\n')
|
|
235
|
-
expect(lines).toHaveLength(3)
|
|
236
|
-
})
|
|
237
|
-
|
|
238
|
-
it('includes index and title on first line', () => {
|
|
239
|
-
const lines = renderMdnDoc(doc, 0).split('\n')
|
|
240
|
-
expect(lines[0]).toBe('1. Fetch API')
|
|
241
|
-
})
|
|
242
|
-
|
|
243
|
-
it('uses 1-based index', () => {
|
|
244
|
-
const lines = renderMdnDoc(doc, 4).split('\n')
|
|
245
|
-
expect(lines[0]).toStartWith('5. ')
|
|
246
|
-
})
|
|
247
|
-
|
|
248
|
-
it('includes full MDN url on second line', () => {
|
|
249
|
-
const lines = renderMdnDoc(doc, 0).split('\n')
|
|
250
|
-
expect(lines[1]).toContain('https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API')
|
|
251
|
-
})
|
|
252
|
-
|
|
253
|
-
it('includes summary on third line', () => {
|
|
254
|
-
const lines = renderMdnDoc(doc, 0).split('\n')
|
|
255
|
-
expect(lines[2]).toContain('The Fetch API provides an interface for fetching resources.')
|
|
256
|
-
})
|
|
257
|
-
|
|
258
|
-
it('indents url and summary lines', () => {
|
|
259
|
-
const lines = renderMdnDoc(doc, 0).split('\n')
|
|
260
|
-
expect(lines[1]).toStartWith(' ')
|
|
261
|
-
expect(lines[2]).toStartWith(' ')
|
|
262
|
-
})
|
|
263
|
-
})
|
package/src/renderers.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { formatDate, formatDateTime } from './format.ts'
|
|
2
|
-
import { normalizeBlurb } from './normalize-blurb.ts'
|
|
3
|
-
import type { BskyPost } from './providers/bsky-search.ts'
|
|
4
|
-
import type { DdgResult } from './providers/ddg-search.ts'
|
|
5
|
-
import type { GnewsResult } from './providers/gnews-search.ts'
|
|
6
|
-
import type { MdnDocument } from './providers/mdn-search.ts'
|
|
7
|
-
import type { RfcResult } from './providers/rfc-search.ts'
|
|
8
|
-
import type { W3cResult } from './providers/w3c-search.ts'
|
|
9
|
-
import type { WhatwgResult } from './providers/whatwg-search.ts'
|
|
10
|
-
import type { WikiPage } from './providers/wiki-search.ts'
|
|
11
|
-
|
|
12
|
-
export const renderDdgResult = (result: DdgResult, index: number): string => {
|
|
13
|
-
return `${index + 1}. ${result.title}
|
|
14
|
-
${result.url}
|
|
15
|
-
${normalizeBlurb(result.abstract)}`
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// Brave and DuckDuckGo return the same title/url/abstract shape.
|
|
19
|
-
export const renderWebResult = renderDdgResult
|
|
20
|
-
|
|
21
|
-
export const renderGnewsItem = (result: GnewsResult, index: number): string => {
|
|
22
|
-
return `${index + 1}. ${result.title}
|
|
23
|
-
${result.source}${result.date ? ' — ' + formatDate(result.date) : ''}
|
|
24
|
-
${result.url}`
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export const renderBskyPost = (post: BskyPost, index: number): string => (
|
|
28
|
-
`${index + 1}. @${post.author.handle} (${formatDateTime(post.record.createdAt)})
|
|
29
|
-
${normalizeBlurb(post.record.text)}
|
|
30
|
-
Likes: ${post.likeCount} Reposts: ${post.repostCount} Replies: ${post.replyCount}`
|
|
31
|
-
)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
export const renderWikiPage = (page: WikiPage, index: number): string => {
|
|
35
|
-
const desc = page.description ? `\n ${normalizeBlurb(page.description)}` : ''
|
|
36
|
-
return `${index + 1}. ${page.title}
|
|
37
|
-
https://en.wikipedia.org/wiki/${page.key}${desc}
|
|
38
|
-
${normalizeBlurb(page.excerpt)}`
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export const renderMdnDoc = (doc: MdnDocument, index: number): string => {
|
|
42
|
-
return `${index + 1}. ${doc.title}
|
|
43
|
-
https://developer.mozilla.org${doc.mdn_url}
|
|
44
|
-
${normalizeBlurb(doc.summary)}`
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export const renderRfc = (result: RfcResult, index: number): string => {
|
|
48
|
-
return `${index + 1}. ${result.id} — ${result.title}
|
|
49
|
-
${result.url}
|
|
50
|
-
${normalizeBlurb(result.abstract)}`
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export const renderW3c = (result: W3cResult, index: number): string => {
|
|
54
|
-
return `${index + 1}. ${result.title}
|
|
55
|
-
${result.url}`
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export const renderWhatwg = (result: WhatwgResult, index: number): string => {
|
|
59
|
-
return `${index + 1}. ${result.title}
|
|
60
|
-
${result.url}
|
|
61
|
-
${normalizeBlurb(result.abstract)}`
|
|
62
|
-
}
|
package/src/safe.test.ts
DELETED
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test'
|
|
2
|
-
import { safe, safeAsync } from './safe.ts'
|
|
3
|
-
|
|
4
|
-
describe('safe', () => {
|
|
5
|
-
it('returns data on success', () => {
|
|
6
|
-
const result = safe(() => 42)
|
|
7
|
-
|
|
8
|
-
expect(result.data).toEqual(42)
|
|
9
|
-
expect(result.error).toEqual(null)
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
it('returns data for string values', () => {
|
|
13
|
-
const result = safe(() => 'hello')
|
|
14
|
-
|
|
15
|
-
expect(result.data).toEqual('hello')
|
|
16
|
-
expect(result.error).toEqual(null)
|
|
17
|
-
})
|
|
18
|
-
|
|
19
|
-
it('returns error when function throws an Error', () => {
|
|
20
|
-
const result = safe(() => {
|
|
21
|
-
throw new Error('boom')
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
expect(result.data).toEqual(null)
|
|
25
|
-
expect(result.error).toBeInstanceOf(Error)
|
|
26
|
-
expect(result.error?.message).toEqual('boom')
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
it('wraps non-Error throws in an Error', () => {
|
|
30
|
-
const result = safe(() => {
|
|
31
|
-
throw 'string error'
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
expect(result.data).toEqual(null)
|
|
35
|
-
expect(result.error).toBeInstanceOf(Error)
|
|
36
|
-
expect(result.error?.message).toEqual('string error')
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
it('returns null data as success', () => {
|
|
40
|
-
const result = safe(() => null)
|
|
41
|
-
|
|
42
|
-
expect(result.data).toEqual(null)
|
|
43
|
-
expect(result.error).toEqual(null)
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
it('returns undefined data as success', () => {
|
|
47
|
-
const result = safe(() => undefined)
|
|
48
|
-
|
|
49
|
-
expect(result.data).toEqual(undefined)
|
|
50
|
-
expect(result.error).toEqual(null)
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
describe('safeAsync', () => {
|
|
55
|
-
it('returns data on success', async () => {
|
|
56
|
-
const result = await safeAsync(() => Promise.resolve(42))
|
|
57
|
-
|
|
58
|
-
expect(result.data).toEqual(42)
|
|
59
|
-
expect(result.error).toEqual(null)
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
it('returns error when promise rejects with Error', async () => {
|
|
63
|
-
const result = await safeAsync(() => Promise.reject(new Error('async boom')))
|
|
64
|
-
|
|
65
|
-
expect(result.data).toEqual(null)
|
|
66
|
-
expect(result.error).toBeInstanceOf(Error)
|
|
67
|
-
expect(result.error?.message).toEqual('async boom')
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
it('wraps non-Error rejections in an Error', async () => {
|
|
71
|
-
const result = await safeAsync(() => Promise.reject('string rejection'))
|
|
72
|
-
|
|
73
|
-
expect(result.data).toEqual(null)
|
|
74
|
-
expect(result.error).toBeInstanceOf(Error)
|
|
75
|
-
expect(result.error?.message).toEqual('string rejection')
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
it('returns error when async function throws', async () => {
|
|
79
|
-
const result = await safeAsync(async () => {
|
|
80
|
-
throw new Error('thrown in async')
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
expect(result.data).toEqual(null)
|
|
84
|
-
expect(result.error).toBeInstanceOf(Error)
|
|
85
|
-
expect(result.error?.message).toEqual('thrown in async')
|
|
86
|
-
})
|
|
87
|
-
})
|
package/src/safe.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
type SafeSuccess<T> = {
|
|
2
|
-
data: T
|
|
3
|
-
error: null
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
type SafeError = {
|
|
7
|
-
data: null
|
|
8
|
-
error: Error
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export type SafeResult<T> = SafeSuccess<T> | SafeError
|
|
12
|
-
|
|
13
|
-
export const safe = <T>(fn: () => T): SafeResult<T> => {
|
|
14
|
-
try {
|
|
15
|
-
return { data: fn(), error: null }
|
|
16
|
-
} catch (error) {
|
|
17
|
-
return { data: null, error: error instanceof Error ? error : new Error(String(error)) }
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export const safeAsync = async <T>(fn: () => Promise<T>): Promise<SafeResult<T>> => {
|
|
22
|
-
try {
|
|
23
|
-
return { data: await fn(), error: null }
|
|
24
|
-
} catch (error) {
|
|
25
|
-
return { data: null, error: error instanceof Error ? error : new Error(String(error)) }
|
|
26
|
-
}
|
|
27
|
-
}
|