MCPcopy
hub / github.com/arktypeio/arktype / flatMorph

Function flatMorph

ark/util/flatMorph.ts:79–109  ·  view source on GitHub ↗
(
	o: object,
	flatMapEntry: (...args: any[]) => listable<GroupableEntry>
)

Source from the content-addressed store, hash-verified

77}
78
79export const flatMorph: FlatMorph = (
80 o: object,
81 flatMapEntry: (...args: any[]) => listable<GroupableEntry>
82): any => {
83 const result: any = {}
84 const inputIsArray = Array.isArray(o)
85 let outputShouldBeArray = false
86
87 for (const [i, entry] of Object.entries(o).entries()) {
88 const mapped =
89 inputIsArray ? flatMapEntry(i, entry[1]) : flatMapEntry(...entry, i)
90
91 outputShouldBeArray ||= typeof mapped[0] === "number"
92
93 const flattenedEntries =
94 Array.isArray(mapped[0]) || mapped.length === 0 ?
95 // if we have an empty array (for filtering) or an array with
96 // another array as its first element, treat it as a list
97 (mapped as GroupableEntry[])
98 // otherwise, it should be a single entry, so nest it in a tuple
99 // so it doesn't get spread when the result is flattened
100 : [mapped as GroupableEntry]
101
102 for (const [k, v] of flattenedEntries) {
103 if (typeof k === "object") result[k.group] = append(result[k.group], v)
104 else result[k] = v
105 }
106 }
107
108 return outputShouldBeArray ? Object.values(result) : result
109}

Callers 15

getCompletionsFunction · 0.90
shared.tsFile · 0.90
ambientAttachmentsMethod · 0.90
string.tsFile · 0.90
constructors.tsFile · 0.90
type.test.tsFile · 0.90
testAllConfig.tsFile · 0.90
KeywordTable.tsxFile · 0.90
updateSnippetsEntrypointFunction · 0.90
bindModuleFunction · 0.90
constructorMethod · 0.90

Calls 1

appendFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…