Skip to content

Commit 862842f

Browse files
committed
Refactor getTermsByEnforcer for readability
1 parent 60e34c6 commit 862842f

1 file changed

Lines changed: 47 additions & 21 deletions

File tree

  • packages/7715-permission-types/src/permissions

packages/7715-permission-types/src/permissions/utils.ts

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,43 +50,69 @@ const ENFORCER_CONTRACT_NAMES = {
5050
* @param options0.throwIfNotFound - Whether to throw if no match is found.
5151
* @returns The matched terms, or `null` when disabled and no caveat is found.
5252
*/
53-
export function getTermsByEnforcer<TThrowIfNotFound extends boolean = true>({
53+
export function getTermsByEnforcer({
5454
caveats,
5555
enforcer,
5656
throwIfNotFound,
5757
}: {
5858
caveats: ChecksumCaveat[];
5959
enforcer: Hex;
60-
throwIfNotFound?: TThrowIfNotFound;
61-
}): TThrowIfNotFound extends true ? Hex : Hex | null {
62-
const matchingCaveats = caveats.filter(
63-
(caveat) => caveat.enforcer === enforcer,
64-
);
60+
throwIfNotFound?: true;
61+
}): Hex;
62+
export function getTermsByEnforcer({
63+
caveats,
64+
enforcer,
65+
throwIfNotFound,
66+
}: {
67+
caveats: ChecksumCaveat[];
68+
enforcer: Hex;
69+
throwIfNotFound: false;
70+
}): Hex | null;
71+
/**
72+
* Implementation signature for getTermsByEnforcer overloads.
73+
*
74+
* @param options0 - Terms lookup arguments.
75+
* @param options0.caveats - Caveats to search.
76+
* @param options0.enforcer - Enforcer address to match.
77+
* @param options0.throwIfNotFound - Whether to throw if no match is found.
78+
* @returns The matched terms, or `null` when disabled and no caveat is found.
79+
*/
80+
export function getTermsByEnforcer({
81+
caveats,
82+
enforcer,
83+
throwIfNotFound = true,
84+
}: {
85+
caveats: ChecksumCaveat[];
86+
enforcer: Hex;
87+
throwIfNotFound?: boolean;
88+
}): Hex | null {
89+
let matchingCaveat: ChecksumCaveat | undefined;
90+
91+
for (const caveat of caveats) {
92+
if (caveat.enforcer !== enforcer) {
93+
continue;
94+
}
6595

66-
if (matchingCaveats.length === 0) {
67-
if (throwIfNotFound ?? true) {
96+
if (matchingCaveat) {
6897
throw new Error(
69-
`Invalid caveats: no caveat found matching enforcer ${enforcer}`,
98+
`Invalid caveats: multiple caveats found matching enforcer ${enforcer}`,
7099
);
71100
}
72-
return null as TThrowIfNotFound extends true ? Hex : Hex | null;
73-
}
74101

75-
if (matchingCaveats.length > 1) {
76-
throw new Error(
77-
`Invalid caveats: multiple caveats found matching enforcer ${enforcer}`,
78-
);
102+
matchingCaveat = caveat;
79103
}
80104

81-
const [caveat] = matchingCaveats;
105+
if (!matchingCaveat) {
106+
if (throwIfNotFound) {
107+
throw new Error(
108+
`Invalid caveats: no caveat found matching enforcer ${enforcer}`,
109+
);
110+
}
82111

83-
if (!caveat) {
84-
throw new Error(
85-
`Invalid caveats: no caveat found matching enforcer ${enforcer}`,
86-
);
112+
return null;
87113
}
88114

89-
return caveat.terms;
115+
return matchingCaveat.terms;
90116
}
91117

92118
/**

0 commit comments

Comments
 (0)