# All files / list index.js

95.92% Statements 47/49
70.59% Branches 24/34
100% Functions 13/13
95.83% Lines 46/48

    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

    3x
    7x

    2x
    1x
    1x
    7x
    2x
    2x

    5x

    1x
    1x
    1x

    7x
    2x
    2x

    5x

    1x

    1x

    1x
    1x
    1x

    1x
    1x
    1x

    1x
    1x
    1x

    1x
    1x
    1x

    2x
    2x
    2x
    1x

    1x

    1x
    1x
    1x

    2x
    2x

    3x
    3x

    3x

    1x
    1x

    1x

     import { buildReducer } from '../lib/buildReducer'
    import { set } from 'dot-prop-immutable'
    import * as ActionTypes from './actionTypes'
    export * from './actions'
    export * from './selectors'

    function lrem(list = [], count = 0, value) {
      if (count === 0) {
        return list.filter(x => x !== value)
      }
      if (count > 0) {
        let removed = 0
        return list.filter((item, index) => {
          if (item === value && removed < count) {
            removed += 1
            return false
          }
          return true
        })
      }
      Eif (count < 0) {
        let removed = count
        return list
          .reverse()
          .filter((item, index) => {
            if (item === value && removed < 0) {
              removed += 1
              return false
            }
            return true
          })
          .reverse()
      }
    }

    function ltrim(list = [], start, stop = start) {
      return list.slice(start, stop + 1)
    }

    const initialState = {}
    export default buildReducer(initialState, {
      [ActionTypes.LPUSH]: (state, action) => {
        const { target, value } = action.payload
        const prevItems = state[target] || []
        return set(state, target, [value, ...prevItems])
      },
      [ActionTypes.RPUSH]: (state, action) => {
        const { target, value } = action.payload
        const prevItems = state[target] || []
        return set(state, target, [...prevItems, value])
      },
      [ActionTypes.LPOP]: (state, action) => {
        const { target } = action.payload
        const newList = (state[target] || []).slice(1)
        return set(state, target, newList)
      },
      [ActionTypes.RPOP]: (state, action) => {
        const { target } = action.payload
        const list = state[target] || []
        return set(state, target, list.slice(0, list.length - 1))
      },
      [ActionTypes.LSET]: (state, action) => {
        const { target, value, index } = action.payload
        const prevItems = state[target] || []
        if (index > prevItems.length) {
          console.warn(
            `LSET specified out of bounds index (${index}) for list ${target}`
          )
          return state
        }
        const newList = [].concat(prevItems)
        newList[index] = value
        return set(state, target, newList)
      },
      [ActionTypes.LREPLACE]: (state, action) => {
        const { target, elements } = action.payload
        return set(state, target, [].concat(elements))
      },
      [ActionTypes.LREM]: (state, action) => {
        const { target, count, value } = action.payload
        Iif (!state[target]) {
          return state
        }
        return set(state, target, lrem(state[target], count, value))
      },
      [ActionTypes.LTRIM]: (state, action) => {
        const { target, start, stop } = action.payload
        Iif (!state[target]) {
          return state
        }
        return set(state, target, ltrim(state[target], start, stop))
      }
    })