Skip to content

Commit

Permalink
Refactor the implementation of deleteInWithCleanUp
Browse files Browse the repository at this point in the history
  • Loading branch information
ms88privat committed Nov 17, 2017
1 parent 2b26dfe commit c8aa688
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/__tests__/deleteInWithCleanUp.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import createDeleteInWithCleanUp from '../deleteInWithCleanUp'
import createCreateDeleteInWithCleanUp from '../deleteInWithCleanUp'
import plain from '../structure/plain'
import plainExpectations from '../structure/plain/__tests__/expectations'
import immutable from '../structure/immutable'
import immutableExpectations from '../structure/immutable/__tests__/expectations'

const describeDeleteInWithCleanUp = (name, structure, setup) => {
const { fromJS } = structure
const deleteInWithCleanUp = createDeleteInWithCleanUp(structure)
const deleteInWithCleanUp = createCreateDeleteInWithCleanUp(structure)()

describe(name, () => {
beforeAll(() => {
Expand Down
18 changes: 15 additions & 3 deletions src/createReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,22 @@ import {
CLEAR_FIELDS,
UPDATE_SYNC_WARNINGS
} from './actionTypes'
import createDeleteInWithCleanUp from './deleteInWithCleanUp'
import createCreateDeleteInWithCleanUp from './deleteInWithCleanUp'
import plain from './structure/plain'
import type { Action, Structure } from './types.js.flow'

const shouldDelete = ({ getIn }) => (state, path) => {
let initialValuesPath = null

if (path.startsWith('values')) {
initialValuesPath = path.replace('values', 'initial')
}

const initialValueComparison = initialValuesPath ? (getIn(state, initialValuesPath) === undefined) : true

return (getIn(state, path) !== undefined) && initialValueComparison
}

const isReduxFormAction = action =>
action &&
action.type &&
Expand All @@ -60,8 +72,8 @@ function createReducer<M, L>(structure: Structure<M, L>) {
some,
splice
} = structure
const deleteInWithCleanUp = createDeleteInWithCleanUp(structure)
const plainDeleteInWithCleanUp = createDeleteInWithCleanUp(plain)
const deleteInWithCleanUp = createCreateDeleteInWithCleanUp(structure)(shouldDelete)
const plainDeleteInWithCleanUp = createCreateDeleteInWithCleanUp(plain)(shouldDelete)
const doSplice = (state, key, field, index, removeNum, value, force) => {
const existing = getIn(state, `${key}.${field}`)
return existing || force
Expand Down
68 changes: 30 additions & 38 deletions src/deleteInWithCleanUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,42 @@
import { toPath } from 'lodash'
import type { Structure } from './types'

function createDeleteInWithCleanUp<DIM, DIL>({
deepEqual,
empty,
getIn,
deleteIn,
setIn
}: Structure<DIM, DIL>) {
const deleteInWithCleanUp = (state: DIM | DIL, path: string): DIM | DIL => {
let initialValuesPath = null

if (path.startsWith('values')) {
initialValuesPath = path.replace('values', 'initial')
}

if (path[path.length - 1] === ']') {
// array path
const pathTokens = toPath(path)
pathTokens.pop()
const parent = getIn(state, pathTokens.join('.'))
return parent ? setIn(state, path) : state
}

let result: DIM | DIL = state
function createCreateDeleteInWithCleanUp<DIM, DIL>(structure: Structure<DIM, DIL>) {
const shouldDeleteDefault = (structure) => (state, path) => structure.getIn(state, path) !== undefined

const { deepEqual, empty, getIn, deleteIn, setIn } = structure

return (shouldDelete = shouldDeleteDefault) => {
const deleteInWithCleanUp = (state: DIM | DIL, path: string): DIM | DIL => {
if (path[path.length - 1] === ']') {
// array path
const pathTokens = toPath(path)
pathTokens.pop()
const parent = getIn(state, pathTokens.join('.'))
return parent ? setIn(state, path) : state
}

const initialValueComparison = initialValuesPath ? (getIn(state, initialValuesPath) === undefined) : true
let result: DIM | DIL = state

if (getIn(state, path) !== undefined && initialValueComparison) {
result = deleteIn(state, path)
}
if (shouldDelete(structure)(state, path)) {
result = deleteIn(state, path)
}

const dotIndex = path.lastIndexOf('.')
if (dotIndex > 0) {
const parentPath = path.substring(0, dotIndex)
if (parentPath[parentPath.length - 1] !== ']') {
const parent = getIn(result, parentPath)
if (deepEqual(parent, empty)) {
return deleteInWithCleanUp(result, parentPath)
const dotIndex = path.lastIndexOf('.')
if (dotIndex > 0) {
const parentPath = path.substring(0, dotIndex)
if (parentPath[parentPath.length - 1] !== ']') {
const parent = getIn(result, parentPath)
if (deepEqual(parent, empty)) {
return deleteInWithCleanUp(result, parentPath)
}
}
}
return result
}
return result
}

return deleteInWithCleanUp
return deleteInWithCleanUp
}
}

export default createDeleteInWithCleanUp
export default createCreateDeleteInWithCleanUp

0 comments on commit c8aa688

Please sign in to comment.