From 03bfe2a4f7b29b15cd90127974b7fc1d8b03edf9 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Tue, 9 Apr 2024 20:02:29 -0400 Subject: [PATCH] Added deep convertion to Result for toObject and toArray (#4681). --- src.ts/abi/coders/abstract-coder.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src.ts/abi/coders/abstract-coder.ts b/src.ts/abi/coders/abstract-coder.ts index 588a129bd..981b2b80a 100644 --- a/src.ts/abi/coders/abstract-coder.ts +++ b/src.ts/abi/coders/abstract-coder.ts @@ -130,27 +130,33 @@ export class Result extends Array { } /** - * Returns the Result as a normal Array. + * Returns the Result as a normal Array. If %%deep%%, any children + * which are Result objects are also converted to a normal Array. * * This will throw if there are any outstanding deferred * errors. */ - toArray(): Array { + toArray(deep?: boolean): Array { const result: Array = [ ]; this.forEach((item, index) => { if (item instanceof Error) { throwError(`index ${ index }`, item); } + if (deep && item instanceof Result) { + item = item.toArray(deep); + } result.push(item); }); return result; } /** - * Returns the Result as an Object with each name-value pair. + * Returns the Result as an Object with each name-value pair. If + * %%deep%%, any children which are Result objects are also + * converted to an Object. * * This will throw if any value is unnamed, or if there are * any outstanding deferred errors. */ - toObject(): Record { + toObject(deep?: boolean): Record { return this.#names.reduce((accum, name, index) => { assert(name != null, "value at index ${ index } unnamed", "UNSUPPORTED_OPERATION", { operation: "toObject()" @@ -158,7 +164,11 @@ export class Result extends Array { // Add values for names that don't conflict if (!(name in accum)) { - accum[name] = this.getValue(name); + let child = this.getValue(name); + if (deep && child instanceof Result) { + child = child.toObject(deep); + } + accum[name] = child; } return accum;