Skip to content

Commit 5fecf32

Browse files
authored
Merge pull request #56 from MetaMask/chore/value-check-refactor
Refactor check if a variable is specified
2 parents 84f41ac + 4797aec commit 5fecf32

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

packages/delegation-toolkit/src/experimental/erc7715RequestExecutionPermissionsAction.ts

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,16 @@ function formatPermissionsRequest(
212212
};
213213
}
214214

215+
/**
216+
* Checks if a value is defined (not null or undefined).
217+
*
218+
* @param value - The value to check.
219+
* @returns A boolean indicating whether the value is defined.
220+
*/
221+
function isDefined<TValue>(value: TValue | null | undefined): value is TValue {
222+
return value !== undefined && value !== null;
223+
}
224+
215225
/**
216226
* Asserts that a value is defined (not null or undefined).
217227
*
@@ -223,7 +233,7 @@ function assertIsDefined<TValue>(
223233
value: TValue | null | undefined,
224234
message?: string,
225235
): asserts value is TValue {
226-
if (value === null || value === undefined) {
236+
if (!isDefined(value)) {
227237
throw new Error(message ?? 'Invalid parameters: value is required');
228238
}
229239
}
@@ -319,21 +329,14 @@ function formatNativeTokenStreamPermission({
319329
},
320330
} = permission;
321331

322-
const isInitialAmountSpecified =
323-
initialAmount !== undefined && initialAmount !== null;
324-
325-
const isMaxAmountSpecified = maxAmount !== undefined && maxAmount !== null;
326-
327-
const isStartTimeSpecified = startTime !== undefined && startTime !== null;
328-
329332
const optionalFields = {
330-
...(isInitialAmountSpecified && {
333+
...(isDefined(initialAmount) && {
331334
initialAmount: toHexOrThrow(initialAmount),
332335
}),
333-
...(isMaxAmountSpecified && {
336+
...(isDefined(maxAmount) && {
334337
maxAmount: toHexOrThrow(maxAmount),
335338
}),
336-
...(isStartTimeSpecified && {
339+
...(isDefined(startTime) && {
337340
startTime: Number(startTime),
338341
}),
339342
...(justification ? { justification } : {}),
@@ -380,21 +383,14 @@ function formatErc20TokenStreamPermission({
380383
},
381384
} = permission;
382385

383-
const isInitialAmountSpecified =
384-
initialAmount !== undefined && initialAmount !== null;
385-
386-
const isMaxAmountSpecified = maxAmount !== undefined && maxAmount !== null;
387-
388-
const isStartTimeSpecified = startTime !== undefined && startTime !== null;
389-
390386
const optionalFields = {
391-
...(isInitialAmountSpecified && {
387+
...(isDefined(initialAmount) && {
392388
initialAmount: toHexOrThrow(initialAmount),
393389
}),
394-
...(isMaxAmountSpecified && {
390+
...(isDefined(maxAmount) && {
395391
maxAmount: toHexOrThrow(maxAmount),
396392
}),
397-
...(isStartTimeSpecified && {
393+
...(isDefined(startTime) && {
398394
startTime: Number(startTime),
399395
}),
400396
...(justification ? { justification } : {}),
@@ -430,10 +426,8 @@ function formatNativeTokenPeriodicPermission({
430426
data: { periodAmount, periodDuration, startTime, justification },
431427
} = permission;
432428

433-
const isStartTimeSpecified = startTime !== undefined && startTime !== null;
434-
435429
const optionalFields = {
436-
...(isStartTimeSpecified && {
430+
...(isDefined(startTime) && {
437431
startTime: Number(startTime),
438432
}),
439433
...(justification ? { justification } : {}),
@@ -475,10 +469,8 @@ function formatErc20TokenPeriodicPermission({
475469
},
476470
} = permission;
477471

478-
const isStartTimeSpecified = startTime !== undefined && startTime !== null;
479-
480472
const optionalFields = {
481-
...(isStartTimeSpecified && {
473+
...(isDefined(startTime) && {
482474
startTime: Number(startTime),
483475
}),
484476
...(justification ? { justification } : {}),

0 commit comments

Comments
 (0)