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