function sortScreenshotsByCategories (images) {
  const clone = {...images}

  // Abort early if no screenshots.
  const screenshots = clone.screenshots
  if (!screenshots) return clone

  // We put category-less screenshots first. This is important.
  const result = []
  const withCategories = {}

  for (const i of screenshots) {
    // We get the category from the dirname.
    const category = path.dirname(i)

    // Separate the screenshots which are at the root of the images folder.
    // They must be first, so we store them in another array.
    if (category === '.') {
      result.push(i)
    } else {
      // Create a new category first.
      if (!withCategories[category]) {
        withCategories[category] = {}
        withCategories[category].title = category
        withCategories[category].elements = []
      }

      withCategories[category].elements.push(i)
    }
  }

  clone.screenshots = result
  clone.screenshotsWithCategory = withCategories
  return clone
}
function sortScreenshotsByCategories (images) {
  const clone = {...images}

  
  const screenshots = clone.screenshots
  if (!screenshots) return clone

  
  const result = []
  const withCategories = {}

  for (const i of screenshots) {
    
    const category = path.dirname(i)

    
    
    if (category === '.') {
      result.push(i)
    } else {
      
      if (!withCategories[category]) {
        withCategories[category] = {}
        withCategories[category].title = category
        withCategories[category].elements = []
      }

      withCategories[category].elements.push(i)
    }
  }

  clone.screenshots = result
  clone.screenshotsWithCategory = withCategories
  return clone
}
function sortScreenshotsByCategories ( images ) { const clone = { ... images } // Abort early if no screenshots. const screenshots = clone . screenshots if ( ! screenshots ) return clone // We put category-less screenshots first. This is important. const result = [ ] const withCategories = { } for ( const i of screenshots ) { // We get the category from the dirname. const category = path . dirname ( i ) // Separate the screenshots which are at the root of the images folder. // They must be first, so we store them in another array. if ( category === '.' ) { result . push ( i ) } else { // Create a new category first. if ( ! withCategories [ category ] ) { withCategories [ category ] = { } withCategories [ category ] . title = category withCategories [ category ] . elements = [ ] } withCategories [ category ] . elements . push ( i ) } } clone . screenshots = result clone . screenshotsWithCategory = withCategories return clone }
This function takes a list of images and separate the images which are in a folder into separate categories.  **Pure function.**  Example: - burger01.png - gifs/burger02.png - wallpapers/burger03.png - wallpapers/burger04.png → { ... screenshots: ['burger01.png'] screenshotsWithCategory: { gifs: {title: 'gifs', elements: ['gifs/burger02.png']} wallpapers: {title: 'gifs', elements: ['wallpapers/burger03.png', 'wallpapers/burger04.png']} } ... }
This function takes a list of images and separate the images which are in a folder into separate categories . ** Pure function . ** Example : - burger01 . png - gifs / burger02 . png - wallpapers / burger03 . png - wallpapers / burger04 . png → { ... screenshots : [ burger01 . png ] screenshotsWithCategory : { gifs : { title : gifs elements : [ gifs / burger02 . png ] } wallpapers : { title : gifs elements : [ wallpapers / burger03 . png wallpapers / burger04 . png ] } } ... }