all files / src/structure/immutable/ deleteWithPath.js

97.56% Statements 40/41
93.02% Branches 40/43
100% Functions 7/7
96.55% Lines 28/29
1 statement, 1 function, 10 branches Ignored     
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    35×       35×           32× 19× 19× 19× 29× 29×   22× 22×   22×     19×   13×        
import { Map, List } from 'immutable'
 
const deleteWithPath = (state, matcher, path = '') => {
  Iif (state === undefined) {
    return state
  }
 
  if (List.isList(state)) {
    let changed = false
    let processed = []
    state.forEach((value, index) => {
      const nextPath = path ? `${path}[${index}]` : String(index)
      if (matcher.test(nextPath)) {
        changed = true
      } else {
        const next = deleteWithPath(value, matcher, nextPath)
        if (next !== value) {
          changed = true
        }
        processed.push(next)
      }
    })
    return changed ? List(processed) : state
  }
  if (Map.isMap(state)) {
    let changed = false
    const processed = {}
    state.forEach((value, key) => {
      const nextPath = path ? `${path}.${key}` : key
      if (matcher.test(nextPath)) {
        changed = true
      } else {
        const next = deleteWithPath(value, matcher, nextPath)
        if (next !== value) {
          changed = true
        }
        processed[ key ] = next
      }
    })
    return changed ? Map(processed) : state
  }
  return state
}
 
export default deleteWithPath