( o: object, flatMapEntry: (...args: any[]) => listable<GroupableEntry> )
| 77 | } |
| 78 | |
| 79 | export 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 | } |
no test coverage detected
searching dependent graphs…