@polkadot/types-codec 8.12.1 → 8.14.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 (65) hide show
  1. package/abstract/Array.d.ts +4 -0
  2. package/abstract/Array.js +18 -2
  3. package/abstract/Base.d.ts +4 -0
  4. package/abstract/Base.js +8 -0
  5. package/abstract/Int.d.ts +4 -0
  6. package/abstract/Int.js +8 -0
  7. package/base/Compact.d.ts +4 -0
  8. package/base/Compact.js +8 -0
  9. package/base/DoNotConstruct.d.ts +1 -0
  10. package/base/DoNotConstruct.js +4 -0
  11. package/base/Enum.d.ts +4 -0
  12. package/base/Enum.js +10 -0
  13. package/base/Null.d.ts +4 -0
  14. package/base/Null.js +8 -0
  15. package/base/Option.d.ts +4 -0
  16. package/base/Option.js +8 -0
  17. package/cjs/abstract/Array.js +18 -2
  18. package/cjs/abstract/Base.js +8 -0
  19. package/cjs/abstract/Int.js +8 -0
  20. package/cjs/base/Compact.js +8 -0
  21. package/cjs/base/DoNotConstruct.js +4 -0
  22. package/cjs/base/Enum.js +10 -0
  23. package/cjs/base/Null.js +8 -0
  24. package/cjs/base/Option.js +8 -0
  25. package/cjs/extended/BTreeSet.js +14 -0
  26. package/cjs/extended/Map.js +17 -1
  27. package/cjs/extended/WrapperKeepOpaque.js +8 -0
  28. package/cjs/native/Bool.js +8 -0
  29. package/cjs/native/Date.js +8 -0
  30. package/cjs/native/Float.js +8 -0
  31. package/cjs/native/Json.js +12 -0
  32. package/cjs/native/Raw.js +16 -8
  33. package/cjs/native/Set.js +8 -0
  34. package/cjs/native/Struct.js +20 -1
  35. package/cjs/native/Text.js +8 -0
  36. package/cjs/package.json +1 -1
  37. package/cjs/packageInfo.js +1 -1
  38. package/cjs/utils/decodeU8a.js +6 -5
  39. package/extended/BTreeSet.d.ts +4 -0
  40. package/extended/BTreeSet.js +14 -0
  41. package/extended/Map.d.ts +4 -0
  42. package/extended/Map.js +16 -1
  43. package/extended/WrapperKeepOpaque.d.ts +4 -0
  44. package/extended/WrapperKeepOpaque.js +8 -0
  45. package/native/Bool.d.ts +4 -0
  46. package/native/Bool.js +8 -0
  47. package/native/Date.d.ts +4 -0
  48. package/native/Date.js +8 -0
  49. package/native/Float.d.ts +4 -0
  50. package/native/Float.js +8 -0
  51. package/native/Json.d.ts +4 -0
  52. package/native/Json.js +11 -0
  53. package/native/Raw.d.ts +4 -0
  54. package/native/Raw.js +16 -8
  55. package/native/Set.d.ts +4 -0
  56. package/native/Set.js +8 -0
  57. package/native/Struct.d.ts +4 -0
  58. package/native/Struct.js +20 -1
  59. package/native/Text.d.ts +4 -0
  60. package/native/Text.js +8 -0
  61. package/package.json +5 -5
  62. package/packageInfo.js +1 -1
  63. package/types/codec.d.ts +5 -1
  64. package/types/interfaces.d.ts +1 -0
  65. package/utils/decodeU8a.js +6 -5
@@ -57,6 +57,10 @@ export declare abstract class AbstractArray<T extends Codec> extends Array<T> im
57
57
  * @description Converts the Object to JSON, typically used for RPC transfers
58
58
  */
59
59
  toJSON(): AnyJson;
60
+ /**
61
+ * @description Converts the value in a best-fit primitive form
62
+ */
63
+ toPrimitive(): AnyJson;
60
64
  /**
61
65
  * @description Returns the base runtime type name for this instance
62
66
  */
package/abstract/Array.js CHANGED
@@ -118,7 +118,7 @@ export class AbstractArray extends Array {
118
118
  const result = new Array(this.length);
119
119
 
120
120
  for (let i = 0; i < this.length; i++) {
121
- result[i] = this[i].toHuman(isExtended);
121
+ result[i] = this[i] && this[i].toHuman(isExtended);
122
122
  }
123
123
 
124
124
  return result;
@@ -132,7 +132,23 @@ export class AbstractArray extends Array {
132
132
  const result = new Array(this.length);
133
133
 
134
134
  for (let i = 0; i < this.length; i++) {
135
- result[i] = this[i].toJSON();
135
+ // We actually log inside the U8a decoding and use JSON.stringify(...), which
136
+ // means that the Vec may be partially populated (same applies to toHuman, same check)
137
+ result[i] = this[i] && this[i].toJSON();
138
+ }
139
+
140
+ return result;
141
+ }
142
+ /**
143
+ * @description Converts the value in a best-fit primitive form
144
+ */
145
+
146
+
147
+ toPrimitive() {
148
+ const result = new Array(this.length);
149
+
150
+ for (let i = 0; i < this.length; i++) {
151
+ result[i] = this[i] && this[i].toPrimitive();
136
152
  }
137
153
 
138
154
  return result;
@@ -43,6 +43,10 @@ export declare abstract class AbstractBase<T extends Codec> implements Codec {
43
43
  * @description Converts the Object to JSON, typically used for RPC transfers
44
44
  */
45
45
  toJSON(): AnyJson;
46
+ /**
47
+ * @description Converts the value in a best-fit primitive form
48
+ */
49
+ toPrimitive(): AnyJson;
46
50
  /**
47
51
  * @description Returns the string representation of the value
48
52
  */
package/abstract/Base.js CHANGED
@@ -81,6 +81,14 @@ export class AbstractBase {
81
81
  toJSON() {
82
82
  return this.#raw.toJSON();
83
83
  }
84
+ /**
85
+ * @description Converts the value in a best-fit primitive form
86
+ */
87
+
88
+
89
+ toPrimitive() {
90
+ return this.#raw.toPrimitive();
91
+ }
84
92
  /**
85
93
  * @description Returns the string representation of the value
86
94
  */
package/abstract/Int.d.ts CHANGED
@@ -59,6 +59,10 @@ export declare abstract class AbstractInt extends BN implements INumber {
59
59
  * @description Converts the Object to JSON, typically used for RPC transfers
60
60
  */
61
61
  toJSON(onlyHex?: boolean): any;
62
+ /**
63
+ * @description Returns the value in a primitive form, either number when <= 52 bits, or string otherwise
64
+ */
65
+ toPrimitive(): number | string;
62
66
  /**
63
67
  * @description Returns the base runtime type name for this instance
64
68
  */
package/abstract/Int.js CHANGED
@@ -194,6 +194,14 @@ export class AbstractInt extends BN {
194
194
  // - this.#bitLength - the type bits (this should be used, however contracts RPC is problematic)
195
195
  return onlyHex || super.bitLength() > MAX_NUMBER_BITS ? this.toHex() : this.toNumber();
196
196
  }
197
+ /**
198
+ * @description Returns the value in a primitive form, either number when <= 52 bits, or string otherwise
199
+ */
200
+
201
+
202
+ toPrimitive() {
203
+ return super.bitLength() > MAX_NUMBER_BITS ? this.toString() : this.toNumber();
204
+ }
197
205
  /**
198
206
  * @description Returns the base runtime type name for this instance
199
207
  */
package/base/Compact.d.ts CHANGED
@@ -69,6 +69,10 @@ export declare class Compact<T extends INumber> implements ICompact<T> {
69
69
  * @description Returns the number representation for the value
70
70
  */
71
71
  toNumber(): number;
72
+ /**
73
+ * @description Converts the value in a best-fit primitive form
74
+ */
75
+ toPrimitive(): string | number;
72
76
  /**
73
77
  * @description Returns the base runtime type name for this instance
74
78
  */
package/base/Compact.js CHANGED
@@ -158,6 +158,14 @@ export class Compact {
158
158
  toNumber() {
159
159
  return this.#raw.toNumber();
160
160
  }
161
+ /**
162
+ * @description Converts the value in a best-fit primitive form
163
+ */
164
+
165
+
166
+ toPrimitive() {
167
+ return this.#raw.toPrimitive();
168
+ }
161
169
  /**
162
170
  * @description Returns the base runtime type name for this instance
163
171
  */
@@ -28,6 +28,7 @@ export declare class DoNotConstruct implements Codec {
28
28
  toHex(): HexString;
29
29
  toHuman(): AnyJson;
30
30
  toJSON(): AnyJson;
31
+ toPrimitive(): AnyJson;
31
32
  toRawType(): string;
32
33
  toString(): string;
33
34
  toU8a(): Uint8Array;
@@ -68,6 +68,10 @@ export class DoNotConstruct {
68
68
  throw this.#neverError;
69
69
  }
70
70
 
71
+ toPrimitive() {
72
+ throw this.#neverError;
73
+ }
74
+
71
75
  toRawType() {
72
76
  throw this.#neverError;
73
77
  }
package/base/Enum.d.ts CHANGED
@@ -103,6 +103,10 @@ export declare class Enum implements IEnum {
103
103
  * @description Returns the number representation for the value
104
104
  */
105
105
  toNumber(): number;
106
+ /**
107
+ * @description Converts the value in a best-fit primitive form
108
+ */
109
+ toPrimitive(): AnyJson;
106
110
  /**
107
111
  * @description Returns a raw struct representation of the enum types
108
112
  */
package/base/Enum.js CHANGED
@@ -402,6 +402,16 @@ export class Enum {
402
402
  toNumber() {
403
403
  return this.index;
404
404
  }
405
+ /**
406
+ * @description Converts the value in a best-fit primitive form
407
+ */
408
+
409
+
410
+ toPrimitive() {
411
+ return this.#isBasic ? this.type : {
412
+ [stringCamelCase(this.type)]: this.#raw.toPrimitive()
413
+ };
414
+ }
405
415
  /**
406
416
  * @description Returns a raw struct representation of the enum types
407
417
  */
package/base/Null.d.ts CHANGED
@@ -36,6 +36,10 @@ export declare class Null implements Codec {
36
36
  * @description Converts the Object to JSON, typically used for RPC transfers
37
37
  */
38
38
  toJSON(): null;
39
+ /**
40
+ * @description Converts the value in a best-fit primitive form
41
+ */
42
+ toPrimitive(): null;
39
43
  /**
40
44
  * @description Returns the base runtime type name for this instance
41
45
  */
package/base/Null.js CHANGED
@@ -64,6 +64,14 @@ export class Null {
64
64
  toJSON() {
65
65
  return null;
66
66
  }
67
+ /**
68
+ * @description Converts the value in a best-fit primitive form
69
+ */
70
+
71
+
72
+ toPrimitive() {
73
+ return null;
74
+ }
67
75
  /**
68
76
  * @description Returns the base runtime type name for this instance
69
77
  */
package/base/Option.d.ts CHANGED
@@ -63,6 +63,10 @@ export declare class Option<T extends Codec> implements IOption<T> {
63
63
  * @description Converts the Object to JSON, typically used for RPC transfers
64
64
  */
65
65
  toJSON(): AnyJson;
66
+ /**
67
+ * @description Converts the value in a best-fit primitive form
68
+ */
69
+ toPrimitive(): AnyJson;
66
70
  /**
67
71
  * @description Returns the base runtime type name for this instance
68
72
  */
package/base/Option.js CHANGED
@@ -201,6 +201,14 @@ export class Option {
201
201
  toJSON() {
202
202
  return this.isNone ? null : this.#raw.toJSON();
203
203
  }
204
+ /**
205
+ * @description Converts the value in a best-fit primitive form
206
+ */
207
+
208
+
209
+ toPrimitive() {
210
+ return this.isNone ? null : this.#raw.toPrimitive();
211
+ }
204
212
  /**
205
213
  * @description Returns the base runtime type name for this instance
206
214
  */
@@ -127,7 +127,7 @@ class AbstractArray extends Array {
127
127
  const result = new Array(this.length);
128
128
 
129
129
  for (let i = 0; i < this.length; i++) {
130
- result[i] = this[i].toHuman(isExtended);
130
+ result[i] = this[i] && this[i].toHuman(isExtended);
131
131
  }
132
132
 
133
133
  return result;
@@ -141,7 +141,23 @@ class AbstractArray extends Array {
141
141
  const result = new Array(this.length);
142
142
 
143
143
  for (let i = 0; i < this.length; i++) {
144
- result[i] = this[i].toJSON();
144
+ // We actually log inside the U8a decoding and use JSON.stringify(...), which
145
+ // means that the Vec may be partially populated (same applies to toHuman, same check)
146
+ result[i] = this[i] && this[i].toJSON();
147
+ }
148
+
149
+ return result;
150
+ }
151
+ /**
152
+ * @description Converts the value in a best-fit primitive form
153
+ */
154
+
155
+
156
+ toPrimitive() {
157
+ const result = new Array(this.length);
158
+
159
+ for (let i = 0; i < this.length; i++) {
160
+ result[i] = this[i] && this[i].toPrimitive();
145
161
  }
146
162
 
147
163
  return result;
@@ -88,6 +88,14 @@ class AbstractBase {
88
88
  toJSON() {
89
89
  return this.#raw.toJSON();
90
90
  }
91
+ /**
92
+ * @description Converts the value in a best-fit primitive form
93
+ */
94
+
95
+
96
+ toPrimitive() {
97
+ return this.#raw.toPrimitive();
98
+ }
91
99
  /**
92
100
  * @description Returns the string representation of the value
93
101
  */
@@ -211,6 +211,14 @@ class AbstractInt extends _util.BN {
211
211
  // - this.#bitLength - the type bits (this should be used, however contracts RPC is problematic)
212
212
  return onlyHex || super.bitLength() > MAX_NUMBER_BITS ? this.toHex() : this.toNumber();
213
213
  }
214
+ /**
215
+ * @description Returns the value in a primitive form, either number when <= 52 bits, or string otherwise
216
+ */
217
+
218
+
219
+ toPrimitive() {
220
+ return super.bitLength() > MAX_NUMBER_BITS ? this.toString() : this.toNumber();
221
+ }
214
222
  /**
215
223
  * @description Returns the base runtime type name for this instance
216
224
  */
@@ -168,6 +168,14 @@ class Compact {
168
168
  toNumber() {
169
169
  return this.#raw.toNumber();
170
170
  }
171
+ /**
172
+ * @description Converts the value in a best-fit primitive form
173
+ */
174
+
175
+
176
+ toPrimitive() {
177
+ return this.#raw.toPrimitive();
178
+ }
171
179
  /**
172
180
  * @description Returns the base runtime type name for this instance
173
181
  */
@@ -76,6 +76,10 @@ class DoNotConstruct {
76
76
  throw this.#neverError;
77
77
  }
78
78
 
79
+ toPrimitive() {
80
+ throw this.#neverError;
81
+ }
82
+
79
83
  toRawType() {
80
84
  throw this.#neverError;
81
85
  }
package/cjs/base/Enum.js CHANGED
@@ -420,6 +420,16 @@ class Enum {
420
420
  toNumber() {
421
421
  return this.index;
422
422
  }
423
+ /**
424
+ * @description Converts the value in a best-fit primitive form
425
+ */
426
+
427
+
428
+ toPrimitive() {
429
+ return this.#isBasic ? this.type : {
430
+ [(0, _util.stringCamelCase)(this.type)]: this.#raw.toPrimitive()
431
+ };
432
+ }
423
433
  /**
424
434
  * @description Returns a raw struct representation of the enum types
425
435
  */
package/cjs/base/Null.js CHANGED
@@ -72,6 +72,14 @@ class Null {
72
72
  toJSON() {
73
73
  return null;
74
74
  }
75
+ /**
76
+ * @description Converts the value in a best-fit primitive form
77
+ */
78
+
79
+
80
+ toPrimitive() {
81
+ return null;
82
+ }
75
83
  /**
76
84
  * @description Returns the base runtime type name for this instance
77
85
  */
@@ -211,6 +211,14 @@ class Option {
211
211
  toJSON() {
212
212
  return this.isNone ? null : this.#raw.toJSON();
213
213
  }
214
+ /**
215
+ * @description Converts the value in a best-fit primitive form
216
+ */
217
+
218
+
219
+ toPrimitive() {
220
+ return this.isNone ? null : this.#raw.toPrimitive();
221
+ }
214
222
  /**
215
223
  * @description Returns the base runtime type name for this instance
216
224
  */
@@ -197,6 +197,20 @@ class BTreeSet extends Set {
197
197
  toRawType() {
198
198
  return `BTreeSet<${this.registry.getClassName(this.#ValClass) || new this.#ValClass(this.registry).toRawType()}>`;
199
199
  }
200
+ /**
201
+ * @description Converts the value in a best-fit primitive form
202
+ */
203
+
204
+
205
+ toPrimitive() {
206
+ const json = [];
207
+
208
+ for (const v of this.values()) {
209
+ json.push(v.toPrimitive());
210
+ }
211
+
212
+ return json;
213
+ }
200
214
  /**
201
215
  * @description Returns the string representation of the value
202
216
  */
@@ -11,6 +11,8 @@ var _Array = require("../abstract/Array");
11
11
 
12
12
  var _Enum = require("../base/Enum");
13
13
 
14
+ var _Raw = require("../native/Raw");
15
+
14
16
  var _Struct = require("../native/Struct");
15
17
 
16
18
  var _utils = require("../utils");
@@ -178,7 +180,7 @@ class CodecMap extends Map {
178
180
  const json = {};
179
181
 
180
182
  for (const [k, v] of this.entries()) {
181
- json[k.toString()] = v.toHuman(isExtended);
183
+ json[k instanceof _Raw.Raw && k.isAscii ? k.toUtf8() : k.toString()] = v.toHuman(isExtended);
182
184
  }
183
185
 
184
186
  return json;
@@ -197,6 +199,20 @@ class CodecMap extends Map {
197
199
 
198
200
  return json;
199
201
  }
202
+ /**
203
+ * @description Converts the value in a best-fit primitive form
204
+ */
205
+
206
+
207
+ toPrimitive() {
208
+ const json = {};
209
+
210
+ for (const [k, v] of this.entries()) {
211
+ json[k instanceof _Raw.Raw && k.isAscii ? k.toUtf8() : k.toString()] = v.toPrimitive();
212
+ }
213
+
214
+ return json;
215
+ }
200
216
  /**
201
217
  * @description Returns the base runtime type name for this instance
202
218
  */
@@ -84,6 +84,14 @@ class WrapperKeepOpaque extends _Bytes.Bytes {
84
84
  toHuman(isExtended) {
85
85
  return this.#decoded ? this.#decoded.toHuman(isExtended) : super.toHuman();
86
86
  }
87
+ /**
88
+ * @description Converts the value in a best-fit primitive form
89
+ */
90
+
91
+
92
+ toPrimitive() {
93
+ return this.#decoded ? this.#decoded.toPrimitive() : super.toPrimitive();
94
+ }
87
95
  /**
88
96
  * @description Returns the base runtime type name for this instance
89
97
  */
@@ -104,6 +104,14 @@ class bool extends Boolean {
104
104
  toJSON() {
105
105
  return this.valueOf();
106
106
  }
107
+ /**
108
+ * @description Converts the value in a best-fit primitive form
109
+ */
110
+
111
+
112
+ toPrimitive() {
113
+ return this.toJSON();
114
+ }
107
115
  /**
108
116
  * @description Returns the base runtime type name for this instance
109
117
  */
@@ -149,6 +149,14 @@ class CodecDate extends Date {
149
149
  toNumber() {
150
150
  return Math.ceil(this.getTime() / 1000);
151
151
  }
152
+ /**
153
+ * @description Converts the value in a best-fit primitive form
154
+ */
155
+
156
+
157
+ toPrimitive() {
158
+ return this.toNumber();
159
+ }
152
160
  /**
153
161
  * @description Returns the base runtime type name for this instance
154
162
  */
@@ -112,6 +112,14 @@ class Float extends Number {
112
112
  toNumber() {
113
113
  return this.valueOf();
114
114
  }
115
+ /**
116
+ * @description Converts the value in a best-fit primitive form
117
+ */
118
+
119
+
120
+ toPrimitive() {
121
+ return this.toNumber();
122
+ }
115
123
  /**
116
124
  * @description Returns the base runtime type name for this instance
117
125
  */
@@ -116,6 +116,18 @@ class Json extends Map {
116
116
  return json;
117
117
  }, {});
118
118
  }
119
+ /**
120
+ * @description Converts the value in a best-fit primitive form
121
+ */
122
+
123
+
124
+ toPrimitive() {
125
+ return [...this.entries()].reduce((json, _ref4) => {
126
+ let [key, value] = _ref4;
127
+ json[key] = (0, _util.isFunction)(value.toHuman) ? value.toPrimitive() : value;
128
+ return json;
129
+ }, {});
130
+ }
119
131
  /**
120
132
  * @description Returns the base runtime type name for this instance
121
133
  */
package/cjs/native/Raw.js CHANGED
@@ -117,6 +117,22 @@ class Raw extends Uint8Array {
117
117
 
118
118
 
119
119
  toHuman() {
120
+ return this.toPrimitive();
121
+ }
122
+ /**
123
+ * @description Converts the Object to JSON, typically used for RPC transfers
124
+ */
125
+
126
+
127
+ toJSON() {
128
+ return this.toHex();
129
+ }
130
+ /**
131
+ * @description Converts the value in a best-fit primitive form
132
+ */
133
+
134
+
135
+ toPrimitive() {
120
136
  if (this.isAscii) {
121
137
  const text = this.toUtf8(); // ensure we didn't end up with multibyte codepoints
122
138
 
@@ -127,14 +143,6 @@ class Raw extends Uint8Array {
127
143
 
128
144
  return this.toJSON();
129
145
  }
130
- /**
131
- * @description Converts the Object to JSON, typically used for RPC transfers
132
- */
133
-
134
-
135
- toJSON() {
136
- return this.toHex();
137
- }
138
146
  /**
139
147
  * @description Returns the base runtime type name for this instance
140
148
  */
package/cjs/native/Set.js CHANGED
@@ -238,6 +238,14 @@ class CodecSet extends Set {
238
238
  toNumber() {
239
239
  return this.valueEncoded.toNumber();
240
240
  }
241
+ /**
242
+ * @description Converts the value in a best-fit primitive form
243
+ */
244
+
245
+
246
+ toPrimitive() {
247
+ return this.toJSON();
248
+ }
241
249
  /**
242
250
  * @description Returns the base runtime type name for this instance
243
251
  */
@@ -267,12 +267,31 @@ class Struct extends Map {
267
267
  const json = {};
268
268
 
269
269
  for (const [k, v] of this.entries()) {
270
- const jsonKey = this.#jsonMap.get(k) || k;
270
+ const jsonKey = this.#jsonMap.get(k) || k; // We actually log inside the U8a decoding and use JSON.stringify(...), which
271
+ // means that the Vec may be partially populated (same applies to toHuman, same check)
272
+
271
273
  json[jsonKey] = v && v.toJSON();
272
274
  }
273
275
 
274
276
  return json;
275
277
  }
278
+ /**
279
+ * @description Converts the value in a best-fit primitive form
280
+ */
281
+
282
+
283
+ toPrimitive() {
284
+ const json = {};
285
+
286
+ for (const [k, v] of this.entries()) {
287
+ const jsonKey = this.#jsonMap.get(k) || k; // We actually log inside the U8a decoding and use JSON.stringify(...), which
288
+ // means that the Vec may be partially populated (same applies to toHuman, same check)
289
+
290
+ json[jsonKey] = v && v.toPrimitive();
291
+ }
292
+
293
+ return json;
294
+ }
276
295
  /**
277
296
  * @description Returns the base runtime type name for this instance
278
297
  */
@@ -147,6 +147,14 @@ class Text extends String {
147
147
  toJSON() {
148
148
  return this.toString();
149
149
  }
150
+ /**
151
+ * @description Converts the value in a best-fit primitive form
152
+ */
153
+
154
+
155
+ toPrimitive() {
156
+ return this.toJSON();
157
+ }
150
158
  /**
151
159
  * @description Returns the base runtime type name for this instance
152
160
  */
package/cjs/package.json CHANGED
@@ -1,3 +1,3 @@
1
1
  {
2
2
  "type": "commonjs"
3
- }
3
+ }
@@ -11,6 +11,6 @@ const packageInfo = {
11
11
  name: '@polkadot/types-codec',
12
12
  path: typeof __dirname === 'string' ? __dirname : 'auto',
13
13
  type: 'cjs',
14
- version: '8.12.1'
14
+ version: '8.14.1'
15
15
  };
16
16
  exports.packageInfo = packageInfo;
@@ -13,7 +13,7 @@ var _util = require("@polkadot/util");
13
13
  // SPDX-License-Identifier: Apache-2.0
14
14
 
15
15
  /** @internal */
16
- function formatFailure(registry, _ref, u8a, i, count, Type, key) {
16
+ function formatFailure(registry, result, _ref, u8a, i, count, Type, key) {
17
17
  let {
18
18
  message
19
19
  } = _ref;
@@ -22,7 +22,8 @@ function formatFailure(registry, _ref, u8a, i, count, Type, key) {
22
22
  try {
23
23
  type = `: ${new Type(registry).toRawType()}`;
24
24
  } catch {// ignore
25
- }
25
+ } // console.error(JSON.stringify(result, null, 2));
26
+
26
27
 
27
28
  return `decodeU8a: failed at ${(0, _util.u8aToHex)(u8a.subarray(0, 16))}…${key ? ` on ${key}` : ''} (index ${i}/${count})${type}:: ${message}`;
28
29
  }
@@ -52,7 +53,7 @@ function decodeU8a(registry, result, u8a, _ref2) {
52
53
  i++;
53
54
  }
54
55
  } catch (error) {
55
- throw new Error(formatFailure(registry, error, u8a.subarray(offset), i, count, Types[i], keys[i]));
56
+ throw new Error(formatFailure(registry, result, error, u8a.subarray(offset), i, count, Types[i], keys[i]));
56
57
  }
57
58
 
58
59
  return [result, offset];
@@ -79,7 +80,7 @@ function decodeU8aStruct(registry, result, u8a, _ref3) {
79
80
  i++;
80
81
  }
81
82
  } catch (error) {
82
- throw new Error(formatFailure(registry, error, u8a.subarray(offset), i, count, Types[i], keys[i]));
83
+ throw new Error(formatFailure(registry, result, error, u8a.subarray(offset), i, count, Types[i], keys[i]));
83
84
  }
84
85
 
85
86
  return [result, offset];
@@ -105,7 +106,7 @@ function decodeU8aVec(registry, result, u8a, startAt, Type) {
105
106
  i++;
106
107
  }
107
108
  } catch (error) {
108
- throw new Error(formatFailure(registry, error, u8a.subarray(offset), i, count, Type));
109
+ throw new Error(formatFailure(registry, result, error, u8a.subarray(offset), i, count, Type));
109
110
  }
110
111
 
111
112
  return [offset, offset - startAt];
@@ -47,6 +47,10 @@ export declare class BTreeSet<V extends Codec = Codec> extends Set<V> implements
47
47
  * @description Returns the base runtime type name for this instance
48
48
  */
49
49
  toRawType(): string;
50
+ /**
51
+ * @description Converts the value in a best-fit primitive form
52
+ */
53
+ toPrimitive(): AnyJson;
50
54
  /**
51
55
  * @description Returns the string representation of the value
52
56
  */
@@ -188,6 +188,20 @@ export class BTreeSet extends Set {
188
188
  toRawType() {
189
189
  return `BTreeSet<${this.registry.getClassName(this.#ValClass) || new this.#ValClass(this.registry).toRawType()}>`;
190
190
  }
191
+ /**
192
+ * @description Converts the value in a best-fit primitive form
193
+ */
194
+
195
+
196
+ toPrimitive() {
197
+ const json = [];
198
+
199
+ for (const v of this.values()) {
200
+ json.push(v.toPrimitive());
201
+ }
202
+
203
+ return json;
204
+ }
191
205
  /**
192
206
  * @description Returns the string representation of the value
193
207
  */
package/extended/Map.d.ts CHANGED
@@ -38,6 +38,10 @@ export declare class CodecMap<K extends Codec = Codec, V extends Codec = Codec>
38
38
  * @description Converts the Object to JSON, typically used for RPC transfers
39
39
  */
40
40
  toJSON(): Record<string, AnyJson>;
41
+ /**
42
+ * @description Converts the value in a best-fit primitive form
43
+ */
44
+ toPrimitive(): AnyJson;
41
45
  /**
42
46
  * @description Returns the base runtime type name for this instance
43
47
  */
package/extended/Map.js CHANGED
@@ -3,6 +3,7 @@
3
3
  import { compactFromU8aLim, compactToU8a, isHex, isObject, isU8a, logger, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
4
4
  import { AbstractArray } from "../abstract/Array.js";
5
5
  import { Enum } from "../base/Enum.js";
6
+ import { Raw } from "../native/Raw.js";
6
7
  import { Struct } from "../native/Struct.js";
7
8
  import { compareMap, decodeU8a, sortMap, typeToConstructor } from "../utils/index.js";
8
9
  const l = logger('Map');
@@ -165,7 +166,7 @@ export class CodecMap extends Map {
165
166
  const json = {};
166
167
 
167
168
  for (const [k, v] of this.entries()) {
168
- json[k.toString()] = v.toHuman(isExtended);
169
+ json[k instanceof Raw && k.isAscii ? k.toUtf8() : k.toString()] = v.toHuman(isExtended);
169
170
  }
170
171
 
171
172
  return json;
@@ -184,6 +185,20 @@ export class CodecMap extends Map {
184
185
 
185
186
  return json;
186
187
  }
188
+ /**
189
+ * @description Converts the value in a best-fit primitive form
190
+ */
191
+
192
+
193
+ toPrimitive() {
194
+ const json = {};
195
+
196
+ for (const [k, v] of this.entries()) {
197
+ json[k instanceof Raw && k.isAscii ? k.toUtf8() : k.toString()] = v.toPrimitive();
198
+ }
199
+
200
+ return json;
201
+ }
187
202
  /**
188
203
  * @description Returns the base runtime type name for this instance
189
204
  */
@@ -20,6 +20,10 @@ export declare class WrapperKeepOpaque<T extends Codec> extends Bytes {
20
20
  * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
21
21
  */
22
22
  toHuman(isExtended?: boolean): AnyJson;
23
+ /**
24
+ * @description Converts the value in a best-fit primitive form
25
+ */
26
+ toPrimitive(): any;
23
27
  /**
24
28
  * @description Returns the base runtime type name for this instance
25
29
  */
@@ -73,6 +73,14 @@ export class WrapperKeepOpaque extends Bytes {
73
73
  toHuman(isExtended) {
74
74
  return this.#decoded ? this.#decoded.toHuman(isExtended) : super.toHuman();
75
75
  }
76
+ /**
77
+ * @description Converts the value in a best-fit primitive form
78
+ */
79
+
80
+
81
+ toPrimitive() {
82
+ return this.#decoded ? this.#decoded.toPrimitive() : super.toPrimitive();
83
+ }
76
84
  /**
77
85
  * @description Returns the base runtime type name for this instance
78
86
  */
package/native/Bool.d.ts CHANGED
@@ -50,6 +50,10 @@ export declare class bool extends Boolean implements Codec {
50
50
  * @description Converts the Object to JSON, typically used for RPC transfers
51
51
  */
52
52
  toJSON(): boolean;
53
+ /**
54
+ * @description Converts the value in a best-fit primitive form
55
+ */
56
+ toPrimitive(): boolean;
53
57
  /**
54
58
  * @description Returns the base runtime type name for this instance
55
59
  */
package/native/Bool.js CHANGED
@@ -95,6 +95,14 @@ export class bool extends Boolean {
95
95
  toJSON() {
96
96
  return this.valueOf();
97
97
  }
98
+ /**
99
+ * @description Converts the value in a best-fit primitive form
100
+ */
101
+
102
+
103
+ toPrimitive() {
104
+ return this.toJSON();
105
+ }
98
106
  /**
99
107
  * @description Returns the base runtime type name for this instance
100
108
  */
package/native/Date.d.ts CHANGED
@@ -64,6 +64,10 @@ export declare class CodecDate extends Date implements INumber {
64
64
  * @description Returns the number representation for the timestamp
65
65
  */
66
66
  toNumber(): number;
67
+ /**
68
+ * @description Converts the value in a best-fit primitive form
69
+ */
70
+ toPrimitive(): number;
67
71
  /**
68
72
  * @description Returns the base runtime type name for this instance
69
73
  */
package/native/Date.js CHANGED
@@ -139,6 +139,14 @@ export class CodecDate extends Date {
139
139
  toNumber() {
140
140
  return Math.ceil(this.getTime() / 1000);
141
141
  }
142
+ /**
143
+ * @description Converts the value in a best-fit primitive form
144
+ */
145
+
146
+
147
+ toPrimitive() {
148
+ return this.toNumber();
149
+ }
142
150
  /**
143
151
  * @description Returns the base runtime type name for this instance
144
152
  */
package/native/Float.d.ts CHANGED
@@ -51,6 +51,10 @@ export declare class Float extends Number implements IFloat {
51
51
  * @description Returns the number representation (Same as valueOf)
52
52
  */
53
53
  toNumber(): number;
54
+ /**
55
+ * @description Converts the value in a best-fit primitive form
56
+ */
57
+ toPrimitive(): number;
54
58
  /**
55
59
  * @description Returns the base runtime type name for this instance
56
60
  */
package/native/Float.js CHANGED
@@ -103,6 +103,14 @@ export class Float extends Number {
103
103
  toNumber() {
104
104
  return this.valueOf();
105
105
  }
106
+ /**
107
+ * @description Converts the value in a best-fit primitive form
108
+ */
109
+
110
+
111
+ toPrimitive() {
112
+ return this.toNumber();
113
+ }
106
114
  /**
107
115
  * @description Returns the base runtime type name for this instance
108
116
  */
package/native/Json.d.ts CHANGED
@@ -48,6 +48,10 @@ export declare class Json extends Map<string, any> implements Codec {
48
48
  * @description Converts the Object to JSON, typically used for RPC transfers
49
49
  */
50
50
  toJSON(): Record<string, AnyJson>;
51
+ /**
52
+ * @description Converts the value in a best-fit primitive form
53
+ */
54
+ toPrimitive(): Record<string, AnyJson>;
51
55
  /**
52
56
  * @description Returns the base runtime type name for this instance
53
57
  */
package/native/Json.js CHANGED
@@ -102,6 +102,17 @@ export class Json extends Map {
102
102
  return json;
103
103
  }, {});
104
104
  }
105
+ /**
106
+ * @description Converts the value in a best-fit primitive form
107
+ */
108
+
109
+
110
+ toPrimitive() {
111
+ return [...this.entries()].reduce((json, [key, value]) => {
112
+ json[key] = isFunction(value.toHuman) ? value.toPrimitive() : value;
113
+ return json;
114
+ }, {});
115
+ }
105
116
  /**
106
117
  * @description Returns the base runtime type name for this instance
107
118
  */
package/native/Raw.d.ts CHANGED
@@ -63,6 +63,10 @@ export declare class Raw extends Uint8Array implements IU8a {
63
63
  * @description Converts the Object to JSON, typically used for RPC transfers
64
64
  */
65
65
  toJSON(): string;
66
+ /**
67
+ * @description Converts the value in a best-fit primitive form
68
+ */
69
+ toPrimitive(): AnyJson;
66
70
  /**
67
71
  * @description Returns the base runtime type name for this instance
68
72
  */
package/native/Raw.js CHANGED
@@ -109,6 +109,22 @@ export class Raw extends Uint8Array {
109
109
 
110
110
 
111
111
  toHuman() {
112
+ return this.toPrimitive();
113
+ }
114
+ /**
115
+ * @description Converts the Object to JSON, typically used for RPC transfers
116
+ */
117
+
118
+
119
+ toJSON() {
120
+ return this.toHex();
121
+ }
122
+ /**
123
+ * @description Converts the value in a best-fit primitive form
124
+ */
125
+
126
+
127
+ toPrimitive() {
112
128
  if (this.isAscii) {
113
129
  const text = this.toUtf8(); // ensure we didn't end up with multibyte codepoints
114
130
 
@@ -119,14 +135,6 @@ export class Raw extends Uint8Array {
119
135
 
120
136
  return this.toJSON();
121
137
  }
122
- /**
123
- * @description Converts the Object to JSON, typically used for RPC transfers
124
- */
125
-
126
-
127
- toJSON() {
128
- return this.toHex();
129
- }
130
138
  /**
131
139
  * @description Returns the base runtime type name for this instance
132
140
  */
package/native/Set.d.ts CHANGED
@@ -63,6 +63,10 @@ export declare class CodecSet extends Set<string> implements ISet<string> {
63
63
  * @description The encoded value for the set members
64
64
  */
65
65
  toNumber(): number;
66
+ /**
67
+ * @description Converts the value in a best-fit primitive form
68
+ */
69
+ toPrimitive(): string[];
66
70
  /**
67
71
  * @description Returns the base runtime type name for this instance
68
72
  */
package/native/Set.js CHANGED
@@ -226,6 +226,14 @@ export class CodecSet extends Set {
226
226
  toNumber() {
227
227
  return this.valueEncoded.toNumber();
228
228
  }
229
+ /**
230
+ * @description Converts the value in a best-fit primitive form
231
+ */
232
+
233
+
234
+ toPrimitive() {
235
+ return this.toJSON();
236
+ }
229
237
  /**
230
238
  * @description Returns the base runtime type name for this instance
231
239
  */
@@ -85,6 +85,10 @@ export declare class Struct<S extends TypesDef = TypesDef, V extends {
85
85
  * @description Converts the Object to JSON, typically used for RPC transfers
86
86
  */
87
87
  toJSON(): Record<string, AnyJson>;
88
+ /**
89
+ * @description Converts the value in a best-fit primitive form
90
+ */
91
+ toPrimitive(): Record<string, AnyJson>;
88
92
  /**
89
93
  * @description Returns the base runtime type name for this instance
90
94
  */
package/native/Struct.js CHANGED
@@ -256,12 +256,31 @@ export class Struct extends Map {
256
256
  const json = {};
257
257
 
258
258
  for (const [k, v] of this.entries()) {
259
- const jsonKey = this.#jsonMap.get(k) || k;
259
+ const jsonKey = this.#jsonMap.get(k) || k; // We actually log inside the U8a decoding and use JSON.stringify(...), which
260
+ // means that the Vec may be partially populated (same applies to toHuman, same check)
261
+
260
262
  json[jsonKey] = v && v.toJSON();
261
263
  }
262
264
 
263
265
  return json;
264
266
  }
267
+ /**
268
+ * @description Converts the value in a best-fit primitive form
269
+ */
270
+
271
+
272
+ toPrimitive() {
273
+ const json = {};
274
+
275
+ for (const [k, v] of this.entries()) {
276
+ const jsonKey = this.#jsonMap.get(k) || k; // We actually log inside the U8a decoding and use JSON.stringify(...), which
277
+ // means that the Vec may be partially populated (same applies to toHuman, same check)
278
+
279
+ json[jsonKey] = v && v.toPrimitive();
280
+ }
281
+
282
+ return json;
283
+ }
265
284
  /**
266
285
  * @description Returns the base runtime type name for this instance
267
286
  */
package/native/Text.d.ts CHANGED
@@ -56,6 +56,10 @@ export declare class Text extends String implements IText {
56
56
  * @description Converts the Object to JSON, typically used for RPC transfers
57
57
  */
58
58
  toJSON(): string;
59
+ /**
60
+ * @description Converts the value in a best-fit primitive form
61
+ */
62
+ toPrimitive(): string;
59
63
  /**
60
64
  * @description Returns the base runtime type name for this instance
61
65
  */
package/native/Text.js CHANGED
@@ -138,6 +138,14 @@ export class Text extends String {
138
138
  toJSON() {
139
139
  return this.toString();
140
140
  }
141
+ /**
142
+ * @description Converts the value in a best-fit primitive form
143
+ */
144
+
145
+
146
+ toPrimitive() {
147
+ return this.toJSON();
148
+ }
141
149
  /**
142
150
  * @description Returns the base runtime type name for this instance
143
151
  */
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "./cjs/detectPackage.js"
21
21
  ],
22
22
  "type": "module",
23
- "version": "8.12.1",
23
+ "version": "8.14.1",
24
24
  "main": "./cjs/index.js",
25
25
  "module": "./index.js",
26
26
  "types": "./index.d.ts",
@@ -434,8 +434,8 @@
434
434
  }
435
435
  },
436
436
  "dependencies": {
437
- "@babel/runtime": "^7.18.6",
438
- "@polkadot/util": "^10.0.1",
439
- "@polkadot/x-bigint": "^10.0.1"
437
+ "@babel/runtime": "^7.18.9",
438
+ "@polkadot/util": "^10.1.1",
439
+ "@polkadot/x-bigint": "^10.1.1"
440
440
  }
441
- }
441
+ }
package/packageInfo.js CHANGED
@@ -5,5 +5,5 @@ export const packageInfo = {
5
5
  name: '@polkadot/types-codec',
6
6
  path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto',
7
7
  type: 'esm',
8
- version: '8.12.1'
8
+ version: '8.14.1'
9
9
  };
package/types/codec.d.ts CHANGED
@@ -60,6 +60,10 @@ export interface Codec {
60
60
  * @description Converts the Object to JSON, typically used for RPC transfers
61
61
  */
62
62
  toJSON(): AnyJson;
63
+ /**
64
+ * @description Converts the value in a best-fit primitive form
65
+ */
66
+ toPrimitive(): AnyJson;
63
67
  /**
64
68
  * @description Returns the base runtime type name for this instance
65
69
  */
@@ -81,5 +85,5 @@ export interface CodecClass<T = Codec> {
81
85
  readonly __fallbackType?: string;
82
86
  new (registry: Registry, ...args: any[]): T;
83
87
  }
84
- export declare type CodecTo = 'toHex' | 'toJSON' | 'toString' | 'toU8a';
88
+ export declare type CodecTo = 'toHex' | 'toJSON' | 'toPrimitive' | 'toString' | 'toU8a';
85
89
  export declare type ArgsDef = Record<string, CodecClass | string>;
@@ -24,6 +24,7 @@ export interface INumber extends Codec {
24
24
  toBigInt(): bigint;
25
25
  toBn(): BN;
26
26
  toNumber(): number;
27
+ toPrimitive(): string | number;
27
28
  }
28
29
  export interface IFloat extends Codec {
29
30
  toNumber(): number;
@@ -3,7 +3,7 @@
3
3
  import { u8aToHex } from '@polkadot/util';
4
4
  /** @internal */
5
5
 
6
- function formatFailure(registry, {
6
+ function formatFailure(registry, result, {
7
7
  message
8
8
  }, u8a, i, count, Type, key) {
9
9
  let type = '';
@@ -11,7 +11,8 @@ function formatFailure(registry, {
11
11
  try {
12
12
  type = `: ${new Type(registry).toRawType()}`;
13
13
  } catch {// ignore
14
- }
14
+ } // console.error(JSON.stringify(result, null, 2));
15
+
15
16
 
16
17
  return `decodeU8a: failed at ${u8aToHex(u8a.subarray(0, 16))}…${key ? ` on ${key}` : ''} (index ${i}/${count})${type}:: ${message}`;
17
18
  }
@@ -40,7 +41,7 @@ export function decodeU8a(registry, result, u8a, [Types, keys]) {
40
41
  i++;
41
42
  }
42
43
  } catch (error) {
43
- throw new Error(formatFailure(registry, error, u8a.subarray(offset), i, count, Types[i], keys[i]));
44
+ throw new Error(formatFailure(registry, result, error, u8a.subarray(offset), i, count, Types[i], keys[i]));
44
45
  }
45
46
 
46
47
  return [result, offset];
@@ -65,7 +66,7 @@ export function decodeU8aStruct(registry, result, u8a, [Types, keys]) {
65
66
  i++;
66
67
  }
67
68
  } catch (error) {
68
- throw new Error(formatFailure(registry, error, u8a.subarray(offset), i, count, Types[i], keys[i]));
69
+ throw new Error(formatFailure(registry, result, error, u8a.subarray(offset), i, count, Types[i], keys[i]));
69
70
  }
70
71
 
71
72
  return [result, offset];
@@ -90,7 +91,7 @@ export function decodeU8aVec(registry, result, u8a, startAt, Type) {
90
91
  i++;
91
92
  }
92
93
  } catch (error) {
93
- throw new Error(formatFailure(registry, error, u8a.subarray(offset), i, count, Type));
94
+ throw new Error(formatFailure(registry, result, error, u8a.subarray(offset), i, count, Type));
94
95
  }
95
96
 
96
97
  return [offset, offset - startAt];