All files / atom/slider/src markerFactory.js

38.46% Statements 10/26
30% Branches 3/10
37.5% Functions 3/8
37.5% Lines 9/24

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 48 49 50 51 521x                           1x 303x 3x               1x                               1x 3x   3x       3x        
const customMarksFactory = ({marks, max, min, step, ticks}) => {
  const steps = Array.from(Array(ticks), (x, index) => index * step + min)
  const markRange = Math.round((max - min) / (marks.length - 1))
  let idx = 0
  return steps.reduce((acc, step) => {
    if (step === min || step % (markRange + min) === 0) {
      acc[step] = marks[idx++]
    } else {
      acc[step] = ''
    }
    return acc
  }, {})
}
 
const linearMarksFactory = ({step, min, max, ticks}) => {
  const steps = Array.from(Array(ticks), (x, index) => index * step)
  return step === 1
    ? {[min]: min, [max]: max}
    : steps.reduce((marksConfig, step) => {
        marksConfig[step] = step
        return marksConfig
      }, {})
}
 
const fullWidthMarksFactory = marks => {
  const marksKeys = Object.keys(marks)
  const firstMarkKey = marksKeys[0]
  const lastMarkKey = marksKeys[marksKeys.length - 1]
 
  const nextMarks = {
    ...marks,
    [firstMarkKey]: {label: marks[firstMarkKey], style: {transform: 'inherit'}},
    [lastMarkKey]: {
      label: marks[lastMarkKey],
      style: {transform: 'inherit', left: 'auto', right: '0'}
    }
  }
  return nextMarks
}
 
const markerFactory = ({step, min, max, marks, isFullWidth}) => {
  const ticks = Math.round((max - min) / step) + 1
 
  const nextMarks = marks
    ? customMarksFactory({marks, min, max, step, ticks})
    : linearMarksFactory({step, min, max, ticks})
 
  return isFullWidth ? fullWidthMarksFactory(nextMarks) : nextMarks
}
 
export default markerFactory