1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.augmentObject = augmentObject;
|
7 |
|
8 | var _util = require("@polkadot/util");
|
9 |
|
10 |
|
11 |
|
12 | const l = (0, _util.logger)('api/augment');
|
13 |
|
14 | function logLength(type, values) {
|
15 | let and = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
16 | return values.length ? ` ${values.length} ${type}${and.length ? ' and' : ''}` : '';
|
17 | }
|
18 |
|
19 | function logValues(type, values) {
|
20 | return values.length ? `\n\t${type.padStart(7)}: ${values.sort().join(', ')}` : '';
|
21 | }
|
22 |
|
23 |
|
24 | function warn(prefix, type, _ref) {
|
25 | let [added, removed] = _ref;
|
26 |
|
27 | if (added.length || removed.length) {
|
28 | l.warn(`api.${prefix}: Found${logLength('added', added, removed)}${logLength('removed', removed)} ${type}:${logValues('added', added)}${logValues('removed', removed)}`);
|
29 | }
|
30 | }
|
31 |
|
32 | function findSectionExcludes(a, b) {
|
33 | return a.filter(s => !b.includes(s));
|
34 | }
|
35 |
|
36 | function findSectionIncludes(a, b) {
|
37 | return a.filter(s => b.includes(s));
|
38 | }
|
39 |
|
40 | function extractSections(src, dst) {
|
41 | const srcSections = Object.keys(src);
|
42 | const dstSections = Object.keys(dst);
|
43 | return [findSectionExcludes(srcSections, dstSections), findSectionExcludes(dstSections, srcSections)];
|
44 | }
|
45 |
|
46 | function findMethodExcludes(src, dst) {
|
47 | const srcSections = Object.keys(src);
|
48 | const dstSections = findSectionIncludes(Object.keys(dst), srcSections);
|
49 | const excludes = [];
|
50 |
|
51 | for (let s = 0; s < dstSections.length; s++) {
|
52 | const section = dstSections[s];
|
53 | const srcMethods = Object.keys(src[section]);
|
54 | const dstMethods = Object.keys(dst[section]);
|
55 | excludes.push(...dstMethods.filter(m => !srcMethods.includes(m)).map(m => `${section}.${m}`));
|
56 | }
|
57 |
|
58 | return excludes;
|
59 | }
|
60 |
|
61 | function extractMethods(src, dst) {
|
62 | return [findMethodExcludes(dst, src), findMethodExcludes(src, dst)];
|
63 | }
|
64 |
|
65 | function lazySection(src, dst) {
|
66 | const creator = m => src[m];
|
67 |
|
68 | const methods = Object.keys(src);
|
69 |
|
70 | for (let i = 0; i < methods.length; i++) {
|
71 | const method = methods[i];
|
72 |
|
73 |
|
74 |
|
75 | if (!Object.prototype.hasOwnProperty.call(dst, method)) {
|
76 | (0, _util.lazyMethod)(dst, method, creator);
|
77 | }
|
78 | }
|
79 | }
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | function augmentObject(prefix, src, dst) {
|
88 | let fromEmpty = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
89 | fromEmpty && (0, _util.objectClear)(dst);
|
90 |
|
91 |
|
92 |
|
93 | if (prefix && Object.keys(dst).length) {
|
94 | warn(prefix, 'modules', extractSections(src, dst));
|
95 | warn(prefix, 'calls', extractMethods(src, dst));
|
96 | }
|
97 |
|
98 | const sections = Object.keys(src);
|
99 |
|
100 | for (let i = 0; i < sections.length; i++) {
|
101 | const section = sections[i];
|
102 |
|
103 |
|
104 | if (!dst[section]) {
|
105 | dst[section] = {};
|
106 | }
|
107 |
|
108 | lazySection(src[section], dst[section]);
|
109 | }
|
110 |
|
111 | return dst;
|
112 | } |
\ | No newline at end of file |