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