Skip to content

Commit

Permalink
style: useAuthData 代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
yyz945947732 committed Aug 30, 2023
1 parent a074c08 commit 0752bb6
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/core/hooks/useAuthData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,35 @@ export function useAuthData(data?: any[], options?: Options) {
return data;
}

const { authKey, isTree, isPreserveAuthKey, childrenKey } = combineOptions;

function filterFn(item: any) {
return (
!item[combineOptions.authKey] ||
!item[authKey] ||
isMatchAuth({
auth,
authProxy,
disabled,
validator,
authCode: item[combineOptions.authKey],
authCode: item[authKey],
})
);
}

function mapFn(item: any) {
return omit(item, combineOptions.authKey);
return omit(item, authKey);
}

const authorizedData = combineOptions?.isTree
? filter(data, filterFn, { childrenKey: combineOptions.childrenKey })
const authorizedData = isTree
? filter(data, filterFn, { childrenKey })
: data.filter(filterFn);

if (combineOptions.isPreserveAuthKey) {
if (isPreserveAuthKey) {
return authorizedData;
}

const purifiedData: any[] = combineOptions?.isTree
? map(authorizedData, mapFn, { childrenKey: combineOptions.childrenKey })
const purifiedData: any[] = isTree
? map(authorizedData, mapFn, { childrenKey })
: authorizedData.map(mapFn);

return purifiedData;
Expand Down

0 comments on commit 0752bb6

Please sign in to comment.