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