@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/abstract/Array.js CHANGED
@@ -1,7 +1,9 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { compactToU8a, u8aConcatStrict, u8aToHex } from '@polkadot/util';
4
5
  import { compareArray } from "../utils/compareArray.js";
6
+
5
7
  /**
6
8
  * @name AbstractArray
7
9
  * @description
@@ -9,7 +11,6 @@ import { compareArray } from "../utils/compareArray.js";
9
11
  * specific encoding/decoding on top of the base type.
10
12
  * @noInheritDoc
11
13
  */
12
-
13
14
  export class AbstractArray extends Array {
14
15
  /**
15
16
  * @description This ensures that operators such as clice, filter, map, etc. return
@@ -18,177 +19,147 @@ export class AbstractArray extends Array {
18
19
  static get [Symbol.species]() {
19
20
  return Array;
20
21
  }
21
-
22
22
  constructor(registry, length) {
23
23
  super(length);
24
24
  this.registry = registry;
25
25
  }
26
+
26
27
  /**
27
28
  * @description The length of the value when encoded as a Uint8Array
28
29
  */
29
-
30
-
31
30
  get encodedLength() {
32
31
  // We need to loop through all entries since they may have a variable length themselves,
33
32
  // e.g. when a Vec or Compact is contained withing, it has a variable length based on data
34
33
  let total = compactToU8a(this.length).length;
35
-
36
34
  for (let i = 0; i < this.length; i++) {
37
35
  total += this[i].encodedLength;
38
36
  }
39
-
40
37
  return total;
41
38
  }
39
+
42
40
  /**
43
41
  * @description returns a hash of the contents
44
42
  */
45
-
46
-
47
43
  get hash() {
48
44
  return this.registry.hash(this.toU8a());
49
45
  }
46
+
50
47
  /**
51
48
  * @description Checks if the value is an empty value
52
49
  */
53
-
54
-
55
50
  get isEmpty() {
56
51
  return this.length === 0;
57
52
  }
53
+
58
54
  /**
59
55
  * @description The length of the value
60
56
  */
61
-
62
-
63
57
  get length() {
64
58
  // only included here since we ignore inherited docs
65
59
  return super.length;
66
60
  }
61
+
67
62
  /**
68
63
  * @description Compares the value of the input to see if there is a match
69
64
  */
70
-
71
-
72
65
  eq(other) {
73
66
  return compareArray(this, other);
74
67
  }
68
+
75
69
  /**
76
70
  * @description Returns a breakdown of the hex encoding for this Codec
77
71
  */
78
-
79
-
80
72
  inspect() {
81
73
  return {
82
74
  inner: this.inspectInner(),
83
75
  outer: [compactToU8a(this.length)]
84
76
  };
85
77
  }
86
-
87
78
  inspectInner() {
88
79
  const inner = new Array(this.length);
89
-
90
80
  for (let i = 0; i < this.length; i++) {
91
81
  inner[i] = this[i].inspect();
92
82
  }
93
-
94
83
  return inner;
95
84
  }
85
+
96
86
  /**
97
87
  * @description Converts the Object to an standard JavaScript Array
98
88
  */
99
-
100
-
101
89
  toArray() {
102
90
  return Array.from(this);
103
91
  }
92
+
104
93
  /**
105
94
  * @description Returns a hex string representation of the value
106
95
  */
107
-
108
-
109
96
  toHex() {
110
97
  return u8aToHex(this.toU8a());
111
98
  }
99
+
112
100
  /**
113
101
  * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
114
102
  */
115
-
116
-
117
103
  toHuman(isExtended) {
118
104
  const result = new Array(this.length);
119
-
120
105
  for (let i = 0; i < this.length; i++) {
121
106
  result[i] = this[i] && this[i].toHuman(isExtended);
122
107
  }
123
-
124
108
  return result;
125
109
  }
110
+
126
111
  /**
127
112
  * @description Converts the Object to JSON, typically used for RPC transfers
128
113
  */
129
-
130
-
131
114
  toJSON() {
132
115
  const result = new Array(this.length);
133
-
134
116
  for (let i = 0; i < this.length; i++) {
135
117
  // We actually log inside the U8a decoding and use JSON.stringify(...), which
136
118
  // means that the Vec may be partially populated (same applies to toHuman, same check)
137
119
  result[i] = this[i] && this[i].toJSON();
138
120
  }
139
-
140
121
  return result;
141
122
  }
123
+
142
124
  /**
143
125
  * @description Converts the value in a best-fit primitive form
144
126
  */
145
-
146
-
147
127
  toPrimitive() {
148
128
  const result = new Array(this.length);
149
-
150
129
  for (let i = 0; i < this.length; i++) {
151
130
  result[i] = this[i] && this[i].toPrimitive();
152
131
  }
153
-
154
132
  return result;
155
133
  }
134
+
156
135
  /**
157
136
  * @description Returns the base runtime type name for this instance
158
137
  */
159
138
 
160
-
161
139
  /**
162
140
  * @description Returns the string representation of the value
163
141
  */
164
142
  toString() {
165
143
  const result = new Array(this.length);
166
-
167
144
  for (let i = 0; i < this.length; i++) {
168
145
  result[i] = this[i].toString();
169
146
  }
170
-
171
147
  return `[${result.join(', ')}]`;
172
148
  }
149
+
173
150
  /**
174
151
  * @description Encodes the value as a Uint8Array as per the SCALE specifications
175
152
  * @param isBare true when the value has none of the type-specific prefixes (internal)
176
153
  */
177
-
178
-
179
154
  toU8a(isBare) {
180
155
  const encoded = this.toU8aInner();
181
156
  return isBare ? u8aConcatStrict(encoded) : u8aConcatStrict([compactToU8a(this.length), ...encoded]);
182
157
  }
183
-
184
158
  toU8aInner(isBare) {
185
159
  const encoded = new Array(this.length);
186
-
187
160
  for (let i = 0; i < this.length; i++) {
188
161
  encoded[i] = this[i].toU8a(isBare);
189
162
  }
190
-
191
163
  return encoded;
192
164
  }
193
-
194
165
  }
package/abstract/Base.js CHANGED
@@ -7,116 +7,100 @@
7
7
  */
8
8
  export class AbstractBase {
9
9
  #raw;
10
-
11
10
  constructor(registry, value, initialU8aLength) {
12
11
  this.#raw = value;
13
12
  this.initialU8aLength = initialU8aLength;
14
13
  this.registry = registry;
15
14
  }
15
+
16
16
  /**
17
17
  * @description The length of the value when encoded as a Uint8Array
18
18
  */
19
-
20
-
21
19
  get encodedLength() {
22
20
  return this.toU8a().length;
23
21
  }
22
+
24
23
  /**
25
24
  * @description returns a hash of the contents
26
25
  */
27
-
28
-
29
26
  get hash() {
30
27
  return this.registry.hash(this.toU8a());
31
28
  }
32
-
33
29
  get inner() {
34
30
  return this.#raw;
35
31
  }
32
+
36
33
  /**
37
34
  * @description Checks if the value is an empty value
38
35
  */
39
-
40
-
41
36
  get isEmpty() {
42
37
  return this.#raw.isEmpty;
43
38
  }
39
+
44
40
  /**
45
41
  * @description Compares the value of the input to see if there is a match
46
42
  */
47
-
48
-
49
43
  eq(other) {
50
44
  return this.#raw.eq(other);
51
45
  }
46
+
52
47
  /**
53
48
  * @description Returns a breakdown of the hex encoding for this Codec
54
49
  */
55
-
56
-
57
50
  inspect() {
58
51
  return this.#raw.inspect();
59
52
  }
53
+
60
54
  /**
61
55
  * @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
62
56
  */
63
-
64
-
65
57
  toHex(isLe) {
66
58
  return this.#raw.toHex(isLe);
67
59
  }
60
+
68
61
  /**
69
62
  * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
70
63
  */
71
-
72
-
73
64
  toHuman(isExtended) {
74
65
  return this.#raw.toHuman(isExtended);
75
66
  }
67
+
76
68
  /**
77
69
  * @description Converts the Object to JSON, typically used for RPC transfers
78
70
  */
79
-
80
-
81
71
  toJSON() {
82
72
  return this.#raw.toJSON();
83
73
  }
74
+
84
75
  /**
85
76
  * @description Converts the value in a best-fit primitive form
86
77
  */
87
-
88
-
89
78
  toPrimitive() {
90
79
  return this.#raw.toPrimitive();
91
80
  }
81
+
92
82
  /**
93
83
  * @description Returns the string representation of the value
94
84
  */
95
-
96
-
97
85
  toString() {
98
86
  return this.#raw.toString();
99
87
  }
88
+
100
89
  /**
101
90
  * @description Encodes the value as a Uint8Array as per the SCALE specifications
102
91
  * @param isBare true when the value has none of the type-specific prefixes (internal)
103
92
  */
104
-
105
-
106
93
  toU8a(isBare) {
107
94
  return this.#raw.toU8a(isBare);
108
95
  }
96
+
109
97
  /**
110
98
  * @description Returns the base runtime type name for this instance
111
99
  */
112
-
113
-
114
100
  toRawType() {
115
101
  return 'Base';
116
102
  }
117
-
118
103
  unwrap() {
119
104
  return this.#raw;
120
105
  }
121
-
122
106
  }
@@ -1,7 +1,9 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { BigInt } from '@polkadot/x-bigint'; // @ts-expect-error not extensible, we make it extensible here
4
3
 
4
+ import { BigInt } from '@polkadot/x-bigint';
5
+
6
+ // @ts-expect-error not extensible, we make it extensible here
5
7
  export class AbstractBigInt extends BigInt {
6
8
  // @ts-expect-error super() not required, the self magic does that
7
9
  constructor(value) {
@@ -9,5 +11,4 @@ export class AbstractBigInt extends BigInt {
9
11
  Object.setPrototypeOf(self, AbstractBigInt.prototype);
10
12
  return self;
11
13
  }
12
-
13
14
  }
package/abstract/Int.js CHANGED
@@ -1,25 +1,24 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isFunction, isHex, isNumber, isObject, isString, isU8a, u8aToBn, u8aToNumber } from '@polkadot/util';
4
- export const DEFAULT_UINT_BITS = 64; // Maximum allowed integer for JS is 2^53 - 1, set limit at 52
5
- // In this case however, we always print any >32 as hex
5
+ export const DEFAULT_UINT_BITS = 64;
6
6
 
7
+ // Maximum allowed integer for JS is 2^53 - 1, set limit at 52
8
+ // In this case however, we always print any >32 as hex
7
9
  const MAX_NUMBER_BITS = 52;
8
10
  const MUL_P = new BN(10000);
9
11
  const FORMATTERS = [['Perquintill', BN_QUINTILL], ['Perbill', BN_BILLION], ['Permill', BN_MILLION], ['Percent', BN_HUNDRED]];
10
-
11
12
  function toPercentage(value, divisor) {
12
13
  return `${(value.mul(MUL_P).div(divisor).toNumber() / 100).toFixed(2)}%`;
13
14
  }
14
- /** @internal */
15
-
16
15
 
16
+ /** @internal */
17
17
  function decodeAbstractInt(value, isNegative) {
18
18
  if (isNumber(value)) {
19
19
  if (!Number.isInteger(value) || value > Number.MAX_SAFE_INTEGER || value < Number.MIN_SAFE_INTEGER) {
20
20
  throw new Error('Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1');
21
21
  }
22
-
23
22
  return value;
24
23
  } else if (isString(value)) {
25
24
  if (isHex(value, -1, true)) {
@@ -28,11 +27,9 @@ function decodeAbstractInt(value, isNegative) {
28
27
  isNegative
29
28
  }).toString();
30
29
  }
31
-
32
30
  if (value.includes('.') || value.includes(',') || value.includes('e')) {
33
31
  throw new Error('String should not contain decimal points or scientific notation');
34
32
  }
35
-
36
33
  return value;
37
34
  } else if (isBn(value)) {
38
35
  return value.toString();
@@ -41,37 +38,31 @@ function decodeAbstractInt(value, isNegative) {
41
38
  // single key objects can be treated equivalently to numbers, assuming they meet the
42
39
  // specific requirements. (This is useful in Weights 1.5 where Objects are compact)
43
40
  const keys = Object.keys(value);
44
-
45
41
  if (keys.length !== 1) {
46
42
  throw new Error('Unable to construct number from multi-key object');
47
43
  }
48
-
49
44
  const inner = value[keys[0]];
50
-
51
45
  if (!isString(inner) && !isNumber(inner)) {
52
46
  throw new Error('Unable to construct from object with non-string/non-number value');
53
47
  }
54
-
55
48
  return decodeAbstractInt(inner, isNegative);
56
49
  }
57
-
58
50
  return bnToBn(value).toString();
59
51
  }
52
+
60
53
  /**
61
54
  * @name AbstractInt
62
55
  * @ignore
63
56
  * @noInheritDoc
64
57
  */
65
-
66
-
67
58
  export class AbstractInt extends BN {
68
59
  #bitLength;
69
-
70
60
  constructor(registry, value = 0, bitLength = DEFAULT_UINT_BITS, isSigned = false) {
71
61
  // Construct via a string/number, which will be passed in the BN constructor.
72
62
  // It would be ideal to actually return a BN, but there is an issue:
73
63
  // https://github.com/indutny/bn.js/issues/206
74
- super( // shortcut isU8a as used in SCALE decoding
64
+ super(
65
+ // shortcut isU8a as used in SCALE decoding
75
66
  isU8a(value) ? bitLength <= 48 ? u8aToNumber(value.subarray(0, bitLength / 8), {
76
67
  isNegative: isSigned
77
68
  }) : u8aToBn(value.subarray(0, bitLength / 8), {
@@ -84,43 +75,38 @@ export class AbstractInt extends BN {
84
75
  this.isUnsigned = !isSigned;
85
76
  const isNegative = this.isNeg();
86
77
  const maxBits = bitLength - (isSigned && !isNegative ? 1 : 0);
87
-
88
78
  if (isNegative && !isSigned) {
89
79
  throw new Error(`${this.toRawType()}: Negative number passed to unsigned type`);
90
80
  } else if (super.bitLength() > maxBits) {
91
81
  throw new Error(`${this.toRawType()}: Input too large. Found input with ${super.bitLength()} bits, expected ${maxBits}`);
92
82
  }
93
83
  }
84
+
94
85
  /**
95
86
  * @description returns a hash of the contents
96
87
  */
97
-
98
-
99
88
  get hash() {
100
89
  return this.registry.hash(this.toU8a());
101
90
  }
91
+
102
92
  /**
103
93
  * @description Checks if the value is a zero value (align elsewhere)
104
94
  */
105
-
106
-
107
95
  get isEmpty() {
108
96
  return this.isZero();
109
97
  }
98
+
110
99
  /**
111
100
  * @description Returns the number of bits in the value
112
101
  */
113
-
114
-
115
102
  bitLength() {
116
103
  return this.#bitLength;
117
104
  }
105
+
118
106
  /**
119
107
  * @description Compares the value of the input to see if there is a match
120
108
  */
121
109
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
122
-
123
-
124
110
  eq(other) {
125
111
  // Here we are actually overriding the built-in .eq to take care of both
126
112
  // number and BN inputs (no `.eqn` needed) - numbers will be converted
@@ -129,46 +115,41 @@ export class AbstractInt extends BN {
129
115
  isNegative: !this.isUnsigned
130
116
  }) : bnToBn(other));
131
117
  }
118
+
132
119
  /**
133
120
  * @description Returns a breakdown of the hex encoding for this Codec
134
121
  */
135
-
136
-
137
122
  inspect() {
138
123
  return {
139
124
  outer: [this.toU8a()]
140
125
  };
141
126
  }
127
+
142
128
  /**
143
129
  * @description True if this value is the max of the type
144
130
  */
145
-
146
-
147
131
  isMax() {
148
132
  const u8a = this.toU8a().filter(b => b === 0xff);
149
133
  return u8a.length === this.#bitLength / 8;
150
134
  }
135
+
151
136
  /**
152
137
  * @description Returns a BigInt representation of the number
153
138
  */
154
-
155
-
156
139
  toBigInt() {
157
140
  return BigInt(this.toString());
158
141
  }
142
+
159
143
  /**
160
144
  * @description Returns the BN representation of the number. (Compatibility)
161
145
  */
162
-
163
-
164
146
  toBn() {
165
147
  return this;
166
148
  }
149
+
167
150
  /**
168
151
  * @description Returns a hex string representation of the value
169
152
  */
170
-
171
-
172
153
  toHex(isLe = false) {
173
154
  // For display/JSON, this is BE, for compare, use isLe
174
155
  return bnToHex(this, {
@@ -177,17 +158,16 @@ export class AbstractInt extends BN {
177
158
  isNegative: !this.isUnsigned
178
159
  });
179
160
  }
161
+
180
162
  /**
181
163
  * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
182
164
  */
183
165
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
184
-
185
-
186
166
  toHuman(isExpanded) {
187
167
  const rawType = this.toRawType();
188
-
189
168
  if (rawType === 'Balance') {
190
- return this.isMax() ? 'everything' // FIXME In the case of multiples we need some way of detecting which instance this belongs
169
+ return this.isMax() ? 'everything'
170
+ // FIXME In the case of multiples we need some way of detecting which instance this belongs
191
171
  // to. as it stands we will always format (incorrectly) against the first token defined
192
172
  : formatBalance(this, {
193
173
  decimals: this.registry.chainDecimals[0],
@@ -195,15 +175,13 @@ export class AbstractInt extends BN {
195
175
  withUnit: this.registry.chainTokens[0]
196
176
  });
197
177
  }
198
-
199
178
  const [, divisor] = FORMATTERS.find(([type]) => type === rawType) || [];
200
179
  return divisor ? toPercentage(this, divisor) : formatNumber(this);
201
180
  }
181
+
202
182
  /**
203
183
  * @description Converts the Object to JSON, typically used for RPC transfers
204
184
  */
205
-
206
-
207
185
  toJSON(onlyHex = false) {
208
186
  // FIXME this return type should by string | number, however BN returns string
209
187
  // Options here are
@@ -211,42 +189,38 @@ export class AbstractInt extends BN {
211
189
  // - this.#bitLength - the type bits (this should be used, however contracts RPC is problematic)
212
190
  return onlyHex || super.bitLength() > MAX_NUMBER_BITS ? this.toHex() : this.toNumber();
213
191
  }
192
+
214
193
  /**
215
194
  * @description Returns the value in a primitive form, either number when <= 52 bits, or string otherwise
216
195
  */
217
-
218
-
219
196
  toPrimitive() {
220
197
  return super.bitLength() > MAX_NUMBER_BITS ? this.toString() : this.toNumber();
221
198
  }
199
+
222
200
  /**
223
201
  * @description Returns the base runtime type name for this instance
224
202
  */
225
-
226
-
227
203
  toRawType() {
228
204
  // NOTE In the case of balances, which have a special meaning on the UI
229
205
  // and can be interpreted differently, return a specific value for it so
230
206
  // underlying it always matches (no matter which length it actually is)
231
207
  return this instanceof this.registry.createClassUnsafe('Balance') ? 'Balance' : `${this.isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
232
208
  }
209
+
233
210
  /**
234
211
  * @description Returns the string representation of the value
235
212
  * @param base The base to use for the conversion
236
213
  */
237
-
238
-
239
214
  toString(base) {
240
215
  // only included here since we do not inherit docs
241
216
  return super.toString(base);
242
217
  }
218
+
243
219
  /**
244
220
  * @description Encodes the value as a Uint8Array as per the SCALE specifications
245
221
  * @param isBare true when the value has none of the type-specific prefixes (internal)
246
222
  */
247
223
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
248
-
249
-
250
224
  toU8a(isBare) {
251
225
  return bnToU8a(this, {
252
226
  bitLength: this.bitLength(),
@@ -254,5 +228,4 @@ export class AbstractInt extends BN {
254
228
  isNegative: !this.isUnsigned
255
229
  });
256
230
  }
257
-
258
231
  }
package/abstract/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  export { AbstractArray } from "./Array.js";
4
5
  export { AbstractBase } from "./Base.js";
5
6
  export { AbstractInt } from "./Int.js";