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