Skip to content

Commit 089ba53

Browse files
committed
Tests & cleanups
1 parent acf90b3 commit 089ba53

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

packages/api-contract/src/base/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ export interface MapMessageQuery<ApiType extends ApiTypes> {
5757
}
5858

5959
export interface Namespaced <T> {
60-
[path: string]: T | Namespaced<T>;
60+
[path: string]: (T & Namespaced<T>) | Namespaced<T>;
6161
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2017-2022 @polkadot/api-contract authors & contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import type { Namespaced } from './types';
5+
6+
import { expandNs } from './util';
7+
8+
type TestNS = Namespaced<string>;
9+
10+
describe('expandNs', (): void => {
11+
it('expands into single-namespaced and normal location', (): void => {
12+
const testNs: TestNS = {};
13+
const test: Record<string, string> = {};
14+
15+
test.a = expandNs(testNs, { path: ['ns_a'] }, 'a');
16+
17+
expect(test.a).toEqual('a');
18+
expect(testNs.ns_a).toEqual('a');
19+
});
20+
21+
it('expands into multi-namespaced and normal location', (): void => {
22+
const testNs: TestNS = {};
23+
24+
expect(expandNs(testNs, { path: ['A', 'B', 'a'] }, 'a')).toEqual('a');
25+
26+
expect(testNs.A.B.a).toEqual('a');
27+
});
28+
29+
it('it expands multiples', (): void => {
30+
const testNs: TestNS = {};
31+
32+
expect(expandNs(testNs, { path: ['A', 'B', 'a'] }, 'a')).toEqual('a');
33+
expect(expandNs(testNs, { path: ['A', 'B', 'b'] }, 'b')).toEqual('b');
34+
expect(expandNs(testNs, { path: ['A', 'C', 'c'] }, 'c')).toEqual('c');
35+
36+
expect(testNs.A.B.a).toEqual('a');
37+
expect(testNs.A.B.b).toEqual('b');
38+
expect(testNs.A.C.c).toEqual('c');
39+
});
40+
});

packages/api-contract/src/base/util.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ import { extractOptions, isOptions } from '../util';
1616

1717
export const EMPTY_SALT = new Uint8Array();
1818

19+
interface WithPath {
20+
path: string[];
21+
}
22+
1923
type ConstructorTx <ApiType extends ApiTypes, R extends ISubmittableResult> = (constructorOrId: AbiConstructor | string | number, options: BlueprintOptions, params: unknown[]) => SubmittableExtrinsic<ApiType, R>;
2024

2125
export function withMeta <T extends { meta: AbiMessage }> (meta: AbiMessage, creator: Omit<T, 'meta'>): T {
@@ -47,14 +51,18 @@ export function encodeSalt (salt: Uint8Array | string | null = randomAsU8a()): U
4751
: EMPTY_SALT;
4852
}
4953

50-
export function expandNs <T> (ns: Namespaced<T>, { path }: AbiMessage, call: T): T {
54+
export function expandNs <T> (ns: Namespaced<T>, { path }: WithPath, call: T): T {
5155
if (path.length > 1) {
52-
for (let i = 0; i < path.length - 2; i++) {
53-
ns = ns[path[i]] = {} as Namespaced<T>;
56+
for (let i = 0; i < path.length - 1; i++) {
57+
if (!ns[path[i]]) {
58+
ns[path[i]] = {};
59+
}
60+
61+
ns = ns[path[i]];
5462
}
5563
}
5664

57-
ns[path[path.length - 1]] = call;
65+
ns[path[path.length - 1]] = call as unknown as Namespaced<T>;
5866

5967
return call;
6068
}

0 commit comments

Comments
 (0)