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AACH,SAAgB,WAAW,CAAI,MAAW,EAAE,MAAW;IACrD,IAAI,MAAM,CAAC,MAAM,KAAK,MAAM,CAAC,MAAM,EAAE;QACnC,OAAO,KAAK,CAAC;KACd;IACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACtC,IAAI,MAAM,CAAC,CAAC,CAAC,KAAK,MAAM,CAAC,CAAC,CAAC,EAAE;YAC3B,OAAO,KAAK,CAAC;SACd;KACF;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAVD,kCAUC","sourcesContent":["/**\n * Helper to find the index of an item within an array, using a callback to\n * determine the match.\n *\n * @public\n * @param array - Array to search.\n * @param cb - Callback which returns true on matches.\n * @param fromIndex - Optional index to start from (defaults to 0)\n */\nexport function findIndex<T>(array: T[], cb: (item: T, index: number) => boolean, fromIndex: number = 0): number {\n let index = -1;\n\n for (let i = fromIndex; array && i < array.length; i++) {\n if (cb(array[i], i)) {\n index = i;\n break;\n }\n }\n\n return index;\n}\n\n/**\n * Helper to find the first item within an array that satisfies the callback.\n * @param array - Array to search\n * @param cb - Callback which returns true on matches\n */\nexport function find<T>(array: T[], cb: (item: T, index: number) => boolean): T | undefined {\n let index = findIndex(array, cb);\n\n if (index < 0) {\n return undefined;\n }\n\n return array[index];\n}\n\n/**\n * Creates an array of a given size and helper method to populate.\n *\n * @public\n * @param size - Size of array.\n * @param getItem - Callback to populate given cell index.\n */\nexport function createArray<T>(size: number, getItem: (index: number) => T): T[] {\n let array: T[] = [];\n\n for (let i = 0; i < size; i++) {\n array.push(getItem(i));\n }\n\n return array;\n}\n\n/**\n * Convert the given array to a matrix with columnCount number\n * of columns.\n *\n * @public\n * @param items - The array to convert\n * @param columnCount - The number of columns for the resulting matrix\n * @returns A matrix of items\n */\nexport function toMatrix<T>(items: T[], columnCount: number): T[][] {\n return items.reduce((rows: T[][], currentValue: T, index: number) => {\n if (index % columnCount === 0) {\n rows.push([currentValue]);\n } else {\n rows[rows.length - 1].push(currentValue);\n }\n return rows;\n }, [] as T[][]);\n}\n\n/**\n * Given an array, it returns a new array that does not contain the item at the given index.\n * @param array - The array to operate on\n * @param index - The index of the element to remove\n */\nexport function removeIndex<T>(array: T[], index: number): T[] {\n return array.filter((_: T, i: number) => index !== i);\n}\n\n/**\n * Given an array, this function returns a new array where the element at a given index has been replaced.\n * @param array - The array to operate on\n * @param newElement - The element that will be placed in the new array\n * @param index - The index of the element that should be replaced\n */\nexport function replaceElement<T>(array: T[], newElement: T, index: number): T[] {\n const copy = array.slice();\n copy[index] = newElement;\n return copy;\n}\n\n/**\n * Given an array, this function returns a new array where an element has been inserted at the given index.\n * @param array - The array to operate on\n * @param index - The index where an element should be inserted\n * @param itemToAdd - The element to insert\n */\nexport function addElementAtIndex<T>(array: T[], index: number, itemToAdd: T): T[] {\n const copy = array.slice();\n copy.splice(index, 0, itemToAdd);\n return copy;\n}\n\n/**\n * Given an array where each element is of type T or T[], flatten it into an array of T\n * @param array - The array where each element can optionally also be an array\n */\nexport function flatten<T>(array: (T | T[])[]): T[] {\n let result: T[] = [];\n array.forEach((item: T | T[]): T[] => (result = result.concat(item)));\n return result;\n}\n\n/**\n * Returns a boolean indicating if the two given arrays are equal in length and values.\n *\n * @param array1 - First array to compare\n * @param array2 - Second array to compare\n * @returns True if the arrays are the same length and have the same values in the same positions, false otherwise.\n */\nexport function arraysEqual<T>(array1: T[], array2: T[]): boolean {\n if (array1.length !== array2.length) {\n return false;\n }\n for (let i = 0; i < array1.length; i++) {\n if (array1[i] !== array2[i]) {\n return false;\n }\n }\n return true;\n}\n"]}
\No newline at end of file