@polkadot/types-codec 8.12.2 → 8.14.2-11

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 (68) 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 -5
  12. package/base/Enum.js +31 -27
  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/base/Result.d.ts +0 -8
  18. package/base/Result.js +0 -16
  19. package/cjs/abstract/Array.js +18 -2
  20. package/cjs/abstract/Base.js +8 -0
  21. package/cjs/abstract/Int.js +8 -0
  22. package/cjs/base/Compact.js +8 -0
  23. package/cjs/base/DoNotConstruct.js +4 -0
  24. package/cjs/base/Enum.js +31 -27
  25. package/cjs/base/Null.js +8 -0
  26. package/cjs/base/Option.js +8 -0
  27. package/cjs/base/Result.js +0 -16
  28. package/cjs/extended/BTreeSet.js +14 -0
  29. package/cjs/extended/Map.js +17 -1
  30. package/cjs/extended/WrapperKeepOpaque.js +8 -0
  31. package/cjs/native/Bool.js +8 -0
  32. package/cjs/native/Date.js +8 -0
  33. package/cjs/native/Float.js +8 -0
  34. package/cjs/native/Json.js +12 -0
  35. package/cjs/native/Raw.js +16 -8
  36. package/cjs/native/Set.js +20 -9
  37. package/cjs/native/Struct.js +53 -27
  38. package/cjs/native/Text.js +8 -0
  39. package/cjs/package.json +1 -1
  40. package/cjs/packageInfo.js +1 -1
  41. package/cjs/utils/decodeU8a.js +6 -5
  42. package/extended/BTreeSet.d.ts +4 -0
  43. package/extended/BTreeSet.js +14 -0
  44. package/extended/Map.d.ts +4 -0
  45. package/extended/Map.js +16 -1
  46. package/extended/WrapperKeepOpaque.d.ts +4 -0
  47. package/extended/WrapperKeepOpaque.js +8 -0
  48. package/native/Bool.d.ts +4 -0
  49. package/native/Bool.js +8 -0
  50. package/native/Date.d.ts +4 -0
  51. package/native/Date.js +8 -0
  52. package/native/Float.d.ts +4 -0
  53. package/native/Float.js +8 -0
  54. package/native/Json.d.ts +4 -0
  55. package/native/Json.js +11 -0
  56. package/native/Raw.d.ts +4 -0
  57. package/native/Raw.js +16 -8
  58. package/native/Set.d.ts +4 -0
  59. package/native/Set.js +20 -9
  60. package/native/Struct.d.ts +14 -7
  61. package/native/Struct.js +53 -27
  62. package/native/Text.d.ts +4 -0
  63. package/native/Text.js +8 -0
  64. package/package.json +5 -5
  65. package/packageInfo.js +1 -1
  66. package/types/codec.d.ts +5 -1
  67. package/types/interfaces.d.ts +1 -0
  68. package/utils/decodeU8a.js +6 -5
package/native/Set.js CHANGED
@@ -97,20 +97,23 @@ export class CodecSet extends Set {
97
97
  }
98
98
 
99
99
  static with(values, bitLength) {
100
- const keys = Object.keys(values);
101
- const isKeys = new Array(keys.length);
100
+ var _class;
102
101
 
103
- for (let i = 0; i < keys.length; i++) {
104
- isKeys[i] = `is${stringPascalCase(keys[i])}`;
105
- }
106
-
107
- return class extends CodecSet {
102
+ return _class = class extends CodecSet {
108
103
  constructor(registry, value) {
109
104
  super(registry, values, value, bitLength);
110
- objectProperties(this, isKeys, (_, i) => this.strings.includes(keys[i]));
111
105
  }
112
106
 
113
- };
107
+ }, (() => {
108
+ const keys = Object.keys(values);
109
+ const isKeys = new Array(keys.length);
110
+
111
+ for (let i = 0; i < keys.length; i++) {
112
+ isKeys[i] = `is${stringPascalCase(keys[i])}`;
113
+ }
114
+
115
+ objectProperties(_class.prototype, isKeys, (_, i, self) => self.strings.includes(keys[i]));
116
+ })(), _class;
114
117
  }
115
118
  /**
116
119
  * @description The length of the value when encoded as a Uint8Array
@@ -226,6 +229,14 @@ export class CodecSet extends Set {
226
229
  toNumber() {
227
230
  return this.valueEncoded.toNumber();
228
231
  }
232
+ /**
233
+ * @description Converts the value in a best-fit primitive form
234
+ */
235
+
236
+
237
+ toPrimitive() {
238
+ return this.toJSON();
239
+ }
229
240
  /**
230
241
  * @description Returns the base runtime type name for this instance
231
242
  */
@@ -35,15 +35,10 @@ export declare class Struct<S extends TypesDef = TypesDef, V extends {
35
35
  * @description The available keys for this struct
36
36
  */
37
37
  get defKeys(): string[];
38
- getT<T>(key: string): T;
39
38
  /**
40
39
  * @description Checks if the value is an empty value
41
40
  */
42
41
  get isEmpty(): boolean;
43
- /**
44
- * @description Returns the Type description of the structure
45
- */
46
- get Type(): E;
47
42
  /**
48
43
  * @description The length of the value when encoded as a Uint8Array
49
44
  */
@@ -52,19 +47,27 @@ export declare class Struct<S extends TypesDef = TypesDef, V extends {
52
47
  * @description returns a hash of the contents
53
48
  */
54
49
  get hash(): IU8a;
50
+ /**
51
+ * @description Returns the Type description of the structure
52
+ */
53
+ get Type(): E;
55
54
  /**
56
55
  * @description Compares the value of the input to see if there is a match
57
56
  */
58
57
  eq(other?: unknown): boolean;
59
58
  /**
60
59
  * @description Returns a specific names entry in the structure
61
- * @param name The name of the entry to retrieve
60
+ * @param key The name of the entry to retrieve
62
61
  */
63
- get(name: keyof S): Codec | undefined;
62
+ get(key: keyof S): Codec | undefined;
64
63
  /**
65
64
  * @description Returns the values of a member at a specific index (Rather use get(name) for performance)
66
65
  */
67
66
  getAtIndex(index: number): Codec;
67
+ /**
68
+ * @description Returns the a types value by name
69
+ */
70
+ getT<T>(key: string): T;
68
71
  /**
69
72
  * @description Returns a breakdown of the hex encoding for this Codec
70
73
  */
@@ -85,6 +88,10 @@ export declare class Struct<S extends TypesDef = TypesDef, V extends {
85
88
  * @description Converts the Object to JSON, typically used for RPC transfers
86
89
  */
87
90
  toJSON(): Record<string, AnyJson>;
91
+ /**
92
+ * @description Converts the value in a best-fit primitive form
93
+ */
94
+ toPrimitive(): Record<string, AnyJson>;
88
95
  /**
89
96
  * @description Returns the base runtime type name for this instance
90
97
  */
package/native/Struct.js CHANGED
@@ -95,21 +95,24 @@ export class Struct extends Map {
95
95
  }
96
96
 
97
97
  static with(Types, jsonMap) {
98
- const keys = Object.keys(Types);
98
+ var _class;
99
+
99
100
  let definition; // eslint-disable-next-line no-return-assign
100
101
 
101
102
  const setDefinition = d => definition = d;
102
103
 
103
- return class extends Struct {
104
+ return _class = class extends Struct {
104
105
  constructor(registry, value) {
105
106
  super(registry, Types, value, jsonMap, {
106
107
  definition,
107
108
  setDefinition
108
109
  });
109
- objectProperties(this, keys, k => this.get(k));
110
110
  }
111
111
 
112
- };
112
+ }, (() => {
113
+ const keys = Object.keys(Types);
114
+ objectProperties(_class.prototype, keys, (k, _, self) => self.get(k));
115
+ })(), _class;
113
116
  }
114
117
  /**
115
118
  * @description The available keys for this struct
@@ -119,10 +122,6 @@ export class Struct extends Map {
119
122
  get defKeys() {
120
123
  return this.#Types[1];
121
124
  }
122
-
123
- getT(key) {
124
- return this.get(key);
125
- }
126
125
  /**
127
126
  * @description Checks if the value is an empty value
128
127
  */
@@ -137,21 +136,6 @@ export class Struct extends Map {
137
136
 
138
137
  return true;
139
138
  }
140
- /**
141
- * @description Returns the Type description of the structure
142
- */
143
-
144
-
145
- get Type() {
146
- const result = {};
147
- const [Types, keys] = this.#Types;
148
-
149
- for (let i = 0; i < keys.length; i++) {
150
- result[keys[i]] = new Types[i](this.registry).toRawType();
151
- }
152
-
153
- return result;
154
- }
155
139
  /**
156
140
  * @description The length of the value when encoded as a Uint8Array
157
141
  */
@@ -174,6 +158,21 @@ export class Struct extends Map {
174
158
  get hash() {
175
159
  return this.registry.hash(this.toU8a());
176
160
  }
161
+ /**
162
+ * @description Returns the Type description of the structure
163
+ */
164
+
165
+
166
+ get Type() {
167
+ const result = {};
168
+ const [Types, keys] = this.#Types;
169
+
170
+ for (let i = 0; i < keys.length; i++) {
171
+ result[keys[i]] = new Types[i](this.registry).toRawType();
172
+ }
173
+
174
+ return result;
175
+ }
177
176
  /**
178
177
  * @description Compares the value of the input to see if there is a match
179
178
  */
@@ -184,12 +183,12 @@ export class Struct extends Map {
184
183
  }
185
184
  /**
186
185
  * @description Returns a specific names entry in the structure
187
- * @param name The name of the entry to retrieve
186
+ * @param key The name of the entry to retrieve
188
187
  */
189
188
 
190
189
 
191
- get(name) {
192
- return super.get(name);
190
+ get(key) {
191
+ return super.get(key);
193
192
  }
194
193
  /**
195
194
  * @description Returns the values of a member at a specific index (Rather use get(name) for performance)
@@ -199,6 +198,14 @@ export class Struct extends Map {
199
198
  getAtIndex(index) {
200
199
  return this.toArray()[index];
201
200
  }
201
+ /**
202
+ * @description Returns the a types value by name
203
+ */
204
+
205
+
206
+ getT(key) {
207
+ return super.get(key);
208
+ }
202
209
  /**
203
210
  * @description Returns a breakdown of the hex encoding for this Codec
204
211
  */
@@ -256,12 +263,31 @@ export class Struct extends Map {
256
263
  const json = {};
257
264
 
258
265
  for (const [k, v] of this.entries()) {
259
- const jsonKey = this.#jsonMap.get(k) || k;
266
+ const jsonKey = this.#jsonMap.get(k) || k; // We actually log inside the U8a decoding and use JSON.stringify(...), which
267
+ // means that the Vec may be partially populated (same applies to toHuman, same check)
268
+
260
269
  json[jsonKey] = v && v.toJSON();
261
270
  }
262
271
 
263
272
  return json;
264
273
  }
274
+ /**
275
+ * @description Converts the value in a best-fit primitive form
276
+ */
277
+
278
+
279
+ toPrimitive() {
280
+ const json = {};
281
+
282
+ for (const [k, v] of this.entries()) {
283
+ const jsonKey = this.#jsonMap.get(k) || k; // We actually log inside the U8a decoding and use JSON.stringify(...), which
284
+ // means that the Vec may be partially populated (same applies to toHuman, same check)
285
+
286
+ json[jsonKey] = v && v.toPrimitive();
287
+ }
288
+
289
+ return json;
290
+ }
265
291
  /**
266
292
  * @description Returns the base runtime type name for this instance
267
293
  */
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.2",
23
+ "version": "8.14.2-11",
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.2",
439
- "@polkadot/x-bigint": "^10.0.2"
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.2'
8
+ version: '8.14.2-11'
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];