All files / src/shared/lib/helpers tiles.ts

100% Statements 19/19
100% Branches 4/4
100% Functions 1/1
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3331x             18x 18x   18x 1x     17x   17x 17x   17x 90x 1501x 1501x 1501x 1501x 20x         17x    
export const resizeTiles = (
  tiles: number[],
  initialWidth: number,
  initialHeight: number,
  outputWidth: number,
  outputHeight: number,
): number[] => {
  const newWidth = Math.max(0, outputWidth);
  const newHeight = Math.max(0, outputHeight);
 
  if (newWidth === initialWidth && newHeight === initialHeight) {
    return tiles;
  }
 
  const outputTiles: number[] = new Array(newWidth * newHeight).fill(0);
 
  const rowsToCopy = Math.min(initialHeight, newHeight);
  const colsToCopy = Math.min(initialWidth, newWidth);
 
  for (let row = 0; row < rowsToCopy; row++) {
    for (let col = 0; col < colsToCopy; col++) {
      const oldIndex = row * initialWidth + col;
      const newIndex = row * newWidth + col;
      const oldValue = tiles[oldIndex];
      if (oldValue !== undefined) {
        outputTiles[newIndex] = oldValue;
      }
    }
  }
 
  return outputTiles;
};