Skip to content

Commit 6d59daa

Browse files
committed
refactor: use TypeScript overloads for parseTransactionFlags
Address CodeRabbit review feedback: replace union signature with proper overloads to prevent invalid call shapes. - Add ParseTransactionFlagsOptions interface - Overload 1: parseTransactionFlags(tx, options?) for Transaction objects - Overload 2: parseTransactionFlags(txType, flagsNum, options?) for strings - Update tests to use new overload-friendly calling convention
1 parent 7079ba4 commit 6d59daa

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

packages/xrpl/src/models/utils/flags.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ export function convertTxFlagsToNumber(tx: Transaction): number {
142142
}, 0)
143143
}
144144

145+
/**
146+
* Options for {@link parseTransactionFlags}.
147+
*/
148+
export interface ParseTransactionFlagsOptions {
149+
/** Set to `true` to include disabled flags (as `false`) in the result. */
150+
includeAll?: boolean
151+
}
152+
145153
/**
146154
* Convert a Transaction flags property into a map for easy interpretation.
147155
*
@@ -173,20 +181,32 @@ export function convertTxFlagsToNumber(tx: Transaction): number {
173181
* @param options.includeAll - Set to `true` to include disabled flags.
174182
* @returns A map of flag names to booleans.
175183
*/
184+
export function parseTransactionFlags(
185+
tx: Transaction,
186+
options?: ParseTransactionFlagsOptions,
187+
): Record<string, boolean>
188+
export function parseTransactionFlags(
189+
txType: string,
190+
flagsNum: number,
191+
options?: ParseTransactionFlagsOptions,
192+
): Record<string, boolean>
176193
export function parseTransactionFlags(
177194
txOrType: Transaction | string,
178-
flagsNum?: number,
179-
options?: { includeAll?: boolean },
195+
flagsNumOrOptions?: number | ParseTransactionFlagsOptions,
196+
maybeOptions?: ParseTransactionFlagsOptions,
180197
): Record<string, boolean> {
181198
let flags: number
182199
let transactionType: string
200+
let options: ParseTransactionFlagsOptions | undefined
183201

184202
if (typeof txOrType === 'string') {
185203
transactionType = txOrType
186-
flags = flagsNum ?? 0
204+
flags = (flagsNumOrOptions as number) ?? 0
205+
options = maybeOptions
187206
} else {
188207
transactionType = txOrType.TransactionType
189208
flags = convertTxFlagsToNumber(txOrType)
209+
options = flagsNumOrOptions as ParseTransactionFlagsOptions | undefined
190210
}
191211

192212
const includeAll = options?.includeAll ?? false

packages/xrpl/test/models/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ describe('Models Utils', function () {
441441
Flags: PaymentChannelClaimFlags.tfRenew,
442442
}
443443

444-
const flagsMap = parseTransactionFlags(tx, undefined, {
444+
const flagsMap = parseTransactionFlags(tx, {
445445
includeAll: true,
446446
})
447447
assert.deepEqual(flagsMap, {

0 commit comments

Comments
 (0)