@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/Compact.js CHANGED
@@ -1,12 +1,11 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { compactFromU8a, compactFromU8aLim, compactToU8a, isU8a } from '@polkadot/util';
4
5
  import { typeToConstructor } from "../utils/index.js";
5
-
6
6
  function noopSetDefinition(d) {
7
7
  return d;
8
8
  }
9
-
10
9
  function decodeCompact(registry, Type, value) {
11
10
  if (isU8a(value)) {
12
11
  const [decodedLength, bn] = (value[0] & 0b11) < 0b11 ? compactFromU8aLim(value) : compactFromU8a(value);
@@ -17,9 +16,9 @@ function decodeCompact(registry, Type, value) {
17
16
  } else if (value instanceof Type) {
18
17
  return [value, 0];
19
18
  }
20
-
21
19
  return [new Type(registry, value), 0];
22
20
  }
21
+
23
22
  /**
24
23
  * @name Compact
25
24
  * @description
@@ -28,12 +27,9 @@ function decodeCompact(registry, Type, value) {
28
27
  * used by other types to add length-prefixed encoding, or in the case of wrapped types, taking
29
28
  * a number and making the compact representation thereof
30
29
  */
31
-
32
-
33
30
  export class Compact {
34
31
  #Type;
35
32
  #raw;
36
-
37
33
  constructor(registry, Type, value = 0, {
38
34
  definition,
39
35
  setDefinition = noopSetDefinition
@@ -44,12 +40,11 @@ export class Compact {
44
40
  this.initialU8aLength = decodedLength;
45
41
  this.#raw = raw;
46
42
  }
47
-
48
43
  static with(Type) {
49
- let definition; // eslint-disable-next-line no-return-assign
44
+ let definition;
50
45
 
46
+ // eslint-disable-next-line no-return-assign
51
47
  const setDefinition = d => definition = d;
52
-
53
48
  return class extends Compact {
54
49
  constructor(registry, value) {
55
50
  super(registry, Type, value, {
@@ -57,148 +52,129 @@ export class Compact {
57
52
  setDefinition
58
53
  });
59
54
  }
60
-
61
55
  };
62
56
  }
57
+
63
58
  /**
64
59
  * @description The length of the value when encoded as a Uint8Array
65
60
  */
66
-
67
-
68
61
  get encodedLength() {
69
62
  return this.toU8a().length;
70
63
  }
64
+
71
65
  /**
72
66
  * @description returns a hash of the contents
73
67
  */
74
-
75
-
76
68
  get hash() {
77
69
  return this.registry.hash(this.toU8a());
78
70
  }
71
+
79
72
  /**
80
73
  * @description Checks if the value is an empty value
81
74
  */
82
-
83
-
84
75
  get isEmpty() {
85
76
  return this.#raw.isEmpty;
86
77
  }
78
+
87
79
  /**
88
80
  * @description Returns the number of bits in the value
89
81
  */
90
-
91
-
92
82
  bitLength() {
93
83
  return this.#raw.bitLength();
94
84
  }
85
+
95
86
  /**
96
87
  * @description Compares the value of the input to see if there is a match
97
88
  */
98
-
99
-
100
89
  eq(other) {
101
90
  return this.#raw.eq(other instanceof Compact ? other.#raw : other);
102
91
  }
92
+
103
93
  /**
104
94
  * @description Returns a breakdown of the hex encoding for this Codec
105
95
  */
106
-
107
-
108
96
  inspect() {
109
97
  return {
110
98
  outer: [this.toU8a()]
111
99
  };
112
100
  }
101
+
113
102
  /**
114
103
  * @description Returns a BigInt representation of the number
115
104
  */
116
-
117
-
118
105
  toBigInt() {
119
106
  return this.#raw.toBigInt();
120
107
  }
108
+
121
109
  /**
122
110
  * @description Returns the BN representation of the number
123
111
  */
124
-
125
-
126
112
  toBn() {
127
113
  return this.#raw.toBn();
128
114
  }
115
+
129
116
  /**
130
117
  * @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
131
118
  */
132
-
133
-
134
119
  toHex(isLe) {
135
120
  return this.#raw.toHex(isLe);
136
121
  }
122
+
137
123
  /**
138
124
  * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
139
125
  */
140
-
141
-
142
126
  toHuman(isExtended) {
143
127
  return this.#raw.toHuman(isExtended);
144
128
  }
129
+
145
130
  /**
146
131
  * @description Converts the Object to JSON, typically used for RPC transfers
147
132
  */
148
-
149
-
150
133
  toJSON() {
151
134
  return this.#raw.toJSON();
152
135
  }
136
+
153
137
  /**
154
138
  * @description Returns the number representation for the value
155
139
  */
156
-
157
-
158
140
  toNumber() {
159
141
  return this.#raw.toNumber();
160
142
  }
143
+
161
144
  /**
162
145
  * @description Converts the value in a best-fit primitive form
163
146
  */
164
-
165
-
166
147
  toPrimitive() {
167
148
  return this.#raw.toPrimitive();
168
149
  }
150
+
169
151
  /**
170
152
  * @description Returns the base runtime type name for this instance
171
153
  */
172
-
173
-
174
154
  toRawType() {
175
155
  return `Compact<${this.registry.getClassName(this.#Type) || this.#raw.toRawType()}>`;
176
156
  }
157
+
177
158
  /**
178
159
  * @description Returns the string representation of the value
179
160
  */
180
-
181
-
182
161
  toString() {
183
162
  return this.#raw.toString();
184
163
  }
164
+
185
165
  /**
186
166
  * @description Encodes the value as a Uint8Array as per the SCALE specifications
187
167
  * @param isBare true when the value has none of the type-specific prefixes (internal)
188
168
  */
189
169
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
190
-
191
-
192
170
  toU8a(isBare) {
193
171
  return compactToU8a(this.#raw.toBn());
194
172
  }
173
+
195
174
  /**
196
175
  * @description Returns the embedded [[UInt]] or [[Moment]] value
197
176
  */
198
-
199
-
200
177
  unwrap() {
201
178
  return this.#raw;
202
179
  }
203
-
204
180
  }
@@ -8,80 +8,64 @@
8
8
  */
9
9
  export class DoNotConstruct {
10
10
  #neverError;
11
-
12
11
  constructor(registry, typeName = 'DoNotConstruct') {
13
12
  this.registry = registry;
14
13
  this.#neverError = new Error(`DoNotConstruct: Cannot construct unknown type ${typeName}`);
15
14
  throw this.#neverError;
16
15
  }
17
-
18
16
  static with(typeName) {
19
17
  return class extends DoNotConstruct {
20
18
  constructor(registry) {
21
19
  super(registry, typeName);
22
20
  }
23
-
24
21
  };
25
22
  }
23
+
26
24
  /**
27
25
  * @description The length of the value when encoded as a Uint8Array
28
26
  */
29
-
30
-
31
27
  get encodedLength() {
32
28
  throw this.#neverError;
33
29
  }
30
+
34
31
  /**
35
32
  * @description returns a hash of the contents
36
33
  */
37
-
38
-
39
34
  get hash() {
40
35
  throw this.#neverError;
41
36
  }
37
+
42
38
  /**
43
39
  * @description Checks if the value is an empty value (always true)
44
40
  */
45
-
46
-
47
41
  get isEmpty() {
48
42
  throw this.#neverError;
49
43
  }
50
-
51
44
  eq() {
52
45
  throw this.#neverError;
53
46
  }
54
-
55
47
  inspect() {
56
48
  throw this.#neverError;
57
49
  }
58
-
59
50
  toHex() {
60
51
  throw this.#neverError;
61
52
  }
62
-
63
53
  toHuman() {
64
54
  throw this.#neverError;
65
55
  }
66
-
67
56
  toJSON() {
68
57
  throw this.#neverError;
69
58
  }
70
-
71
59
  toPrimitive() {
72
60
  throw this.#neverError;
73
61
  }
74
-
75
62
  toRawType() {
76
63
  throw this.#neverError;
77
64
  }
78
-
79
65
  toString() {
80
66
  throw this.#neverError;
81
67
  }
82
-
83
68
  toU8a() {
84
69
  throw this.#neverError;
85
70
  }
86
-
87
71
  }