Skip to content

Commit

Permalink
make the array match defensive
Browse files Browse the repository at this point in the history
  • Loading branch information
mckervinc committed Jun 15, 2024
1 parent 59e612e commit 5be2ada
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# CHANGELOG

## 0.5.7

_2024-06-14_

### Bugfix

- `arrayMatch` is now null-safe

## 0.5.6

_2024-06-14_
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-fluid-table",
"version": "0.5.6",
"version": "0.5.7",
"description": "A React table inspired by react-window",
"author": "Mckervin Ceme <mckervinc@live.com>",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion src/TableContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function TableContextProvider<T>({ children, initialState }: ProviderProps<T>) {
(refreshed.minColumnWidth || _stateOnMount.current.minColumnWidth) ?? 80,
refreshed.columns
);
if (!arraysMatch(widths, _stateOnMount.current.pixelWidths!)) {
if (!arraysMatch(widths, _stateOnMount.current.pixelWidths)) {
refreshed.pixelWidths = widths;
}
}
Expand Down
20 changes: 14 additions & 6 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,26 @@ export const cx = (...args: (string | number | null | boolean | undefined)[]) =>
.join(" ");
};

export const arraysMatch = <T>(arr1: T[], arr2: T[]) => {
if (arr1.length !== arr2.length) {
return false;
export const arraysMatch = <T>(arr1: T[] | null | undefined, arr2: T[] | null | undefined) => {
if (arr1 == null && arr2 == null) {
return true;
}

for (var i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
if (arr1 != null && arr2 != null) {
if (arr1.length !== arr2.length) {
return false;
}

for (var i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}

return true;
}

return true;
return false;
};

export const randomString = (num: number) => {
Expand Down

0 comments on commit 5be2ada

Please sign in to comment.