@polkadot/types-codec 9.5.2 → 9.6.2

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 (152) hide show
  1. package/abstract/Array.js +15 -44
  2. package/abstract/Base.js +12 -28
  3. package/abstract/BigInt.js +3 -2
  4. package/abstract/Int.js +25 -52
  5. package/abstract/index.js +1 -0
  6. package/base/Compact.js +21 -45
  7. package/base/DoNotConstruct.js +3 -19
  8. package/base/Enum.js +37 -88
  9. package/base/Int.js +2 -5
  10. package/base/Null.js +12 -23
  11. package/base/Option.js +23 -59
  12. package/base/Result.js +8 -19
  13. package/base/Tuple.js +12 -29
  14. package/base/UInt.js +2 -4
  15. package/base/Vec.js +7 -25
  16. package/base/VecAny.js +2 -2
  17. package/base/VecFixed.js +8 -20
  18. package/base/index.js +1 -0
  19. package/bundle.js +1 -0
  20. package/cjs/abstract/Array.js +13 -47
  21. package/cjs/abstract/Base.js +12 -30
  22. package/cjs/abstract/BigInt.js +1 -4
  23. package/cjs/abstract/Int.js +25 -55
  24. package/cjs/abstract/index.js +0 -3
  25. package/cjs/base/Compact.js +21 -48
  26. package/cjs/base/DoNotConstruct.js +3 -21
  27. package/cjs/base/Enum.js +34 -92
  28. package/cjs/base/Int.js +0 -7
  29. package/cjs/base/Null.js +10 -25
  30. package/cjs/base/Option.js +23 -63
  31. package/cjs/base/Result.js +6 -21
  32. package/cjs/base/Tuple.js +12 -33
  33. package/cjs/base/UInt.js +0 -6
  34. package/cjs/base/Vec.js +7 -31
  35. package/cjs/base/VecAny.js +0 -4
  36. package/cjs/base/VecFixed.js +8 -25
  37. package/cjs/base/index.js +0 -12
  38. package/cjs/bundle.js +0 -13
  39. package/cjs/detectPackage.js +2 -4
  40. package/cjs/extended/BTreeMap.js +1 -5
  41. package/cjs/extended/BTreeSet.js +17 -57
  42. package/cjs/extended/BitVec.js +11 -25
  43. package/cjs/extended/Bytes.js +9 -20
  44. package/cjs/extended/HashMap.js +1 -5
  45. package/cjs/extended/Linkage.js +5 -20
  46. package/cjs/extended/Map.js +16 -62
  47. package/cjs/extended/OptionBool.js +8 -21
  48. package/cjs/extended/Range.js +3 -13
  49. package/cjs/extended/RangeInclusive.js +1 -6
  50. package/cjs/extended/Type.js +1 -7
  51. package/cjs/extended/U8aFixed.js +2 -15
  52. package/cjs/extended/WrapperKeepOpaque.js +8 -28
  53. package/cjs/extended/WrapperOpaque.js +2 -8
  54. package/cjs/extended/index.js +0 -14
  55. package/cjs/index.js +0 -3
  56. package/cjs/native/Bool.js +14 -32
  57. package/cjs/native/Date.js +18 -40
  58. package/cjs/native/Float.js +11 -29
  59. package/cjs/native/Json.js +14 -33
  60. package/cjs/native/Raw.js +18 -41
  61. package/cjs/native/Set.js +21 -67
  62. package/cjs/native/Struct.data.js +1 -0
  63. package/cjs/native/Struct.js +33 -83
  64. package/cjs/native/Text.js +20 -44
  65. package/cjs/native/index.js +0 -8
  66. package/cjs/packageInfo.js +3 -1
  67. package/cjs/primitive/F32.js +0 -3
  68. package/cjs/primitive/F64.js +0 -3
  69. package/cjs/primitive/I128.js +0 -3
  70. package/cjs/primitive/I16.js +0 -3
  71. package/cjs/primitive/I256.js +0 -3
  72. package/cjs/primitive/I32.js +0 -3
  73. package/cjs/primitive/I64.js +0 -3
  74. package/cjs/primitive/I8.js +0 -3
  75. package/cjs/primitive/U128.js +0 -3
  76. package/cjs/primitive/U16.js +0 -3
  77. package/cjs/primitive/U256.js +0 -3
  78. package/cjs/primitive/U32.js +0 -3
  79. package/cjs/primitive/U64.js +0 -3
  80. package/cjs/primitive/U8.js +0 -3
  81. package/cjs/primitive/USize.js +0 -4
  82. package/cjs/primitive/index.js +0 -15
  83. package/cjs/types/index.js +0 -8
  84. package/cjs/utils/compareArray.js +1 -4
  85. package/cjs/utils/compareMap.js +4 -9
  86. package/cjs/utils/compareSet.js +4 -6
  87. package/cjs/utils/decodeU8a.js +10 -18
  88. package/cjs/utils/index.js +0 -9
  89. package/cjs/utils/mapToTypeMap.js +0 -4
  90. package/cjs/utils/sanitize.js +66 -71
  91. package/cjs/utils/sortValues.js +5 -18
  92. package/cjs/utils/typeToConstructor.js +1 -2
  93. package/cjs/utils/typesToMap.js +1 -3
  94. package/cjs/utils/util.js +1 -2
  95. package/detectOther.js +2 -0
  96. package/detectPackage.js +2 -0
  97. package/extended/BTreeMap.js +1 -2
  98. package/extended/BTreeSet.js +17 -53
  99. package/extended/BitVec.js +13 -22
  100. package/extended/Bytes.js +11 -17
  101. package/extended/HashMap.js +1 -2
  102. package/extended/Linkage.js +5 -12
  103. package/extended/Map.js +16 -54
  104. package/extended/OptionBool.js +8 -17
  105. package/extended/Range.js +4 -11
  106. package/extended/RangeInclusive.js +1 -3
  107. package/extended/Type.js +3 -4
  108. package/extended/U8aFixed.js +4 -12
  109. package/extended/WrapperKeepOpaque.js +8 -23
  110. package/extended/WrapperOpaque.js +2 -5
  111. package/extended/index.js +1 -0
  112. package/index.js +1 -0
  113. package/native/Bool.js +16 -30
  114. package/native/Date.js +18 -37
  115. package/native/Float.js +12 -27
  116. package/native/Json.js +16 -30
  117. package/native/Raw.js +20 -39
  118. package/native/Set.js +21 -63
  119. package/native/Struct.data.js +1 -0
  120. package/native/Struct.js +33 -80
  121. package/native/Text.js +20 -40
  122. package/native/index.js +1 -0
  123. package/package.json +2 -2
  124. package/packageInfo.js +3 -1
  125. package/primitive/F32.js +2 -1
  126. package/primitive/F64.js +2 -1
  127. package/primitive/I128.js +2 -1
  128. package/primitive/I16.js +2 -1
  129. package/primitive/I256.js +2 -1
  130. package/primitive/I32.js +2 -1
  131. package/primitive/I64.js +2 -1
  132. package/primitive/I8.js +2 -1
  133. package/primitive/U128.js +2 -1
  134. package/primitive/U16.js +2 -1
  135. package/primitive/U256.js +2 -1
  136. package/primitive/U32.js +2 -1
  137. package/primitive/U64.js +2 -1
  138. package/primitive/U8.js +2 -1
  139. package/primitive/USize.js +2 -2
  140. package/primitive/index.js +1 -0
  141. package/types/index.js +1 -0
  142. package/utils/compareArray.js +4 -3
  143. package/utils/compareMap.js +4 -7
  144. package/utils/compareSet.js +4 -5
  145. package/utils/decodeU8a.js +12 -15
  146. package/utils/index.js +1 -0
  147. package/utils/mapToTypeMap.js +2 -3
  148. package/utils/sanitize.js +66 -60
  149. package/utils/sortValues.js +6 -15
  150. package/utils/typeToConstructor.js +1 -0
  151. package/utils/typesToMap.js +1 -2
  152. package/utils/util.js +1 -0
package/base/Option.js CHANGED
@@ -1,13 +1,12 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { isCodec, isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
4
5
  import { typeToConstructor } from "../utils/index.js";
5
6
  import { Null } from "./Null.js";
6
-
7
7
  function noopSetDefinition(d) {
8
8
  return d;
9
9
  }
10
-
11
10
  class None extends Null {
12
11
  /**
13
12
  * @description Returns the base runtime type name for this instance
@@ -15,11 +14,9 @@ class None extends Null {
15
14
  toRawType() {
16
15
  return 'None';
17
16
  }
18
-
19
17
  }
20
- /** @internal */
21
-
22
18
 
19
+ /** @internal */
23
20
  function decodeOption(registry, Type, value) {
24
21
  if (value instanceof Type) {
25
22
  // don't re-create, use as it (which also caters for derived types)
@@ -31,9 +28,9 @@ function decodeOption(registry, Type, value) {
31
28
  } else if (value.isNone) {
32
29
  // internal is None, we are also none
33
30
  return new None(registry);
34
- } // convert the actual value into known
35
-
31
+ }
36
32
 
33
+ // convert the actual value into known
37
34
  return new Type(registry, value.value);
38
35
  } else if (isNull(value) || isUndefined(value) || value === '0x' || value instanceof None) {
39
36
  // anyhting empty we pass as-is
@@ -43,9 +40,9 @@ function decodeOption(registry, Type, value) {
43
40
  // may be an instance of it, so Type and Option checks go in first
44
41
  return !value.length || value[0] === 0 ? new None(registry) : new Type(registry, value.subarray(1));
45
42
  }
46
-
47
43
  return new Type(registry, value);
48
44
  }
45
+
49
46
  /**
50
47
  * @name Option
51
48
  * @description
@@ -54,12 +51,9 @@ function decodeOption(registry, Type, value) {
54
51
  * implements that - decodes, checks for optionality and wraps the required structure
55
52
  * with a value if/as required/found.
56
53
  */
57
-
58
-
59
54
  export class Option {
60
55
  #Type;
61
56
  #raw;
62
-
63
57
  constructor(registry, typeName, value, {
64
58
  definition,
65
59
  setDefinition = noopSetDefinition
@@ -69,20 +63,16 @@ export class Option {
69
63
  this.registry = registry;
70
64
  this.#Type = Type;
71
65
  this.#raw = decoded;
72
-
73
66
  if (decoded && decoded.initialU8aLength) {
74
67
  this.initialU8aLength = 1 + decoded.initialU8aLength;
75
68
  }
76
69
  }
77
-
78
70
  static with(Type) {
79
71
  let definition;
80
-
81
72
  const setDefinition = d => {
82
73
  definition = d;
83
74
  return d;
84
75
  };
85
-
86
76
  return class extends Option {
87
77
  constructor(registry, value) {
88
78
  super(registry, Type, value, {
@@ -90,82 +80,71 @@ export class Option {
90
80
  setDefinition
91
81
  });
92
82
  }
93
-
94
83
  };
95
84
  }
85
+
96
86
  /**
97
87
  * @description The length of the value when encoded as a Uint8Array
98
88
  */
99
-
100
-
101
89
  get encodedLength() {
102
90
  // boolean byte (has value, doesn't have) along with wrapped length
103
91
  return 1 + this.#raw.encodedLength;
104
92
  }
93
+
105
94
  /**
106
95
  * @description returns a hash of the contents
107
96
  */
108
-
109
-
110
97
  get hash() {
111
98
  return this.registry.hash(this.toU8a());
112
99
  }
100
+
113
101
  /**
114
102
  * @description Checks if the Option has no value
115
103
  */
116
-
117
-
118
104
  get isEmpty() {
119
105
  return this.isNone;
120
106
  }
107
+
121
108
  /**
122
109
  * @description Checks if the Option has no value
123
110
  */
124
-
125
-
126
111
  get isNone() {
127
112
  return this.#raw instanceof None;
128
113
  }
114
+
129
115
  /**
130
116
  * @description Checks if the Option has a value
131
117
  */
132
-
133
-
134
118
  get isSome() {
135
119
  return !this.isNone;
136
120
  }
121
+
137
122
  /**
138
123
  * @description The actual value for the Option
139
124
  */
140
-
141
-
142
125
  get value() {
143
126
  return this.#raw;
144
127
  }
128
+
145
129
  /**
146
130
  * @description Compares the value of the input to see if there is a match
147
131
  */
148
-
149
-
150
132
  eq(other) {
151
133
  if (other instanceof Option) {
152
134
  return this.isSome === other.isSome && this.value.eq(other.value);
153
135
  }
154
-
155
136
  return this.value.eq(other);
156
137
  }
138
+
157
139
  /**
158
140
  * @description Returns a breakdown of the hex encoding for this Codec
159
141
  */
160
-
161
-
162
142
  inspect() {
163
143
  if (this.isNone) {
164
144
  return {
165
145
  outer: [new Uint8Array([0])]
166
146
  };
167
147
  }
168
-
169
148
  const {
170
149
  inner,
171
150
  outer = []
@@ -175,106 +154,91 @@ export class Option {
175
154
  outer: [new Uint8Array([1]), ...outer]
176
155
  };
177
156
  }
157
+
178
158
  /**
179
159
  * @description Returns a hex string representation of the value
180
160
  */
181
-
182
-
183
161
  toHex() {
184
162
  // This attempts to align with the JSON encoding - actually in this case
185
163
  // the isSome value is correct, however the `isNone` may be problematic
186
164
  return this.isNone ? '0x' : u8aToHex(this.toU8a().subarray(1));
187
165
  }
166
+
188
167
  /**
189
168
  * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
190
169
  */
191
-
192
-
193
170
  toHuman(isExtended) {
194
171
  return this.#raw.toHuman(isExtended);
195
172
  }
173
+
196
174
  /**
197
175
  * @description Converts the Object to JSON, typically used for RPC transfers
198
176
  */
199
-
200
-
201
177
  toJSON() {
202
178
  return this.isNone ? null : this.#raw.toJSON();
203
179
  }
180
+
204
181
  /**
205
182
  * @description Converts the value in a best-fit primitive form
206
183
  */
207
-
208
-
209
184
  toPrimitive() {
210
185
  return this.isNone ? null : this.#raw.toPrimitive();
211
186
  }
187
+
212
188
  /**
213
189
  * @description Returns the base runtime type name for this instance
214
190
  */
215
-
216
-
217
191
  toRawType(isBare) {
218
192
  const wrapped = this.registry.getClassName(this.#Type) || new this.#Type(this.registry).toRawType();
219
193
  return isBare ? wrapped : `Option<${wrapped}>`;
220
194
  }
195
+
221
196
  /**
222
197
  * @description Returns the string representation of the value
223
198
  */
224
-
225
-
226
199
  toString() {
227
200
  return this.#raw.toString();
228
201
  }
202
+
229
203
  /**
230
204
  * @description Encodes the value as a Uint8Array as per the SCALE specifications
231
205
  * @param isBare true when the value has none of the type-specific prefixes (internal)
232
206
  */
233
-
234
-
235
207
  toU8a(isBare) {
236
208
  if (isBare) {
237
209
  return this.#raw.toU8a(true);
238
210
  }
239
-
240
211
  const u8a = new Uint8Array(this.encodedLength);
241
-
242
212
  if (this.isSome) {
243
213
  u8a.set([1]);
244
214
  u8a.set(this.#raw.toU8a(), 1);
245
215
  }
246
-
247
216
  return u8a;
248
217
  }
218
+
249
219
  /**
250
220
  * @description Returns the value that the Option represents (if available), throws if null
251
221
  */
252
-
253
-
254
222
  unwrap() {
255
223
  if (this.isNone) {
256
224
  throw new Error('Option: unwrapping a None value');
257
225
  }
258
-
259
226
  return this.#raw;
260
227
  }
228
+
261
229
  /**
262
230
  * @description Returns the value that the Option represents (if available) or defaultValue if none
263
231
  * @param defaultValue The value to return if the option isNone
264
232
  */
265
-
266
-
267
233
  unwrapOr(defaultValue) {
268
234
  return this.isSome ? this.unwrap() : defaultValue;
269
235
  }
236
+
270
237
  /**
271
238
  * @description Returns the value that the Option represents (if available) or defaultValue if none
272
239
  * @param defaultValue The value to return if the option isNone
273
240
  */
274
-
275
-
276
241
  unwrapOrDefault() {
277
242
  return this.isSome ? this.unwrap() : new this.#Type(this.registry);
278
243
  }
279
-
280
244
  }
package/base/Result.js CHANGED
@@ -1,12 +1,13 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { Enum } from "./Enum.js";
5
+
4
6
  /**
5
7
  * @name Result
6
8
  * @description
7
9
  * A Result maps to the Rust Result type, that can either wrap a success or error value
8
10
  */
9
-
10
11
  export class Result extends Enum {
11
12
  constructor(registry, Ok, Err, value) {
12
13
  // NOTE This is order-dependent, Ok (with index 0) needs to be first
@@ -16,72 +17,60 @@ export class Result extends Enum {
16
17
  Err
17
18
  }, value);
18
19
  }
19
-
20
20
  static with(Types) {
21
21
  return class extends Result {
22
22
  constructor(registry, value) {
23
23
  super(registry, Types.Ok, Types.Err, value);
24
24
  }
25
-
26
25
  };
27
26
  }
27
+
28
28
  /**
29
29
  * @description Returns the wrapper Err value (if isErr)
30
30
  */
31
-
32
-
33
31
  get asErr() {
34
32
  if (!this.isErr) {
35
33
  throw new Error('Cannot extract Err value from Ok result, check isErr first');
36
34
  }
37
-
38
35
  return this.value;
39
36
  }
37
+
40
38
  /**
41
39
  * @description Returns the wrapper Ok value (if isOk)
42
40
  */
43
-
44
-
45
41
  get asOk() {
46
42
  if (!this.isOk) {
47
43
  throw new Error('Cannot extract Ok value from Err result, check isOk first');
48
44
  }
49
-
50
45
  return this.value;
51
46
  }
47
+
52
48
  /**
53
49
  * @description Checks if the Result has no value
54
50
  */
55
-
56
-
57
51
  get isEmpty() {
58
52
  return this.isOk && this.value.isEmpty;
59
53
  }
54
+
60
55
  /**
61
56
  * @description Checks if the Result wraps an Err value
62
57
  */
63
-
64
-
65
58
  get isErr() {
66
59
  return !this.isOk;
67
60
  }
61
+
68
62
  /**
69
63
  * @description Checks if the Result wraps an Ok value
70
64
  */
71
-
72
-
73
65
  get isOk() {
74
66
  return this.index === 0;
75
67
  }
68
+
76
69
  /**
77
70
  * @description Returns the base runtime type name for this instance
78
71
  */
79
-
80
-
81
72
  toRawType() {
82
73
  const Types = this._toRawStruct();
83
-
84
74
  return `Result<${Types.Ok},${Types.Err}>`;
85
75
  }
86
-
87
76
  }
package/base/Tuple.js CHANGED
@@ -1,44 +1,38 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { isFunction, isHex, isString, isU8a, stringify, u8aConcatStrict, u8aToU8a } from '@polkadot/util';
4
5
  import { AbstractArray } from "../abstract/Array.js";
5
6
  import { decodeU8a, mapToTypeMap, typeToConstructor } from "../utils/index.js";
6
-
7
7
  function noopSetDefinition(d) {
8
8
  return d;
9
9
  }
10
- /** @internal */
11
-
12
10
 
11
+ /** @internal */
13
12
  function decodeTuple(registry, result, value, Classes) {
14
13
  if (isU8a(value) || isHex(value)) {
15
14
  return decodeU8a(registry, result, u8aToU8a(value), Classes);
16
15
  }
17
-
18
16
  const Types = Classes[0];
19
-
20
17
  for (let i = 0; i < Types.length; i++) {
21
18
  try {
22
- const entry = value === null || value === void 0 ? void 0 : value[i];
19
+ const entry = value == null ? void 0 : value[i];
23
20
  result[i] = entry instanceof Types[i] ? entry : new Types[i](registry, entry);
24
21
  } catch (error) {
25
22
  throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
26
23
  }
27
24
  }
28
-
29
25
  return [result, 0];
30
26
  }
27
+
31
28
  /**
32
29
  * @name Tuple
33
30
  * @description
34
31
  * A Tuple defines an anonymous fixed-length array, where each element has its
35
32
  * own type. It extends the base JS `Array` object.
36
33
  */
37
-
38
-
39
34
  export class Tuple extends AbstractArray {
40
35
  #Types;
41
-
42
36
  constructor(registry, Types, value, {
43
37
  definition,
44
38
  setDefinition = noopSetDefinition
@@ -48,12 +42,11 @@ export class Tuple extends AbstractArray {
48
42
  this.initialU8aLength = (isU8a(value) ? decodeU8a(registry, this, value, Classes) : decodeTuple(registry, this, value, Classes))[1];
49
43
  this.#Types = Classes;
50
44
  }
51
-
52
45
  static with(Types) {
53
- let definition; // eslint-disable-next-line no-return-assign
46
+ let definition;
54
47
 
48
+ // eslint-disable-next-line no-return-assign
55
49
  const setDefinition = d => definition = d;
56
-
57
50
  return class extends Tuple {
58
51
  constructor(registry, value) {
59
52
  super(registry, Types, value, {
@@ -61,67 +54,57 @@ export class Tuple extends AbstractArray {
61
54
  setDefinition
62
55
  });
63
56
  }
64
-
65
57
  };
66
58
  }
59
+
67
60
  /**
68
61
  * @description The length of the value when encoded as a Uint8Array
69
62
  */
70
-
71
-
72
63
  get encodedLength() {
73
64
  let total = 0;
74
-
75
65
  for (let i = 0; i < this.length; i++) {
76
66
  total += this[i].encodedLength;
77
67
  }
78
-
79
68
  return total;
80
69
  }
70
+
81
71
  /**
82
72
  * @description The types definition of the tuple
83
73
  */
84
-
85
-
86
74
  get Types() {
87
75
  return this.#Types[1].length ? this.#Types[1] : this.#Types[0].map(T => new T(this.registry).toRawType());
88
76
  }
77
+
89
78
  /**
90
79
  * @description Returns a breakdown of the hex encoding for this Codec
91
80
  */
92
-
93
-
94
81
  inspect() {
95
82
  return {
96
83
  inner: this.inspectInner()
97
84
  };
98
85
  }
86
+
99
87
  /**
100
88
  * @description Returns the base runtime type name for this instance
101
89
  */
102
-
103
-
104
90
  toRawType() {
105
91
  const types = this.#Types[0].map(T => this.registry.getClassName(T) || new T(this.registry).toRawType());
106
92
  return `(${types.join(',')})`;
107
93
  }
94
+
108
95
  /**
109
96
  * @description Returns the string representation of the value
110
97
  */
111
-
112
-
113
98
  toString() {
114
99
  // Overwrite the default toString representation of Array.
115
100
  return stringify(this.toJSON());
116
101
  }
102
+
117
103
  /**
118
104
  * @description Encodes the value as a Uint8Array as per the SCALE specifications
119
105
  * @param isBare true when the value has none of the type-specific prefixes (internal)
120
106
  */
121
-
122
-
123
107
  toU8a(isBare) {
124
108
  return u8aConcatStrict(this.toU8aInner(isBare));
125
109
  }
126
-
127
110
  }
package/base/UInt.js CHANGED
@@ -1,6 +1,8 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { AbstractInt } from "../abstract/Int.js";
5
+
4
6
  /**
5
7
  * @name UInt
6
8
  * @description
@@ -11,19 +13,15 @@ import { AbstractInt } from "../abstract/Int.js";
11
13
  * is available here.
12
14
  * @noInheritDoc
13
15
  */
14
-
15
16
  export class UInt extends AbstractInt {
16
17
  static with(bitLength, typeName) {
17
18
  return class extends UInt {
18
19
  constructor(registry, value) {
19
20
  super(registry, value, bitLength);
20
21
  }
21
-
22
22
  toRawType() {
23
23
  return typeName || super.toRawType();
24
24
  }
25
-
26
25
  };
27
26
  }
28
-
29
27
  }
package/base/Vec.js CHANGED
@@ -1,39 +1,32 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { compactFromU8aLim, isU8a, logger, u8aToU8a } from '@polkadot/util';
4
5
  import { AbstractArray } from "../abstract/Array.js";
5
6
  import { decodeU8aVec, typeToConstructor } from "../utils/index.js";
6
7
  const MAX_LENGTH = 64 * 1024;
7
8
  const l = logger('Vec');
8
-
9
9
  function noopSetDefinition(d) {
10
10
  return d;
11
11
  }
12
-
13
12
  function decodeVecLength(value) {
14
13
  if (Array.isArray(value)) {
15
14
  return [value, value.length, 0];
16
15
  }
17
-
18
16
  const u8a = u8aToU8a(value);
19
17
  const [startAt, length] = compactFromU8aLim(u8a);
20
-
21
18
  if (length > MAX_LENGTH) {
22
19
  throw new Error(`Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
23
20
  }
24
-
25
21
  return [u8a, length, startAt];
26
22
  }
27
-
28
23
  export function decodeVec(registry, result, value, startAt, Type) {
29
24
  if (Array.isArray(value)) {
30
25
  const count = result.length;
31
-
32
26
  for (let i = 0; i < count; i++) {
33
27
  // 26/08/2022 this is actually a false positive - after recent eslint upgdates
34
28
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
35
29
  const entry = value[i];
36
-
37
30
  try {
38
31
  result[i] = entry instanceof Type ? entry : new Type(registry, entry);
39
32
  } catch (error) {
@@ -41,12 +34,11 @@ export function decodeVec(registry, result, value, startAt, Type) {
41
34
  throw error;
42
35
  }
43
36
  }
44
-
45
37
  return [0, 0];
46
38
  }
47
-
48
39
  return decodeU8aVec(registry, result, u8aToU8a(value), startAt, Type);
49
40
  }
41
+
50
42
  /**
51
43
  * @name Vec
52
44
  * @description
@@ -54,10 +46,8 @@ export function decodeVec(registry, result, value, startAt, Type) {
54
46
  * construction with the passed `Type` in the constructor. It is an extension to Array, providing
55
47
  * specific encoding/decoding on top of the base type.
56
48
  */
57
-
58
49
  export class Vec extends AbstractArray {
59
50
  #Type;
60
-
61
51
  constructor(registry, Type, value = [], {
62
52
  definition,
63
53
  setDefinition = noopSetDefinition
@@ -67,12 +57,11 @@ export class Vec extends AbstractArray {
67
57
  this.#Type = definition || setDefinition(typeToConstructor(registry, Type));
68
58
  this.initialU8aLength = (isU8a(decodeFrom) ? decodeU8aVec(registry, this, decodeFrom, startAt, this.#Type) : decodeVec(registry, this, decodeFrom, startAt, this.#Type))[0];
69
59
  }
70
-
71
60
  static with(Type) {
72
- let definition; // eslint-disable-next-line no-return-assign
61
+ let definition;
73
62
 
63
+ // eslint-disable-next-line no-return-assign
74
64
  const setDefinition = d => definition = d;
75
-
76
65
  return class extends Vec {
77
66
  constructor(registry, value) {
78
67
  super(registry, Type, value, {
@@ -80,41 +69,34 @@ export class Vec extends AbstractArray {
80
69
  setDefinition
81
70
  });
82
71
  }
83
-
84
72
  };
85
73
  }
74
+
86
75
  /**
87
76
  * @description The type for the items
88
77
  */
89
-
90
-
91
78
  get Type() {
92
79
  return this.#Type.name;
93
80
  }
81
+
94
82
  /**
95
83
  * @description Finds the index of the value in the array
96
84
  */
97
-
98
-
99
85
  indexOf(_other) {
100
86
  // convert type first, this removes overhead from the eq
101
87
  const other = _other instanceof this.#Type ? _other : new this.#Type(this.registry, _other);
102
-
103
88
  for (let i = 0; i < this.length; i++) {
104
89
  if (other.eq(this[i])) {
105
90
  return i;
106
91
  }
107
92
  }
108
-
109
93
  return -1;
110
94
  }
95
+
111
96
  /**
112
97
  * @description Returns the base runtime type name for this instance
113
98
  */
114
-
115
-
116
99
  toRawType() {
117
100
  return `Vec<${this.registry.getClassName(this.#Type) || new this.#Type(this.registry).toRawType()}>`;
118
101
  }
119
-
120
102
  }
package/base/VecAny.js CHANGED
@@ -1,6 +1,8 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { AbstractArray } from "../abstract/Array.js";
5
+
4
6
  /**
5
7
  * @name VecAny
6
8
  * @description
@@ -8,7 +10,6 @@ import { AbstractArray } from "../abstract/Array.js";
8
10
  * a vector, this can be used to manage array-like structures with variable arguments of
9
11
  * any types
10
12
  */
11
-
12
13
  export class VecAny extends AbstractArray {
13
14
  /**
14
15
  * @description Returns the base runtime type name for this instance
@@ -17,5 +18,4 @@ export class VecAny extends AbstractArray {
17
18
  // FIXME This is basically an any type, cannot instantiate via createType
18
19
  return 'Vec<Codec>';
19
20
  }
20
-
21
21
  }