Skip to content

Commit

Permalink
fix(array): ensure item names are numbers
Browse files Browse the repository at this point in the history
Throw error in non-production if `setItem` works on `index`es that
aren't numbers.
  • Loading branch information
davidbonnet committed Apr 9, 2019
1 parent 98360d2 commit 70e0462
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/arrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ export const array = (Component) =>
key,
value: props.value && props.value[index],
name: index,
onChange: props.onChange && this.onChangeItem,
onChange:
props.onChange &&
lazyProperty(this, 'onChangeItem', onChangeItem),
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/immutables.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ export function setItem(array, index, value) {
If `index` equals `-1` or is `undefined`, returns the `array` untouched.
If the `array` is `nil`, it is considered as an `EMPTY_ARRAY`.
*/
if (process.env.NODE_ENV !== 'production') {
if (index != null && !isFinite(index)) {
throw new Error(
`Expected "index" to be a number, but is of type "${typeof index}" instead`,
)
}
}
return index === -1 || index == null
? array == null
? EMPTY_ARRAY
Expand Down

0 comments on commit 70e0462

Please sign in to comment.