All files operations.js

98.61% Statements 71/72
96.88% Branches 31/32
100% Functions 18/18
98.61% Lines 71/72
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250                    107x                     1x 210x                     1x 21x                       213x       311x       184x       89x 69x     20x 12x     8x       12x   12x 78x   12x 78x 28x       12x       107x 107x         1x 27x                     1x 167x   167x 152x                     15x 15x   15x                     1x 13x                     1x 4x                     1x 89x 89x 89x   89x         89x                       89x 4x     85x 85x 85x   85x 523x 169x   169x 85x 85x     354x 85x 85x 85x         85x 85x             89x 89x   89x 406x   89x 406x 143x   143x 81x     263x 74x   189x         89x                                
import {
  curry,
  flow,
  without,
  includes,
  intersection,
  union,
} from 'lodash/fp'
 
function init() {
  return {
    items: emptyIterable(),
    selected: [],
    changed: {
      selected: [],
      deselected: [],
    },
    anchor: null,
  }
}
 
const setItems = curry((items, state) =>
  ({
    items,
    selected: state.selected,
    changed: {
      selected: [],
      deselected: [],
    },
    anchor: state.anchor,
  })
)
 
const setSelection = curry((selection, state) =>
  ({
    items: state.items,
    selected: selection,
    changed: {
      selected: without(state.selected, selection),
      deselected: without(selection, state.selected),
    },
    anchor: state.anchor,
  })
)
 
function getSelection(state) {
  return state.selected
}
 
function getChangedSelection(state) {
  return state.changed.selected
}
 
function getChangedDeselection(state) {
  return state.changed.deselected
}
 
function getAnchor(state) {
  if (state.anchor !== null && state.anchor !== undefined) {
    return state.anchor
  }
 
  if (state.selected.length > 0) {
    return getBottommostSelectedItem(state)
  }
 
  return state.items[Symbol.iterator]().next().value
}
 
function getBottommostSelectedItem(state) {
  let previousItem = null
 
  const isSelected = (item) =>
    state.selected.indexOf(item) !== -1
 
  for (const item of state.items) {
    if (isSelected(item)) {
      previousItem = item
    }
  }
 
  return previousItem
}
 
function emptyIterable() {
  const array = []
  return {
    [Symbol.iterator]: array[Symbol.iterator].bind(array),
  }
}
 
const replace = curry((selectedItem, state) =>
  ({
    items: state.items,
    selected: [selectedItem],
    changed: {
      selected: without(state.selected, [selectedItem]),
      deselected: without([selectedItem], state.selected),
    },
    anchor: selectedItem,
  })
)
 
const toggle = curry((toggledItem, state) => {
  const itemIsAdded = !includes(toggledItem, state.selected)
 
  if (itemIsAdded) {
    return {
      items: state.items,
      selected: state.selected.concat([toggledItem]),
      changed: {
        selected: [toggledItem],
        deselected: [],
      },
      anchor: toggledItem,
    }
  }
 
  const anchorIsRemoved = (toggledItem === state.anchor)
  const newAnchor = anchorIsRemoved ? null : state.anchor
 
  return {
    items: state.items,
    selected: without([toggledItem], state.selected),
    changed: {
      selected: [],
      deselected: [toggledItem],
    },
    anchor: newAnchor,
  }
})
 
const remove = curry((removedItem, state) =>
  ({
    items: state.items,
    selected: without(removedItem, state.selected),
    changed: {
      selected: [],
      deselected: intersection(removedItem, state.selected),
    },
    anchor: null,
  })
)
 
const removeAll = curry((state) =>
  ({
    items: state.items,
    selected: [],
    changed: {
      selected: [],
      deselected: state.selected,
    },
    anchor: null,
  })
)
 
const rangeTo = curry((toItem, state) => {
  const anchor = getAnchor(state, state.items)
  const connected = connectedWith(anchor, state.selected, state.items)
  const range = between(anchor, toItem, state.items)
 
  const selected = flow(
    without(connected),
    union(range)
  )(state.selected)
 
  return {
    items: state.items,
    selected,
    changed: {
      selected: without(state.selected, selected),
      deselected: without(selected, state.selected),
    },
    anchor: state.anchor,
  }
})
 
function between(start, end, iterable) {
  if (start === end) {
    return [start]
  }
 
  const result = []
  let inRange = false
  let foundStartEnd = 0
 
  for (const item of iterable) {
    if (inRange) {
      result.push(item)
 
      if ((item === start) || (item === end)) {
        foundStartEnd++
        inRange = false
      }
    } else {
      if ((item === start) || (item === end)) {
        foundStartEnd++
        inRange = true
        result.push(item)
      }
    }
  }
 
  Eif (foundStartEnd === 2) {
    return result
  }
 
  return []
}
 
function connectedWith(targetItem, selected, iterable) {
  let range = []
  let isRangeWithTargetItem = false
 
  const isSelected = (item) =>
    selected.indexOf(item) !== -1
 
  for (const item of iterable) {
    if (isSelected(item)) {
      range.push(item)
 
      if (item === targetItem) {
        isRangeWithTargetItem = true
      }
    } else {
      if (isRangeWithTargetItem) {
        break
      } else {
        range = []
      }
    }
  }
 
  return range
}
 
export {
  init,
  setItems,
  setSelection,
  replace,
  toggle,
  remove,
  removeAll,
  rangeTo,
  getSelection,
  getChangedSelection,
  getChangedDeselection,
}