More internal reduce adjustments (#4149)
This commit is contained in:
@@ -195,17 +195,19 @@ export class Struct<
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the Type description to sthe structure
|
||||
* @description Returns the Type description of the structure
|
||||
*/
|
||||
public get Type (): E {
|
||||
return (Object
|
||||
.entries(this.#Types))
|
||||
.reduce((result: E, [key, Type]): E => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
(result as any)[key as keyof E] = new Type(this.registry).toRawType();
|
||||
const result: Record<string, string> = {};
|
||||
const defs = Object.entries(this.#Types);
|
||||
|
||||
return result;
|
||||
}, {} as E);
|
||||
for (let i = 0; i < defs.length; i++) {
|
||||
const [key, Type] = defs[i];
|
||||
|
||||
result[key] = new Type(this.registry).toRawType();
|
||||
}
|
||||
|
||||
return result as E;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { flattenUniq } from '.';
|
||||
|
||||
describe('flattenUniq', (): void => {
|
||||
it('flattens nested arrays', (): void => {
|
||||
expect(
|
||||
flattenUniq(['a', ['b', ['c', 'd', ['e']]], [['f', 'g']]])
|
||||
).toEqual(['a', 'b', 'c', 'd', 'e', 'f', 'g']);
|
||||
});
|
||||
|
||||
it('flattens nested arrays, removing duplicated', (): void => {
|
||||
expect(
|
||||
flattenUniq(['a', 'b', ['b', ['b', 'c', 'd', ['e'], 'f', 'g']], ['c', ['f', 'g']]])
|
||||
).toEqual(['a', 'b', 'c', 'd', 'e', 'f', 'g']);
|
||||
});
|
||||
});
|
||||
@@ -4,16 +4,16 @@
|
||||
type Types = string | Types[];
|
||||
|
||||
/** @internal */
|
||||
export function flattenUniq (list: Types[], start: string[] = []): string[] {
|
||||
return [...new Set(
|
||||
list.reduce((result: string[], entry): string[] => {
|
||||
if (Array.isArray(entry)) {
|
||||
return flattenUniq(entry, result);
|
||||
}
|
||||
export function flattenUniq (list: Types[], result: string[] = []): string[] {
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
const entry = list[i];
|
||||
|
||||
if (Array.isArray(entry)) {
|
||||
flattenUniq(entry, result);
|
||||
} else {
|
||||
result.push(entry);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}, start)
|
||||
)];
|
||||
return [...new Set(result)];
|
||||
}
|
||||
|
||||
@@ -93,18 +93,20 @@ function decodeStorageKey (value?: string | Uint8Array | StorageKey | StorageEnt
|
||||
function decodeHashers <A extends AnyTuple> (registry: Registry, value: Uint8Array, hashers: [StorageHasher, SiLookupTypeId][]): A {
|
||||
// the storage entry is xxhashAsU8a(prefix, 128) + xxhashAsU8a(method, 128), 256 bits total
|
||||
let offset = 32;
|
||||
const result = new Array<Codec>(hashers.length);
|
||||
|
||||
return hashers.reduce((result: Codec[], [hasher, type]): Codec[] => {
|
||||
for (let i = 0; i < hashers.length; i++) {
|
||||
const [hasher, type] = hashers[i];
|
||||
const [hashLen, canDecode] = HASHER_MAP[hasher.type as 'Identity'];
|
||||
const decoded = canDecode
|
||||
? registry.createType(registry.createLookupType(type) as 'Raw', value.subarray(offset + hashLen))
|
||||
: registry.createType('Raw', value.subarray(offset, offset + hashLen));
|
||||
|
||||
offset += hashLen + (canDecode ? decoded.encodedLength : 0);
|
||||
result.push(decoded);
|
||||
result[i] = decoded;
|
||||
}
|
||||
|
||||
return result;
|
||||
}, []) as A;
|
||||
return result as A;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
|
||||
Reference in New Issue
Block a user