All files utils.js

35.29% Statements 12/34
15.79% Branches 3/19
16.67% Functions 1/6
38.71% Lines 12/31
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 631x 2x 2x 2x   2x 3x 3x     2x     1x                 1x                                   1x                           1x                
export const formatBytes = (b) => {
  const units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
  let l = 0
  let n = b
 
  while (n >= 1024) {
    n /= 1024
    l += 1
  }
 
  return `${n.toFixed(n >= 10 || l < 1 ? 0 : 1)}${units[l]}`
}
 
export const formatDuration = (seconds) => {
  const date = new Date(null)
  date.setSeconds(seconds)
  const dateString = date.toISOString().slice(11, 19)
  if (seconds < 3600) return dateString.slice(3)
  return dateString
}
 
// adapted from: https://github.com/okonet/attr-accept/blob/master/src/index.js
export const accepts = (file, accept) => {
  if (!file || !accept || accept === '*') return true
 
  const fileName = file.name || ''
  const mimeType = file.type || ''
  const baseMimeType = mimeType.replace(/\/.*$/, '')
 
  return accept.split(',').map(t => t.trim()).some((type) => {
    if (type.charAt(0) === '.') {
      return fileName.toLowerCase().endsWith(type.toLowerCase())
    } else if (type.endsWith('/*')) {
      // this is something like an image/* mime type
      return baseMimeType === type.replace(/\/.*$/, '')
    }
    return mimeType === type
  })
}
 
export const defaultClassNames = {
  dropzone: 'dzu-dropzone',
  dropzoneWithFiles: 'dzu-dropzone',
  dropzoneActive: 'dzu-dropzoneActive',
  dropzoneReject: 'dzu-dropzoneActive',
  input: 'dzu-input',
  inputLabel: 'dzu-inputLabel',
  inputLabelWithFiles: 'dzu-inputLabelWithFiles',
  preview: 'dzu-previewContainer',
  previewImage: 'dzu-previewImage',
  submitButtonContainer: 'dzu-submitButtonContainer',
  submitButton: 'dzu-submitButton',
}
 
export const mergeStyles = (classNames, styles, addClassNames) => {
  const mergedClassNames = { ...defaultClassNames, ...classNames }
  for (const key of Object.keys(addClassNames)) {
    mergedClassNames[key] = `${mergedClassNames[key]} ${addClassNames[key]}`
  }
 
  return { classNames: mergedClassNames, styles }
}