MCPcopy
hub / github.com/ocsjs/ocsjs / simplifyWorkResult

Function simplifyWorkResult

packages/scripts/src/utils/work.ts:220–257  ·  view source on GitHub ↗
(
	results: WorkResult<any>[],
	/**
	 * 标题处理方法
	 * 在答题时使用相同的处理方法,可以使答题结果显示的题目与搜题的题目保持一致
	 */
	titleTransform?: (title: (HTMLElement | undefined)[], index: number) => string
)

Source from the content-addressed store, hash-verified

218
219/** 将 {@link WorkResult} 转换成 {@link SimplifyWorkResult} */
220export function simplifyWorkResult(
221 results: WorkResult<any>[],
222 /**
223 * 标题处理方法
224 * 在答题时使用相同的处理方法,可以使答题结果显示的题目与搜题的题目保持一致
225 */
226 titleTransform?: (title: (HTMLElement | undefined)[], index: number) => string
227): SimplifyWorkResult[] {
228 const res: SimplifyWorkResult[] = [];
229 let i = 0;
230 for (const wr of results) {
231 const ques =
232 titleTransform?.(wr.ctx?.elements.title || [], i) ||
233 wr.ctx?.elements.title
234 ?.map((e) => e?.innerText.trim())
235 .filter(Boolean)
236 .join('<br>') ||
237 '';
238 res.push({
239 requested: wr.requested,
240 resolved: wr.resolved,
241 error: wr.error,
242 type: wr.ctx?.type,
243 question: ques,
244 finish: wr.result?.finish,
245 searchInfos:
246 wr.ctx?.searchInfos.map((sr) => ({
247 error: sr.error,
248 name: sr.name,
249 homepage: sr.homepage,
250 results: sr.results.map((ans) => [ans.question, ans.answer, ans.extra_data || {}])
251 })) || []
252 });
253 i++;
254 }
255
256 return res;
257}
258
259/**
260 * 从题目中移除指定的冗余词

Callers 8

onResultsUpdateFunction · 0.90
onResultsUpdateFunction · 0.90
onResultsUpdateFunction · 0.90
onResultsUpdateFunction · 0.90
onResultsUpdateFunction · 0.90
updateFunction · 0.90
singleQuestionHandleFunction · 0.90
onResultsUpdateFunction · 0.90

Calls 1

titleTransformFunction · 0.50

Tested by

no test coverage detected