1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.augmentObject = void 0;
|
4 | const util_1 = require("@polkadot/util");
|
5 | const l = (0, util_1.logger)('api/augment');
|
6 | function logLength(type, values, and = []) {
|
7 | return values.length
|
8 | ? ` ${values.length} ${type}${and.length ? ' and' : ''}`
|
9 | : '';
|
10 | }
|
11 | function logValues(type, values) {
|
12 | return values.length
|
13 | ? `\n\t${type.padStart(7)}: ${values.sort().join(', ')}`
|
14 | : '';
|
15 | }
|
16 | function warn(prefix, type, [added, removed]) {
|
17 | if (added.length || removed.length) {
|
18 | l.warn(`api.${prefix}: Found${logLength('added', added, removed)}${logLength('removed', removed)} ${type}:${logValues('added', added)}${logValues('removed', removed)}`);
|
19 | }
|
20 | }
|
21 | function findSectionExcludes(a, b) {
|
22 | return a.filter((s) => !b.includes(s));
|
23 | }
|
24 | function findSectionIncludes(a, b) {
|
25 | return a.filter((s) => b.includes(s));
|
26 | }
|
27 | function extractSections(src, dst) {
|
28 | const srcSections = Object.keys(src);
|
29 | const dstSections = Object.keys(dst);
|
30 | return [
|
31 | findSectionExcludes(srcSections, dstSections),
|
32 | findSectionExcludes(dstSections, srcSections)
|
33 | ];
|
34 | }
|
35 | function findMethodExcludes(src, dst) {
|
36 | const srcSections = Object.keys(src);
|
37 | const dstSections = findSectionIncludes(Object.keys(dst), srcSections);
|
38 | const excludes = [];
|
39 | for (let s = 0, scount = dstSections.length; s < scount; s++) {
|
40 | const section = dstSections[s];
|
41 | const srcMethods = Object.keys(src[section]);
|
42 | const dstMethods = Object.keys(dst[section]);
|
43 | for (let d = 0, mcount = dstMethods.length; d < mcount; d++) {
|
44 | const method = dstMethods[d];
|
45 | if (!srcMethods.includes(method)) {
|
46 | excludes.push(`${section}.${method}`);
|
47 | }
|
48 | }
|
49 | }
|
50 | return excludes;
|
51 | }
|
52 | function extractMethods(src, dst) {
|
53 | return [
|
54 | findMethodExcludes(dst, src),
|
55 | findMethodExcludes(src, dst)
|
56 | ];
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | function augmentObject(prefix, src, dst, fromEmpty = false) {
|
64 | fromEmpty && (0, util_1.objectClear)(dst);
|
65 |
|
66 |
|
67 |
|
68 | if (prefix && Object.keys(dst).length) {
|
69 | warn(prefix, 'modules', extractSections(src, dst));
|
70 | warn(prefix, 'calls', extractMethods(src, dst));
|
71 | }
|
72 | const sections = Object.keys(src);
|
73 | for (let i = 0, count = sections.length; i < count; i++) {
|
74 | const section = sections[i];
|
75 | const methods = src[section];
|
76 |
|
77 |
|
78 | if (!dst[section]) {
|
79 | dst[section] = {};
|
80 | }
|
81 | (0, util_1.lazyMethods)(dst[section], Object.keys(methods), (m) => methods[m]);
|
82 | }
|
83 | return dst;
|
84 | }
|
85 | exports.augmentObject = augmentObject;
|