(list: ClassItem[])
| 79 | } |
| 80 | |
| 81 | function sortFractionsLast(list: ClassItem[]) { |
| 82 | type Bucket = { |
| 83 | utility: string |
| 84 | items: ClassItem[] |
| 85 | } |
| 86 | |
| 87 | // 1. Create "buckets" for each utility group |
| 88 | let buckets: Bucket[] = [] |
| 89 | let current: Bucket | null = null |
| 90 | |
| 91 | // 2. Determine the last bucket for each utility group |
| 92 | let lastUtilityBucket = new Map<string, Bucket>() |
| 93 | |
| 94 | // 3. Collect all fractions in a given utility group |
| 95 | let fractions = new DefaultMap<string, ClassItem[]>(() => []) |
| 96 | |
| 97 | for (let item of list) { |
| 98 | let { utility, fraction } = item |
| 99 | |
| 100 | if (!current) { |
| 101 | current = { utility, items: [] } |
| 102 | lastUtilityBucket.set(utility, current) |
| 103 | } |
| 104 | |
| 105 | if (utility !== current.utility) { |
| 106 | buckets.push(current) |
| 107 | |
| 108 | current = { utility, items: [] } |
| 109 | lastUtilityBucket.set(utility, current) |
| 110 | } |
| 111 | |
| 112 | if (fraction) { |
| 113 | fractions.get(utility).push(item) |
| 114 | } else { |
| 115 | current.items.push(item) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (current && buckets[buckets.length - 1] !== current) { |
| 120 | buckets.push(current) |
| 121 | } |
| 122 | |
| 123 | // 4. Add fractions to their respective last utility buckets |
| 124 | for (let [utility, items] of fractions) { |
| 125 | let bucket = lastUtilityBucket.get(utility) |
| 126 | if (!bucket) continue |
| 127 | |
| 128 | bucket.items.push(...items) |
| 129 | } |
| 130 | |
| 131 | // 5. Flatten the buckets into a single list |
| 132 | let entries: ClassEntry[] = [] |
| 133 | |
| 134 | for (let bucket of buckets) { |
| 135 | for (let entry of bucket.items) { |
| 136 | entries.push([entry.name, { modifiers: entry.modifiers }]) |
| 137 | } |
| 138 | } |
no test coverage detected