@polkadot/types-codec 9.14.2 → 10.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/abstract/Array.d.ts +1 -1
- package/abstract/Array.js +136 -155
- package/abstract/Base.d.ts +1 -1
- package/abstract/Base.js +95 -113
- package/abstract/Int.d.ts +1 -1
- package/abstract/Int.js +209 -220
- package/abstract/Object.d.ts +1 -1
- package/abstract/Object.js +29 -45
- package/abstract/index.d.ts +3 -3
- package/abstract/index.js +3 -6
- package/base/Compact.d.ts +1 -1
- package/base/Compact.js +149 -166
- package/base/DoNotConstruct.d.ts +1 -1
- package/base/DoNotConstruct.js +62 -65
- package/base/Enum.d.ts +1 -1
- package/base/Enum.js +347 -383
- package/base/Int.d.ts +2 -2
- package/base/Int.js +14 -18
- package/base/Null.d.ts +1 -1
- package/base/Null.js +68 -82
- package/base/Option.d.ts +1 -1
- package/base/Option.js +217 -227
- package/base/Result.d.ts +2 -2
- package/base/Result.js +54 -67
- package/base/Tuple.d.ts +2 -2
- package/base/Tuple.js +100 -102
- package/base/UInt.d.ts +2 -2
- package/base/UInt.js +11 -15
- package/base/Vec.d.ts +2 -2
- package/base/Vec.js +88 -90
- package/base/VecAny.d.ts +2 -2
- package/base/VecAny.js +8 -12
- package/base/VecFixed.d.ts +2 -2
- package/base/VecFixed.js +63 -71
- package/base/index.d.ts +12 -12
- package/base/index.js +12 -15
- package/bundle.d.ts +7 -7
- package/bundle.js +7 -10
- package/cjs/abstract/Array.js +138 -159
- package/cjs/abstract/Base.js +96 -117
- package/cjs/abstract/Int.js +215 -238
- package/cjs/abstract/Object.js +30 -49
- package/cjs/abstract/index.js +8 -25
- package/cjs/base/Compact.js +151 -173
- package/cjs/base/DoNotConstruct.js +63 -70
- package/cjs/base/Enum.js +350 -396
- package/cjs/base/Int.js +16 -24
- package/cjs/base/Null.js +70 -86
- package/cjs/base/Option.js +220 -234
- package/cjs/base/Result.js +56 -71
- package/cjs/base/Tuple.js +103 -109
- package/cjs/base/UInt.js +13 -19
- package/cjs/base/Vec.js +94 -101
- package/cjs/base/VecAny.js +10 -16
- package/cjs/base/VecFixed.js +66 -79
- package/cjs/base/index.js +26 -88
- package/cjs/bundle.js +11 -86
- package/cjs/detectOther.js +2 -10
- package/cjs/detectPackage.js +6 -11
- package/cjs/extended/BTreeMap.js +10 -16
- package/cjs/extended/BTreeSet.js +162 -178
- package/cjs/extended/BitVec.js +70 -77
- package/cjs/extended/Bytes.js +59 -62
- package/cjs/extended/HashMap.js +10 -16
- package/cjs/extended/Linkage.js +50 -60
- package/cjs/extended/Map.js +176 -181
- package/cjs/extended/OptionBool.js +65 -68
- package/cjs/extended/Range.js +37 -46
- package/cjs/extended/RangeInclusive.js +13 -21
- package/cjs/extended/Type.js +14 -22
- package/cjs/extended/U8aFixed.js +33 -43
- package/cjs/extended/WrapperKeepOpaque.js +99 -99
- package/cjs/extended/WrapperOpaque.js +19 -28
- package/cjs/extended/index.js +31 -108
- package/cjs/index.js +4 -16
- package/cjs/native/Bool.js +101 -116
- package/cjs/native/Date.js +125 -151
- package/cjs/native/Float.js +95 -114
- package/cjs/native/Json.js +102 -124
- package/cjs/native/Raw.js +128 -150
- package/cjs/native/Set.js +200 -220
- package/cjs/native/Struct.data.js +2 -9
- package/cjs/native/Struct.js +238 -251
- package/cjs/native/Text.js +137 -149
- package/cjs/native/index.js +21 -78
- package/cjs/packageInfo.js +2 -16
- package/cjs/primitive/F32.js +8 -11
- package/cjs/primitive/F64.js +8 -11
- package/cjs/primitive/I128.js +8 -11
- package/cjs/primitive/I16.js +8 -11
- package/cjs/primitive/I256.js +8 -11
- package/cjs/primitive/I32.js +8 -11
- package/cjs/primitive/I64.js +8 -11
- package/cjs/primitive/I8.js +8 -11
- package/cjs/primitive/U128.js +8 -11
- package/cjs/primitive/U16.js +8 -11
- package/cjs/primitive/U256.js +8 -11
- package/cjs/primitive/U32.js +8 -11
- package/cjs/primitive/U64.js +8 -11
- package/cjs/primitive/U8.js +8 -11
- package/cjs/primitive/USize.js +7 -13
- package/cjs/primitive/index.js +47 -199
- package/cjs/types/codec.js +2 -1
- package/cjs/types/helpers.js +2 -1
- package/cjs/types/index.js +6 -48
- package/cjs/types/interfaces.js +2 -1
- package/cjs/types/registry.js +2 -1
- package/cjs/utils/compareArray.js +12 -17
- package/cjs/utils/compareMap.js +23 -26
- package/cjs/utils/compareSet.js +17 -23
- package/cjs/utils/decodeU8a.js +63 -74
- package/cjs/utils/index.js +24 -91
- package/cjs/utils/mapToTypeMap.js +13 -18
- package/cjs/utils/sanitize.js +198 -204
- package/cjs/utils/sortValues.js +39 -49
- package/cjs/utils/typeToConstructor.js +8 -11
- package/cjs/utils/typesToMap.js +10 -16
- package/cjs/utils/util.js +6 -11
- package/detectOther.js +0 -5
- package/detectPackage.js +2 -7
- package/extended/BTreeMap.d.ts +2 -2
- package/extended/BTreeMap.js +8 -11
- package/extended/BTreeSet.d.ts +1 -1
- package/extended/BTreeSet.js +159 -172
- package/extended/BitVec.d.ts +2 -2
- package/extended/BitVec.js +67 -71
- package/extended/Bytes.d.ts +2 -2
- package/extended/Bytes.js +56 -57
- package/extended/HashMap.d.ts +2 -2
- package/extended/HashMap.js +8 -11
- package/extended/Linkage.d.ts +4 -4
- package/extended/Linkage.js +46 -51
- package/extended/Map.d.ts +1 -1
- package/extended/Map.js +173 -174
- package/extended/OptionBool.d.ts +3 -3
- package/extended/OptionBool.js +62 -62
- package/extended/Range.d.ts +2 -2
- package/extended/Range.js +35 -40
- package/extended/RangeInclusive.d.ts +2 -2
- package/extended/RangeInclusive.js +11 -16
- package/extended/Type.d.ts +2 -2
- package/extended/Type.js +12 -17
- package/extended/U8aFixed.d.ts +2 -2
- package/extended/U8aFixed.js +30 -36
- package/extended/WrapperKeepOpaque.d.ts +2 -2
- package/extended/WrapperKeepOpaque.js +96 -92
- package/extended/WrapperOpaque.d.ts +2 -2
- package/extended/WrapperOpaque.js +17 -23
- package/extended/index.d.ts +14 -14
- package/extended/index.js +14 -17
- package/index.d.ts +2 -2
- package/index.js +2 -5
- package/native/Bool.d.ts +1 -1
- package/native/Bool.js +99 -111
- package/native/Date.d.ts +1 -1
- package/native/Date.js +123 -144
- package/native/Float.d.ts +1 -1
- package/native/Float.js +93 -108
- package/native/Json.d.ts +1 -1
- package/native/Json.js +100 -114
- package/native/Raw.d.ts +1 -1
- package/native/Raw.js +126 -146
- package/native/Set.d.ts +1 -1
- package/native/Set.js +197 -211
- package/native/Struct.d.ts +1 -1
- package/native/Struct.data.js +0 -3
- package/native/Struct.js +235 -242
- package/native/Text.d.ts +1 -1
- package/native/Text.js +135 -144
- package/native/index.d.ts +8 -8
- package/native/index.js +8 -11
- package/package.json +8 -5
- package/packageInfo.js +1 -11
- package/primitive/F32.d.ts +2 -2
- package/primitive/F32.js +6 -7
- package/primitive/F64.d.ts +2 -2
- package/primitive/F64.js +6 -7
- package/primitive/I128.d.ts +2 -2
- package/primitive/I128.js +6 -7
- package/primitive/I16.d.ts +2 -2
- package/primitive/I16.js +6 -7
- package/primitive/I256.d.ts +2 -2
- package/primitive/I256.js +6 -7
- package/primitive/I32.d.ts +2 -2
- package/primitive/I32.js +6 -7
- package/primitive/I64.d.ts +2 -2
- package/primitive/I64.js +6 -7
- package/primitive/I8.d.ts +2 -2
- package/primitive/I8.js +6 -7
- package/primitive/U128.d.ts +2 -2
- package/primitive/U128.js +6 -7
- package/primitive/U16.d.ts +2 -2
- package/primitive/U16.js +6 -7
- package/primitive/U256.d.ts +2 -2
- package/primitive/U256.js +6 -7
- package/primitive/U32.d.ts +2 -2
- package/primitive/U32.js +6 -7
- package/primitive/U64.d.ts +2 -2
- package/primitive/U64.js +6 -7
- package/primitive/U8.d.ts +2 -2
- package/primitive/U8.js +6 -7
- package/primitive/USize.d.ts +2 -2
- package/primitive/USize.js +5 -9
- package/primitive/index.d.ts +15 -15
- package/primitive/index.js +15 -18
- package/types/codec.d.ts +3 -3
- package/types/helpers.d.ts +1 -1
- package/types/index.d.ts +4 -4
- package/types/index.js +4 -7
- package/types/interfaces.d.ts +2 -2
- package/types/registry.d.ts +3 -3
- package/utils/compareArray.js +7 -11
- package/utils/compareMap.js +18 -19
- package/utils/compareSet.js +12 -16
- package/utils/decodeU8a.d.ts +1 -1
- package/utils/decodeU8a.js +54 -61
- package/utils/index.d.ts +9 -9
- package/utils/index.js +9 -12
- package/utils/mapToTypeMap.d.ts +1 -1
- package/utils/mapToTypeMap.js +9 -13
- package/utils/sanitize.d.ts +1 -1
- package/utils/sanitize.js +182 -185
- package/utils/sortValues.d.ts +1 -1
- package/utils/sortValues.js +32 -36
- package/utils/typeToConstructor.d.ts +1 -1
- package/utils/typeToConstructor.js +3 -4
- package/utils/typesToMap.d.ts +1 -1
- package/utils/typesToMap.js +5 -8
- package/utils/util.js +1 -4
package/cjs/base/Enum.js
CHANGED
|
@@ -1,145 +1,129 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
2
|
+
var _Enum_def, _Enum_entryIndex, _Enum_indexes, _Enum_isBasic, _Enum_isIndexed, _Enum_raw;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
4
|
exports.Enum = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
12
|
-
|
|
5
|
+
const tslib_1 = require("tslib");
|
|
6
|
+
const util_1 = require("@polkadot/util");
|
|
7
|
+
const index_js_1 = require("../utils/index.js");
|
|
8
|
+
const Null_js_1 = require("./Null.js");
|
|
13
9
|
function noopSetDefinition(d) {
|
|
14
|
-
|
|
10
|
+
return d;
|
|
15
11
|
}
|
|
16
12
|
function isRustEnum(def) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
13
|
+
const defValues = Object.values(def);
|
|
14
|
+
if (defValues.some((v) => (0, util_1.isNumber)(v))) {
|
|
15
|
+
if (!defValues.every((v) => (0, util_1.isNumber)(v) && v >= 0 && v <= 255)) {
|
|
16
|
+
throw new Error('Invalid number-indexed enum definition');
|
|
17
|
+
}
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
return true;
|
|
25
21
|
}
|
|
26
22
|
function extractDef(registry, _def) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
Type: _Null.Null,
|
|
60
|
-
index
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
isBasic = true;
|
|
64
|
-
isIndexed = true;
|
|
65
|
-
}
|
|
66
|
-
return {
|
|
67
|
-
def,
|
|
68
|
-
isBasic,
|
|
69
|
-
isIndexed
|
|
70
|
-
};
|
|
23
|
+
const def = {};
|
|
24
|
+
let isBasic;
|
|
25
|
+
let isIndexed;
|
|
26
|
+
if (Array.isArray(_def)) {
|
|
27
|
+
for (let i = 0; i < _def.length; i++) {
|
|
28
|
+
def[_def[i]] = { Type: Null_js_1.Null, index: i };
|
|
29
|
+
}
|
|
30
|
+
isBasic = true;
|
|
31
|
+
isIndexed = false;
|
|
32
|
+
}
|
|
33
|
+
else if (isRustEnum(_def)) {
|
|
34
|
+
const [Types, keys] = (0, index_js_1.mapToTypeMap)(registry, _def);
|
|
35
|
+
for (let i = 0; i < keys.length; i++) {
|
|
36
|
+
def[keys[i]] = { Type: Types[i], index: i };
|
|
37
|
+
}
|
|
38
|
+
isBasic = !Object.values(def).some(({ Type }) => Type !== Null_js_1.Null);
|
|
39
|
+
isIndexed = false;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
const entries = Object.entries(_def);
|
|
43
|
+
for (let i = 0; i < entries.length; i++) {
|
|
44
|
+
const [key, index] = entries[i];
|
|
45
|
+
def[key] = { Type: Null_js_1.Null, index };
|
|
46
|
+
}
|
|
47
|
+
isBasic = true;
|
|
48
|
+
isIndexed = true;
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
def,
|
|
52
|
+
isBasic,
|
|
53
|
+
isIndexed
|
|
54
|
+
};
|
|
71
55
|
}
|
|
72
56
|
function getEntryType(def, checkIdx) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
throw new Error(`Unable to create Enum via index ${checkIdx}, in ${Object.keys(def).join(', ')}`);
|
|
57
|
+
const values = Object.values(def);
|
|
58
|
+
for (let i = 0; i < values.length; i++) {
|
|
59
|
+
const { Type, index } = values[i];
|
|
60
|
+
if (index === checkIdx) {
|
|
61
|
+
return Type;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
throw new Error(`Unable to create Enum via index ${checkIdx}, in ${Object.keys(def).join(', ')}`);
|
|
84
65
|
}
|
|
85
66
|
function createFromU8a(registry, def, index, value) {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
67
|
+
const Type = getEntryType(def, index);
|
|
68
|
+
return {
|
|
69
|
+
index,
|
|
70
|
+
value: new Type(registry, value)
|
|
71
|
+
};
|
|
91
72
|
}
|
|
92
|
-
function createFromValue(registry, def) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
73
|
+
function createFromValue(registry, def, index = 0, value) {
|
|
74
|
+
const Type = getEntryType(def, index);
|
|
75
|
+
return {
|
|
76
|
+
index,
|
|
77
|
+
value: value instanceof Type
|
|
78
|
+
? value
|
|
79
|
+
: new Type(registry, value)
|
|
80
|
+
};
|
|
100
81
|
}
|
|
101
82
|
function decodeFromJSON(registry, def, key, value) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
83
|
+
// JSON comes in the form of { "<type (camelCase)>": "<value for type>" }, here we
|
|
84
|
+
// additionally force to lower to ensure forward compat
|
|
85
|
+
const keys = Object.keys(def).map((k) => k.toLowerCase());
|
|
86
|
+
const keyLower = key.toLowerCase();
|
|
87
|
+
const index = keys.indexOf(keyLower);
|
|
88
|
+
if (index === -1) {
|
|
89
|
+
throw new Error(`Cannot map Enum JSON, unable to find '${key}' in ${keys.join(', ')}`);
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
return createFromValue(registry, def, Object.values(def)[index].index, value);
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
throw new Error(`Enum(${key}):: ${error.message}`);
|
|
96
|
+
}
|
|
115
97
|
}
|
|
116
98
|
function decodeEnum(registry, def, value, index) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
99
|
+
// NOTE We check the index path first, before looking at values - this allows treating
|
|
100
|
+
// the optional indexes before anything else, more-specific > less-specific
|
|
101
|
+
if ((0, util_1.isNumber)(index)) {
|
|
102
|
+
return createFromValue(registry, def, index, value);
|
|
103
|
+
}
|
|
104
|
+
else if ((0, util_1.isU8a)(value) || (0, util_1.isHex)(value)) {
|
|
105
|
+
const u8a = (0, util_1.u8aToU8a)(value);
|
|
106
|
+
// nested, we don't want to match isObject below
|
|
107
|
+
if (u8a.length) {
|
|
108
|
+
return createFromU8a(registry, def, u8a[0], u8a.subarray(1));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else if (value instanceof Enum) {
|
|
112
|
+
return createFromValue(registry, def, value.index, value.value);
|
|
113
|
+
}
|
|
114
|
+
else if ((0, util_1.isNumber)(value)) {
|
|
115
|
+
return createFromValue(registry, def, value);
|
|
116
|
+
}
|
|
117
|
+
else if ((0, util_1.isString)(value)) {
|
|
118
|
+
return decodeFromJSON(registry, def, value.toString());
|
|
119
|
+
}
|
|
120
|
+
else if ((0, util_1.isObject)(value)) {
|
|
121
|
+
const key = Object.keys(value)[0];
|
|
122
|
+
return decodeFromJSON(registry, def, key, value[key]);
|
|
123
|
+
}
|
|
124
|
+
// Worst-case scenario, return the first with default
|
|
125
|
+
return createFromValue(registry, def, Object.values(def)[0].index);
|
|
141
126
|
}
|
|
142
|
-
|
|
143
127
|
/**
|
|
144
128
|
* @name Enum
|
|
145
129
|
* @description
|
|
@@ -147,276 +131,246 @@ function decodeEnum(registry, def, value, index) {
|
|
|
147
131
|
* an extension to enum where the value type is determined by the actual index.
|
|
148
132
|
*/
|
|
149
133
|
class Enum {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
this.#isBasic = isBasic;
|
|
172
|
-
this.#isIndexed = isIndexed;
|
|
173
|
-
this.#indexes = Object.values(def).map(_ref2 => {
|
|
174
|
-
let {
|
|
175
|
-
index
|
|
176
|
-
} = _ref2;
|
|
177
|
-
return index;
|
|
178
|
-
});
|
|
179
|
-
this.#entryIndex = this.#indexes.indexOf(decoded.index);
|
|
180
|
-
this.#raw = decoded.value;
|
|
181
|
-
if (this.#raw.initialU8aLength) {
|
|
182
|
-
this.initialU8aLength = 1 + this.#raw.initialU8aLength;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
static with(Types) {
|
|
186
|
-
var _class;
|
|
187
|
-
let definition;
|
|
188
|
-
|
|
189
|
-
// eslint-disable-next-line no-return-assign
|
|
190
|
-
const setDefinition = d => definition = d;
|
|
191
|
-
return _class = class extends Enum {
|
|
192
|
-
constructor(registry, value, index) {
|
|
193
|
-
super(registry, Types, value, index, {
|
|
194
|
-
definition,
|
|
195
|
-
setDefinition
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
}, (() => {
|
|
199
|
-
const keys = Array.isArray(Types) ? Types : Object.keys(Types);
|
|
200
|
-
const asKeys = new Array(keys.length);
|
|
201
|
-
const isKeys = new Array(keys.length);
|
|
202
|
-
for (let i = 0; i < keys.length; i++) {
|
|
203
|
-
const name = (0, _util.stringPascalCase)(keys[i]);
|
|
204
|
-
asKeys[i] = `as${name}`;
|
|
205
|
-
isKeys[i] = `is${name}`;
|
|
206
|
-
}
|
|
207
|
-
(0, _util.objectProperties)(_class.prototype, isKeys, (_, i, self) => self.type === keys[i]);
|
|
208
|
-
(0, _util.objectProperties)(_class.prototype, asKeys, (k, i, self) => {
|
|
209
|
-
if (self.type !== keys[i]) {
|
|
210
|
-
throw new Error(`Cannot convert '${self.type}' via ${k}`);
|
|
134
|
+
constructor(registry, Types, value, index, { definition, setDefinition = noopSetDefinition } = {}) {
|
|
135
|
+
_Enum_def.set(this, void 0);
|
|
136
|
+
_Enum_entryIndex.set(this, void 0);
|
|
137
|
+
_Enum_indexes.set(this, void 0);
|
|
138
|
+
_Enum_isBasic.set(this, void 0);
|
|
139
|
+
_Enum_isIndexed.set(this, void 0);
|
|
140
|
+
_Enum_raw.set(this, void 0);
|
|
141
|
+
const { def, isBasic, isIndexed } = definition || setDefinition(extractDef(registry, Types));
|
|
142
|
+
// shortcut isU8a as used in SCALE decoding
|
|
143
|
+
const decoded = (0, util_1.isU8a)(value) && value.length && !(0, util_1.isNumber)(index)
|
|
144
|
+
? createFromU8a(registry, def, value[0], value.subarray(1))
|
|
145
|
+
: decodeEnum(registry, def, value, index);
|
|
146
|
+
this.registry = registry;
|
|
147
|
+
tslib_1.__classPrivateFieldSet(this, _Enum_def, def, "f");
|
|
148
|
+
tslib_1.__classPrivateFieldSet(this, _Enum_isBasic, isBasic, "f");
|
|
149
|
+
tslib_1.__classPrivateFieldSet(this, _Enum_isIndexed, isIndexed, "f");
|
|
150
|
+
tslib_1.__classPrivateFieldSet(this, _Enum_indexes, Object.values(def).map(({ index }) => index), "f");
|
|
151
|
+
tslib_1.__classPrivateFieldSet(this, _Enum_entryIndex, tslib_1.__classPrivateFieldGet(this, _Enum_indexes, "f").indexOf(decoded.index), "f");
|
|
152
|
+
tslib_1.__classPrivateFieldSet(this, _Enum_raw, decoded.value, "f");
|
|
153
|
+
if (tslib_1.__classPrivateFieldGet(this, _Enum_raw, "f").initialU8aLength) {
|
|
154
|
+
this.initialU8aLength = 1 + tslib_1.__classPrivateFieldGet(this, _Enum_raw, "f").initialU8aLength;
|
|
211
155
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
156
|
+
}
|
|
157
|
+
static with(Types) {
|
|
158
|
+
var _a;
|
|
159
|
+
let definition;
|
|
160
|
+
// eslint-disable-next-line no-return-assign
|
|
161
|
+
const setDefinition = (d) => definition = d;
|
|
162
|
+
return _a = class extends Enum {
|
|
163
|
+
constructor(registry, value, index) {
|
|
164
|
+
super(registry, Types, value, index, { definition, setDefinition });
|
|
165
|
+
}
|
|
166
|
+
},
|
|
167
|
+
(() => {
|
|
168
|
+
const keys = Array.isArray(Types)
|
|
169
|
+
? Types
|
|
170
|
+
: Object.keys(Types);
|
|
171
|
+
const asKeys = new Array(keys.length);
|
|
172
|
+
const isKeys = new Array(keys.length);
|
|
173
|
+
for (let i = 0; i < keys.length; i++) {
|
|
174
|
+
const name = (0, util_1.stringPascalCase)(keys[i]);
|
|
175
|
+
asKeys[i] = `as${name}`;
|
|
176
|
+
isKeys[i] = `is${name}`;
|
|
177
|
+
}
|
|
178
|
+
(0, util_1.objectProperties)(_a.prototype, isKeys, (_, i, self) => self.type === keys[i]);
|
|
179
|
+
(0, util_1.objectProperties)(_a.prototype, asKeys, (k, i, self) => {
|
|
180
|
+
if (self.type !== keys[i]) {
|
|
181
|
+
throw new Error(`Cannot convert '${self.type}' via ${k}`);
|
|
182
|
+
}
|
|
183
|
+
return self.value;
|
|
184
|
+
});
|
|
185
|
+
})(),
|
|
186
|
+
_a;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* @description The length of the value when encoded as a Uint8Array
|
|
190
|
+
*/
|
|
191
|
+
get encodedLength() {
|
|
192
|
+
return 1 + tslib_1.__classPrivateFieldGet(this, _Enum_raw, "f").encodedLength;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* @description returns a hash of the contents
|
|
196
|
+
*/
|
|
197
|
+
get hash() {
|
|
198
|
+
return this.registry.hash(this.toU8a());
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* @description The index of the enum value
|
|
202
|
+
*/
|
|
203
|
+
get index() {
|
|
204
|
+
return tslib_1.__classPrivateFieldGet(this, _Enum_indexes, "f")[tslib_1.__classPrivateFieldGet(this, _Enum_entryIndex, "f")];
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* @description The value of the enum
|
|
208
|
+
*/
|
|
209
|
+
get inner() {
|
|
210
|
+
return tslib_1.__classPrivateFieldGet(this, _Enum_raw, "f");
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* @description true if this is a basic enum (no values)
|
|
214
|
+
*/
|
|
215
|
+
get isBasic() {
|
|
216
|
+
return tslib_1.__classPrivateFieldGet(this, _Enum_isBasic, "f");
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* @description Checks if the value is an empty value
|
|
220
|
+
*/
|
|
221
|
+
get isEmpty() {
|
|
222
|
+
return tslib_1.__classPrivateFieldGet(this, _Enum_raw, "f").isEmpty;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* @description Checks if the Enum points to a [[Null]] type
|
|
226
|
+
*/
|
|
227
|
+
get isNone() {
|
|
228
|
+
return tslib_1.__classPrivateFieldGet(this, _Enum_raw, "f") instanceof Null_js_1.Null;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* @description The available keys for this enum
|
|
232
|
+
*/
|
|
233
|
+
get defIndexes() {
|
|
234
|
+
return tslib_1.__classPrivateFieldGet(this, _Enum_indexes, "f");
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* @description The available keys for this enum
|
|
238
|
+
*/
|
|
239
|
+
get defKeys() {
|
|
240
|
+
return Object.keys(tslib_1.__classPrivateFieldGet(this, _Enum_def, "f"));
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* @description The name of the type this enum value represents
|
|
244
|
+
*/
|
|
245
|
+
get type() {
|
|
246
|
+
return this.defKeys[tslib_1.__classPrivateFieldGet(this, _Enum_entryIndex, "f")];
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* @description The value of the enum
|
|
250
|
+
*/
|
|
251
|
+
get value() {
|
|
252
|
+
return tslib_1.__classPrivateFieldGet(this, _Enum_raw, "f");
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* @description Compares the value of the input to see if there is a match
|
|
256
|
+
*/
|
|
257
|
+
eq(other) {
|
|
258
|
+
// cater for the case where we only pass the enum index
|
|
259
|
+
if ((0, util_1.isU8a)(other)) {
|
|
260
|
+
return !this.toU8a().some((entry, index) => entry !== other[index]);
|
|
261
|
+
}
|
|
262
|
+
else if ((0, util_1.isNumber)(other)) {
|
|
263
|
+
return this.toNumber() === other;
|
|
264
|
+
}
|
|
265
|
+
else if (tslib_1.__classPrivateFieldGet(this, _Enum_isBasic, "f") && (0, util_1.isString)(other)) {
|
|
266
|
+
return this.type === other;
|
|
267
|
+
}
|
|
268
|
+
else if ((0, util_1.isHex)(other)) {
|
|
269
|
+
return this.toHex() === other;
|
|
270
|
+
}
|
|
271
|
+
else if (other instanceof Enum) {
|
|
272
|
+
return this.index === other.index && this.value.eq(other.value);
|
|
273
|
+
}
|
|
274
|
+
else if ((0, util_1.isObject)(other)) {
|
|
275
|
+
return this.value.eq(other[this.type]);
|
|
276
|
+
}
|
|
277
|
+
// compare the actual wrapper value
|
|
278
|
+
return this.value.eq(other);
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* @description Returns a breakdown of the hex encoding for this Codec
|
|
282
|
+
*/
|
|
283
|
+
inspect() {
|
|
284
|
+
if (tslib_1.__classPrivateFieldGet(this, _Enum_isBasic, "f")) {
|
|
285
|
+
return { outer: [new Uint8Array([this.index])] };
|
|
286
|
+
}
|
|
287
|
+
const { inner, outer = [] } = tslib_1.__classPrivateFieldGet(this, _Enum_raw, "f").inspect();
|
|
288
|
+
return {
|
|
289
|
+
inner,
|
|
290
|
+
outer: [new Uint8Array([this.index]), ...outer]
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* @description Returns a hex string representation of the value
|
|
295
|
+
*/
|
|
296
|
+
toHex() {
|
|
297
|
+
return (0, util_1.u8aToHex)(this.toU8a());
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
301
|
+
*/
|
|
302
|
+
toHuman(isExtended) {
|
|
303
|
+
return tslib_1.__classPrivateFieldGet(this, _Enum_isBasic, "f") || this.isNone
|
|
304
|
+
? this.type
|
|
305
|
+
: { [this.type]: tslib_1.__classPrivateFieldGet(this, _Enum_raw, "f").toHuman(isExtended) };
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
309
|
+
*/
|
|
310
|
+
toJSON() {
|
|
311
|
+
return tslib_1.__classPrivateFieldGet(this, _Enum_isBasic, "f")
|
|
312
|
+
? this.type
|
|
313
|
+
: { [(0, util_1.stringCamelCase)(this.type)]: tslib_1.__classPrivateFieldGet(this, _Enum_raw, "f").toJSON() };
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* @description Returns the number representation for the value
|
|
317
|
+
*/
|
|
318
|
+
toNumber() {
|
|
319
|
+
return this.index;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* @description Converts the value in a best-fit primitive form
|
|
323
|
+
*/
|
|
324
|
+
toPrimitive() {
|
|
325
|
+
return tslib_1.__classPrivateFieldGet(this, _Enum_isBasic, "f")
|
|
326
|
+
? this.type
|
|
327
|
+
: { [(0, util_1.stringCamelCase)(this.type)]: tslib_1.__classPrivateFieldGet(this, _Enum_raw, "f").toPrimitive() };
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* @description Returns a raw struct representation of the enum types
|
|
331
|
+
*/
|
|
332
|
+
_toRawStruct() {
|
|
333
|
+
if (tslib_1.__classPrivateFieldGet(this, _Enum_isBasic, "f")) {
|
|
334
|
+
return tslib_1.__classPrivateFieldGet(this, _Enum_isIndexed, "f")
|
|
335
|
+
? this.defKeys.reduce((out, key, index) => {
|
|
336
|
+
out[key] = tslib_1.__classPrivateFieldGet(this, _Enum_indexes, "f")[index];
|
|
337
|
+
return out;
|
|
338
|
+
}, {})
|
|
339
|
+
: this.defKeys;
|
|
340
|
+
}
|
|
341
|
+
const entries = Object.entries(tslib_1.__classPrivateFieldGet(this, _Enum_def, "f"));
|
|
342
|
+
return (0, index_js_1.typesToMap)(this.registry, entries.reduce((out, [key, { Type }], i) => {
|
|
343
|
+
out[0][i] = Type;
|
|
344
|
+
out[1][i] = key;
|
|
345
|
+
return out;
|
|
346
|
+
}, [new Array(entries.length), new Array(entries.length)]));
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* @description Returns the base runtime type name for this instance
|
|
350
|
+
*/
|
|
351
|
+
toRawType() {
|
|
352
|
+
return (0, util_1.stringify)({ _enum: this._toRawStruct() });
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* @description Returns the string representation of the value
|
|
356
|
+
*/
|
|
357
|
+
toString() {
|
|
358
|
+
return this.isNone
|
|
359
|
+
? this.type
|
|
360
|
+
: (0, util_1.stringify)(this.toJSON());
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
364
|
+
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
|
365
|
+
*/
|
|
366
|
+
toU8a(isBare) {
|
|
367
|
+
return isBare
|
|
368
|
+
? tslib_1.__classPrivateFieldGet(this, _Enum_raw, "f").toU8a(isBare)
|
|
369
|
+
: (0, util_1.u8aConcatStrict)([
|
|
370
|
+
new Uint8Array([this.index]),
|
|
371
|
+
tslib_1.__classPrivateFieldGet(this, _Enum_raw, "f").toU8a(isBare)
|
|
372
|
+
]);
|
|
373
|
+
}
|
|
421
374
|
}
|
|
422
|
-
exports.Enum = Enum;
|
|
375
|
+
exports.Enum = Enum;
|
|
376
|
+
_Enum_def = new WeakMap(), _Enum_entryIndex = new WeakMap(), _Enum_indexes = new WeakMap(), _Enum_isBasic = new WeakMap(), _Enum_isIndexed = new WeakMap(), _Enum_raw = new WeakMap();
|