MCPcopy Create free account
hub / github.com/chainjet/platform / extractFirstItem

Function extractFirstItem

apps/runner/src/utils/trigger.utils.ts:36–59  ·  view source on GitHub ↗
(
  idKey: string,
  data: Record<string, unknown>,
)

Source from the content-addressed store, hash-verified

34}
35
36export function extractFirstItem(
37 idKey: string,
38 data: Record<string, unknown>,
39): { id: string | number; item: Record<string, unknown> } | null {
40 const objectKeys = idKey.replace(/\[]\./g, '.0.').split('.')
41
42 let iterationData: any = data
43 let item: Record<string, any> = {}
44 for (const key of objectKeys) {
45 if (!iterationData[key]) {
46 return null
47 }
48 if (key.toString() === '0') {
49 iterationData = sortByCreationDate(iterationData)
50 }
51 item = iterationData
52 iterationData = iterationData[key]
53 }
54
55 if (typeof iterationData === 'string' || typeof iterationData === 'number') {
56 return { id: iterationData, item }
57 }
58 return null
59}
60
61/**
62 * Tries to sort a generic array of items by creation time

Callers 2

extractTriggerItemsFunction · 0.85

Calls 1

sortByCreationDateFunction · 0.85

Tested by

no test coverage detected