All files / src/utils collection.ts

80.95% Statements 17/21
55.55% Branches 5/9
100% Functions 8/8
77.77% Lines 14/18

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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47  33x   33x 8x       13x     33x 8x         33x 7x       7x 7x           33x 7x                         33x 2x    
import { Collection } from '../types'
import { BeeArgumentError } from './error'
 
export function isCollection(data: unknown): data is Collection {
  Iif (!Array.isArray(data)) {
    return false
  }
 
  return data.every(entry => typeof entry === 'object' && entry.path && entry.size !== undefined)
}
 
export function assertCollection(data: unknown): asserts data is Collection {
  Iif (!isCollection(data)) {
    throw new BeeArgumentError('invalid collection', data)
  }
}
 
export function makeFilePath(file: File) {
  Iif (file.webkitRelativePath && file.webkitRelativePath !== '') {
    return file.webkitRelativePath.replace(/.*?\//i, '')
  }
 
  if (file.name) {
    return file.name
  }
 
  throw new TypeError('file is not valid File object')
}
 
export function makeCollectionFromFileList(fileList: FileList | File[]): Collection {
  return Array.from(fileList).map(file => ({
    path: makeFilePath(file),
    size: file.size,
    file,
  }))
}
 
/**
 * Calculate cumulative size of files
 *
 * @param fileList list of files to check
 * @returns size in bytes
 */
export function getCollectionSize(fileList: FileList | File[]): number {
  return Array.from(fileList).reduce((sum, file) => sum + file.size, 0)
}