Skip to content

Commit

Permalink
fix(bodyValue): account for null or undefined previous body values
Browse files Browse the repository at this point in the history
  • Loading branch information
ramfox authored and chriswhong committed Nov 29, 2019
1 parent 7797cad commit f9a93e0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
1 change: 0 additions & 1 deletion app/components/ComponentList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ function removeHiddenComponents (status: DatasetStatus) {
}

return (component): boolean => {
console.log(status[component.name])
return status[component.name] || !hideWhenMissing[component.name]
}
}
Expand Down
17 changes: 7 additions & 10 deletions app/utils/bodyValue.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { PageInfo } from '../../app/models/store'

export default function bodyValue (prev: Object | any[] | undefined, curr: Object | any[], pageInfo: PageInfo): Object | any[] {
// if there is no previous body value, just return the current body value
if (!prev) return curr
// work through cases where the current body value is an array
if (Array.isArray(curr)) {
// if the previous body value is not an array
// just return the current body value
if (!Array.isArray(prev)) {
return curr
if (curr.length === 0) {
if (!prev) return curr
return prev
}
// otherwise, it is an array and we should concat the lists

if (Array.isArray(curr[0])) {
// for tabular/2D arrays only:
Expand All @@ -23,6 +19,9 @@ export default function bodyValue (prev: Object | any[] | undefined, curr: Objec
d.unshift(index)
})

// if there is no previous body value, just return the current body value
if (!prev || !Array.isArray(prev)) return curr

if (prev.length !== 0) {
const currLastIndex = curr[curr.length - 1][0]
const prevLastIndex = prev[prev.length - 1][0] || 0
Expand Down Expand Up @@ -51,14 +50,12 @@ export default function bodyValue (prev: Object | any[] | undefined, curr: Objec

return [...prev, ...curr]
}

return [...prev, ...curr]
}
// work through cases where the current body value is an object (but not an array)
if (typeof curr === 'object') {
// if the previous body value is not an object (or is an object but is also an array)
// return the current body value
if (!(typeof prev === 'object') && Array.isArray(prev)) {
if (!prev || (!(typeof prev === 'object') && Array.isArray(prev))) {
return curr
}
// otherwise return the spread!
Expand Down
7 changes: 7 additions & 0 deletions test/app/utils/bodyValue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ describe('bodyValue', () => {
pageInfo: pageInfo2,
expected: []
},
{
describe: 'prev is undefined',
prev: undefined,
curr: [['a','b','c'],['d','e','f']],
pageInfo: initPageInfo,
expected: [[0, 'a','b','c'],[1, 'd','e','f']]
},
{
describe: 'prev is empty',
prev: [],
Expand Down

0 comments on commit f9a93e0

Please sign in to comment.