(array: T[] | undefined, visitor: (item: T) => T)
| 26 | * @param visitor |
| 27 | */ |
| 28 | export function lazyMap<T>(array: T[] | undefined, visitor: (item: T) => T): T[] { |
| 29 | if (!array) { |
| 30 | return []; |
| 31 | } |
| 32 | |
| 33 | let outputArray = array; |
| 34 | |
| 35 | let index = 0; |
| 36 | for (const item of array) { |
| 37 | const newItem = visitor(item); |
| 38 | |
| 39 | if (newItem !== item) { |
| 40 | if (outputArray === array) { |
| 41 | outputArray = array.slice(0, array.length); |
| 42 | } |
| 43 | |
| 44 | outputArray[index] = newItem; |
| 45 | } |
| 46 | |
| 47 | index++; |
| 48 | } |
| 49 | |
| 50 | return outputArray; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Search an exact match item in a sorted array. |
no test coverage detected