(collection: Collection<Node<T>>)
| 16 | const cache = new WeakMap<Iterable<unknown>, number>(); |
| 17 | |
| 18 | export function getItemCount<T>(collection: Collection<Node<T>>): number { |
| 19 | let count = cache.get(collection); |
| 20 | if (count != null) { |
| 21 | return count; |
| 22 | } |
| 23 | |
| 24 | // TS isn't smart enough to know we've ensured count is a number, so use a new variable |
| 25 | let counter = 0; |
| 26 | let countItems = (items: Iterable<Node<T>>) => { |
| 27 | for (let item of items) { |
| 28 | if (item.type === 'section') { |
| 29 | countItems(getChildNodes(item, collection)); |
| 30 | } else if (item.type === 'item') { |
| 31 | counter++; |
| 32 | } |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | countItems(collection); |
| 37 | cache.set(collection, counter); |
| 38 | return counter; |
| 39 | } |
no test coverage detected