Skip to content

Commit

Permalink
Merge pull request #16891 from Nexus-Mods/bugfix-getsafe-fallback
Browse files Browse the repository at this point in the history
fixed store getter not returning fallback in certain use cases
  • Loading branch information
insomnious authored Jan 13, 2025
2 parents ba33f00 + b584ed4 commit b8e5a69
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/util/storeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function clone<T>(input: T): T {

/**
* return an item from state or the fallback if the path doesn't lead
* to an item.
* to an item or if the item is null/undefined.
*
* @export
* @template T
Expand All @@ -28,19 +28,13 @@ function clone<T>(input: T): T {
*/
export function getSafe<T>(state: any, path: Array<(string | number)>, fallback: T): T {
let current = state;
for (const segment of path) {
if ((current === undefined)
|| (current === null)
|| !Object.hasOwnProperty.call(current, segment)) {
for (let i = 0; i < path.length; i++) {
current = current?.[path[i]];
if (current == null) {
return fallback;
} else {
current = current[segment];
}
}
if (current === undefined) {
return fallback;
}
return current;
return current ?? fallback;
}

/**
Expand Down

0 comments on commit b8e5a69

Please sign in to comment.