<commit_before>import { ContainerLayout, Position, Dimensions } from '../types/interfaces';
import { calculateColumnsForSameWidthLayouts } from './calculateColumnsForSameWidthLayouts';

/**
 * Same size layout for items that have the same width/height
 */
export default (
  containerWidth: number,
  itemsDimensions: Dimensions[],
  gutterPixels: number
): ContainerLayout => {
  let cols = calculateColumnsForSameWidthLayouts(
    containerWidth,
    itemsDimensions[0].width,
    gutterPixels
  );
  let row = 0;

  const itemsPositions = itemsDimensions.map(
    ({ width, height }, index): Position => {
      // update current row
      if (index % cols === 0 && index >= cols) row++;
      // determine pos in grid
      const spot = index - cols * row;
      // return object with new position in array
      return {
        left: spot * (width + gutterPixels),
        top: row * (height + gutterPixels),
      };
    }
  );

  // These help calculate the final container height
  const totalRows = row + 1;
  const firstItemHeight = (itemsDimensions[0].height || 0) + gutterPixels;
  const containerHeight = totalRows * firstItemHeight + gutterPixels;

  return {
    containerHeight,
    itemsPositions,
  };
};<commit_msg>Simplify calculation of same size layouts<commit_after>import { ContainerLayout, Position, Dimensions } from '../types/interfaces';
import { calculateColumnsForSameWidthLayouts } from './calculateColumnsForSameWidthLayouts';

/**
 * Same size layout for items that have the same width/height
 */
export default (
  containerWidth: number,
  itemsDimensions: Dimensions[],
  gutterPixels: number
): ContainerLayout => {
  const columns = calculateColumnsForSameWidthLayouts(
    containerWidth,
    itemsDimensions[0].width,
    gutterPixels
  );

  const itemsPositions = itemsDimensions.map(
    ({ width, height }, index): Position => {
      const row = Math.floor(index / columns);
      const col = index - columns * row;
      return {
        left: col * (width + gutterPixels),
        top: row * (height + gutterPixels),
      };
    }
  );

  const totalRows = Math.floor(itemsDimensions.length / columns) + 1;
  const firstItemHeight = itemsDimensions[0].height + gutterPixels;
  const containerHeight = totalRows * firstItemHeight + gutterPixels;

  return {
    containerHeight,
    itemsPositions,
  };
};
{'fraction_non_alphanumeric': 0.10535259133389975, 'fraction_numerical': 0.0038232795242141037, 'mean_word_length': 3.4183864915572233, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5826901', 'n_tokens_mistral': 672, 'n_tokens_neox': 659, 'n_words': 186}