MCPcopy Create free account
hub / github.com/Snapchat/Valdi / lazyMap

Function lazyMap

src/valdi_modules/src/valdi/coreutils/src/ArrayUtils.ts:28–51  ·  view source on GitHub ↗
(array: T[] | undefined, visitor: (item: T) => T)

Source from the content-addressed store, hash-verified

26 * @param visitor
27 */
28export 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.

Callers 1

preprocessRepeatedFunction · 0.90

Calls 1

visitorFunction · 0.50

Tested by

no test coverage detected