Skip to content

Commit

Permalink
Tests & cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Mar 31, 2022
1 parent acf90b3 commit 089ba53
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/api-contract/src/base/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ export interface MapMessageQuery<ApiType extends ApiTypes> {
}

export interface Namespaced <T> {
[path: string]: T | Namespaced<T>;
[path: string]: (T & Namespaced<T>) | Namespaced<T>;
}
40 changes: 40 additions & 0 deletions packages/api-contract/src/base/util.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2017-2022 @polkadot/api-contract authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { Namespaced } from './types';

import { expandNs } from './util';

type TestNS = Namespaced<string>;

describe('expandNs', (): void => {
it('expands into single-namespaced and normal location', (): void => {
const testNs: TestNS = {};
const test: Record<string, string> = {};

test.a = expandNs(testNs, { path: ['ns_a'] }, 'a');

expect(test.a).toEqual('a');
expect(testNs.ns_a).toEqual('a');
});

it('expands into multi-namespaced and normal location', (): void => {
const testNs: TestNS = {};

expect(expandNs(testNs, { path: ['A', 'B', 'a'] }, 'a')).toEqual('a');

expect(testNs.A.B.a).toEqual('a');
});

it('it expands multiples', (): void => {
const testNs: TestNS = {};

expect(expandNs(testNs, { path: ['A', 'B', 'a'] }, 'a')).toEqual('a');
expect(expandNs(testNs, { path: ['A', 'B', 'b'] }, 'b')).toEqual('b');
expect(expandNs(testNs, { path: ['A', 'C', 'c'] }, 'c')).toEqual('c');

expect(testNs.A.B.a).toEqual('a');
expect(testNs.A.B.b).toEqual('b');
expect(testNs.A.C.c).toEqual('c');
});
});
16 changes: 12 additions & 4 deletions packages/api-contract/src/base/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import { extractOptions, isOptions } from '../util';

export const EMPTY_SALT = new Uint8Array();

interface WithPath {
path: string[];
}

type ConstructorTx <ApiType extends ApiTypes, R extends ISubmittableResult> = (constructorOrId: AbiConstructor | string | number, options: BlueprintOptions, params: unknown[]) => SubmittableExtrinsic<ApiType, R>;

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

export function expandNs <T> (ns: Namespaced<T>, { path }: AbiMessage, call: T): T {
export function expandNs <T> (ns: Namespaced<T>, { path }: WithPath, call: T): T {
if (path.length > 1) {
for (let i = 0; i < path.length - 2; i++) {
ns = ns[path[i]] = {} as Namespaced<T>;
for (let i = 0; i < path.length - 1; i++) {
if (!ns[path[i]]) {
ns[path[i]] = {};
}

ns = ns[path[i]];
}
}

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

return call;
}
Expand Down

0 comments on commit 089ba53

Please sign in to comment.