@polkadot/types-codec 6.12.2-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 (214) hide show
  1. package/abstract/AbstractArray.cjs +208 -0
  2. package/abstract/AbstractArray.d.ts +84 -0
  3. package/abstract/AbstractArray.js +197 -0
  4. package/abstract/AbstractInt.cjs +254 -0
  5. package/abstract/AbstractInt.d.ts +75 -0
  6. package/abstract/AbstractInt.js +231 -0
  7. package/abstract/index.cjs +21 -0
  8. package/abstract/index.d.ts +2 -0
  9. package/abstract/index.js +4 -0
  10. package/base/Base.cjs +109 -0
  11. package/base/Base.d.ts +54 -0
  12. package/base/Base.js +100 -0
  13. package/base/Compact.cjs +201 -0
  14. package/base/Compact.d.ts +83 -0
  15. package/base/Compact.js +186 -0
  16. package/base/DoNotConstruct.cjs +101 -0
  17. package/base/DoNotConstruct.d.ts +33 -0
  18. package/base/DoNotConstruct.js +88 -0
  19. package/base/Enum.cjs +437 -0
  20. package/base/Enum.d.ts +101 -0
  21. package/base/Enum.js +413 -0
  22. package/base/Int.cjs +45 -0
  23. package/base/Int.d.ts +16 -0
  24. package/base/Int.js +33 -0
  25. package/base/Null.cjs +111 -0
  26. package/base/Null.d.ts +54 -0
  27. package/base/Null.js +101 -0
  28. package/base/Option.cjs +256 -0
  29. package/base/Option.d.ts +88 -0
  30. package/base/Option.js +240 -0
  31. package/base/Result.cjs +109 -0
  32. package/base/Result.d.ts +46 -0
  33. package/base/Result.js +98 -0
  34. package/base/Tuple.cjs +132 -0
  35. package/base/Tuple.d.ts +39 -0
  36. package/base/Tuple.js +116 -0
  37. package/base/UInt.cjs +39 -0
  38. package/base/UInt.d.ts +15 -0
  39. package/base/UInt.js +29 -0
  40. package/base/Vec.cjs +126 -0
  41. package/base/Vec.d.ts +28 -0
  42. package/base/Vec.js +104 -0
  43. package/base/VecAny.cjs +31 -0
  44. package/base/VecAny.d.ts +15 -0
  45. package/base/VecAny.js +21 -0
  46. package/base/VecFixed.cjs +111 -0
  47. package/base/VecFixed.d.ts +26 -0
  48. package/base/VecFixed.js +93 -0
  49. package/base/index.cjs +109 -0
  50. package/base/index.d.ts +13 -0
  51. package/base/index.js +15 -0
  52. package/bundle.cjs +100 -0
  53. package/bundle.d.ts +7 -0
  54. package/bundle.js +9 -0
  55. package/detectPackage.cjs +9 -0
  56. package/detectPackage.d.ts +1 -0
  57. package/detectPackage.js +5 -0
  58. package/extended/BTreeMap.cjs +24 -0
  59. package/extended/BTreeMap.d.ts +5 -0
  60. package/extended/BTreeMap.js +14 -0
  61. package/extended/BTreeSet.cjs +227 -0
  62. package/extended/BTreeSet.d.ts +55 -0
  63. package/extended/BTreeSet.js +212 -0
  64. package/extended/BitVec.cjs +97 -0
  65. package/extended/BitVec.d.ts +26 -0
  66. package/extended/BitVec.js +82 -0
  67. package/extended/Bytes.cjs +86 -0
  68. package/extended/Bytes.d.ts +25 -0
  69. package/extended/Bytes.js +75 -0
  70. package/extended/HashMap.cjs +24 -0
  71. package/extended/HashMap.d.ts +5 -0
  72. package/extended/HashMap.js +14 -0
  73. package/extended/Linkage.cjs +89 -0
  74. package/extended/Linkage.d.ts +31 -0
  75. package/extended/Linkage.js +71 -0
  76. package/extended/Map.cjs +244 -0
  77. package/extended/Map.d.ts +50 -0
  78. package/extended/Map.js +225 -0
  79. package/extended/Range.cjs +92 -0
  80. package/extended/Range.d.ts +30 -0
  81. package/extended/Range.js +75 -0
  82. package/extended/Type.cjs +39 -0
  83. package/extended/Type.d.ts +16 -0
  84. package/extended/Type.js +27 -0
  85. package/extended/U8aFixed.cjs +66 -0
  86. package/extended/U8aFixed.d.ts +16 -0
  87. package/extended/U8aFixed.js +53 -0
  88. package/extended/WrapperOpaque.cjs +68 -0
  89. package/extended/WrapperOpaque.d.ts +18 -0
  90. package/extended/WrapperOpaque.js +57 -0
  91. package/extended/index.cjs +105 -0
  92. package/extended/index.d.ts +11 -0
  93. package/extended/index.js +13 -0
  94. package/index.cjs +20 -0
  95. package/index.d.ts +2 -0
  96. package/index.js +4 -0
  97. package/native/Bool.cjs +141 -0
  98. package/native/Bool.d.ts +62 -0
  99. package/native/Bool.js +130 -0
  100. package/native/Date.cjs +170 -0
  101. package/native/Date.d.ts +76 -0
  102. package/native/Date.js +158 -0
  103. package/native/Json.cjs +133 -0
  104. package/native/Json.d.ts +55 -0
  105. package/native/Json.js +117 -0
  106. package/native/Raw.cjs +192 -0
  107. package/native/Raw.d.ts +90 -0
  108. package/native/Raw.js +182 -0
  109. package/native/Set.cjs +268 -0
  110. package/native/Set.d.ts +76 -0
  111. package/native/Set.js +251 -0
  112. package/native/Struct.cjs +289 -0
  113. package/native/Struct.d.ts +92 -0
  114. package/native/Struct.js +273 -0
  115. package/native/Text.cjs +193 -0
  116. package/native/Text.d.ts +71 -0
  117. package/native/Text.js +178 -0
  118. package/native/index.cjs +79 -0
  119. package/native/index.d.ts +7 -0
  120. package/native/index.js +9 -0
  121. package/package.json +387 -0
  122. package/packageInfo.cjs +14 -0
  123. package/packageInfo.d.ts +4 -0
  124. package/packageInfo.js +7 -0
  125. package/primitive/I128.cjs +26 -0
  126. package/primitive/I128.d.ts +11 -0
  127. package/primitive/I128.js +16 -0
  128. package/primitive/I16.cjs +26 -0
  129. package/primitive/I16.d.ts +11 -0
  130. package/primitive/I16.js +16 -0
  131. package/primitive/I256.cjs +26 -0
  132. package/primitive/I256.d.ts +11 -0
  133. package/primitive/I256.js +16 -0
  134. package/primitive/I32.cjs +26 -0
  135. package/primitive/I32.d.ts +11 -0
  136. package/primitive/I32.js +16 -0
  137. package/primitive/I64.cjs +26 -0
  138. package/primitive/I64.d.ts +11 -0
  139. package/primitive/I64.js +16 -0
  140. package/primitive/I8.cjs +26 -0
  141. package/primitive/I8.d.ts +11 -0
  142. package/primitive/I8.js +16 -0
  143. package/primitive/U128.cjs +26 -0
  144. package/primitive/U128.d.ts +11 -0
  145. package/primitive/U128.js +16 -0
  146. package/primitive/U16.cjs +26 -0
  147. package/primitive/U16.d.ts +11 -0
  148. package/primitive/U16.js +16 -0
  149. package/primitive/U256.cjs +26 -0
  150. package/primitive/U256.d.ts +11 -0
  151. package/primitive/U256.js +16 -0
  152. package/primitive/U32.cjs +26 -0
  153. package/primitive/U32.d.ts +11 -0
  154. package/primitive/U32.js +16 -0
  155. package/primitive/U64.cjs +26 -0
  156. package/primitive/U64.d.ts +11 -0
  157. package/primitive/U64.js +16 -0
  158. package/primitive/U8.cjs +26 -0
  159. package/primitive/U8.d.ts +11 -0
  160. package/primitive/U8.js +16 -0
  161. package/primitive/USize.cjs +28 -0
  162. package/primitive/USize.d.ts +12 -0
  163. package/primitive/USize.js +18 -0
  164. package/primitive/index.cjs +187 -0
  165. package/primitive/index.d.ts +13 -0
  166. package/primitive/index.js +15 -0
  167. package/types/codec.cjs +1 -0
  168. package/types/codec.d.ts +77 -0
  169. package/types/codec.js +1 -0
  170. package/types/helpers.cjs +1 -0
  171. package/types/helpers.d.ts +14 -0
  172. package/types/helpers.js +1 -0
  173. package/types/index.cjs +57 -0
  174. package/types/index.d.ts +4 -0
  175. package/types/index.js +6 -0
  176. package/types/interfaces.cjs +1 -0
  177. package/types/interfaces.d.ts +68 -0
  178. package/types/interfaces.js +1 -0
  179. package/types/registry.cjs +1 -0
  180. package/types/registry.d.ts +44 -0
  181. package/types/registry.js +1 -0
  182. package/utils/compareArray.cjs +22 -0
  183. package/utils/compareArray.d.ts +1 -0
  184. package/utils/compareArray.js +13 -0
  185. package/utils/compareMap.cjs +39 -0
  186. package/utils/compareMap.d.ts +1 -0
  187. package/utils/compareMap.js +31 -0
  188. package/utils/compareSet.cjs +29 -0
  189. package/utils/compareSet.d.ts +1 -0
  190. package/utils/compareSet.js +22 -0
  191. package/utils/decodeU8a.cjs +69 -0
  192. package/utils/decodeU8a.d.ts +13 -0
  193. package/utils/decodeU8a.js +60 -0
  194. package/utils/index.cjs +95 -0
  195. package/utils/index.d.ts +9 -0
  196. package/utils/index.js +11 -0
  197. package/utils/mapToTypeMap.cjs +25 -0
  198. package/utils/mapToTypeMap.d.ts +5 -0
  199. package/utils/mapToTypeMap.js +17 -0
  200. package/utils/sanitize.cjs +242 -0
  201. package/utils/sanitize.d.ts +16 -0
  202. package/utils/sanitize.js +215 -0
  203. package/utils/sortValues.cjs +89 -0
  204. package/utils/sortValues.d.ts +13 -0
  205. package/utils/sortValues.js +73 -0
  206. package/utils/typeToConstructor.cjs +14 -0
  207. package/utils/typeToConstructor.d.ts +2 -0
  208. package/utils/typeToConstructor.js +6 -0
  209. package/utils/typesToMap.cjs +20 -0
  210. package/utils/typesToMap.d.ts +2 -0
  211. package/utils/typesToMap.js +13 -0
  212. package/utils/util.cjs +15 -0
  213. package/utils/util.d.ts +3 -0
  214. package/utils/util.js +7 -0
package/base/Option.js ADDED
@@ -0,0 +1,240 @@
1
+ import _classPrivateFieldLooseBase from "@babel/runtime/helpers/esm/classPrivateFieldLooseBase";
2
+ import _classPrivateFieldLooseKey from "@babel/runtime/helpers/esm/classPrivateFieldLooseKey";
3
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
4
+ // SPDX-License-Identifier: Apache-2.0
5
+ import { assert, isCodec, isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
6
+ import { typeToConstructor } from "../utils/index.js";
7
+ import { Null } from "./Null.js";
8
+ /** @internal */
9
+
10
+ function decodeOption(registry, Type, value) {
11
+ // In the case of an option, unwrap the inner
12
+ if (value instanceof Option) {
13
+ value = value.value;
14
+ }
15
+
16
+ if (isNull(value) || isUndefined(value) || value instanceof Null || value === '0x') {
17
+ return new Null(registry);
18
+ } else if (value instanceof Type) {
19
+ // don't re-create, use as it (which also caters for derived types)
20
+ return value;
21
+ } else if (isU8a(value)) {
22
+ // the isU8a check happens last in the if-tree - since the wrapped value
23
+ // may be an instance of it, so Type and Option checks go in first
24
+ return !value.length || value[0] === 0 ? new Null(registry) : new Type(registry, value.subarray(1));
25
+ }
26
+
27
+ return new Type(registry, value);
28
+ }
29
+ /**
30
+ * @name Option
31
+ * @description
32
+ * An Option is an optional field. Basically the first byte indicates that there is
33
+ * is value to follow. If the byte is `1` there is an actual value. So the Option
34
+ * implements that - decodes, checks for optionality and wraps the required structure
35
+ * with a value if/as required/found.
36
+ */
37
+
38
+
39
+ var _Type = /*#__PURE__*/_classPrivateFieldLooseKey("Type");
40
+
41
+ var _initialU8aLength = /*#__PURE__*/_classPrivateFieldLooseKey("initialU8aLength");
42
+
43
+ var _raw = /*#__PURE__*/_classPrivateFieldLooseKey("raw");
44
+
45
+ export class Option {
46
+ constructor(registry, typeName, value) {
47
+ this.registry = void 0;
48
+ this.createdAtHash = void 0;
49
+ Object.defineProperty(this, _Type, {
50
+ writable: true,
51
+ value: void 0
52
+ });
53
+ Object.defineProperty(this, _initialU8aLength, {
54
+ writable: true,
55
+ value: void 0
56
+ });
57
+ Object.defineProperty(this, _raw, {
58
+ writable: true,
59
+ value: void 0
60
+ });
61
+ const Type = typeToConstructor(registry, typeName);
62
+ const decoded = isU8a(value) && value.length && !isCodec(value) ? value[0] === 0 ? new Null(registry) : new Type(registry, value.subarray(1)) : decodeOption(registry, Type, value);
63
+ this.registry = registry;
64
+ _classPrivateFieldLooseBase(this, _Type)[_Type] = Type;
65
+ _classPrivateFieldLooseBase(this, _raw)[_raw] = decoded;
66
+
67
+ if (decoded.initialU8aLength) {
68
+ _classPrivateFieldLooseBase(this, _initialU8aLength)[_initialU8aLength] = 1 + decoded.initialU8aLength;
69
+ }
70
+ }
71
+
72
+ static with(Type) {
73
+ return class extends Option {
74
+ constructor(registry, value) {
75
+ super(registry, Type, value);
76
+ }
77
+
78
+ };
79
+ }
80
+ /**
81
+ * @description The length of the value when encoded as a Uint8Array
82
+ */
83
+
84
+
85
+ get encodedLength() {
86
+ // boolean byte (has value, doesn't have) along with wrapped length
87
+ return 1 + _classPrivateFieldLooseBase(this, _raw)[_raw].encodedLength;
88
+ }
89
+ /**
90
+ * @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
91
+ */
92
+
93
+
94
+ get initialU8aLength() {
95
+ return _classPrivateFieldLooseBase(this, _initialU8aLength)[_initialU8aLength];
96
+ }
97
+ /**
98
+ * @description returns a hash of the contents
99
+ */
100
+
101
+
102
+ get hash() {
103
+ return this.registry.hash(this.toU8a());
104
+ }
105
+ /**
106
+ * @description Checks if the Option has no value
107
+ */
108
+
109
+
110
+ get isEmpty() {
111
+ return this.isNone;
112
+ }
113
+ /**
114
+ * @description Checks if the Option has no value
115
+ */
116
+
117
+
118
+ get isNone() {
119
+ return _classPrivateFieldLooseBase(this, _raw)[_raw] instanceof Null;
120
+ }
121
+ /**
122
+ * @description Checks if the Option has a value
123
+ */
124
+
125
+
126
+ get isSome() {
127
+ return !this.isNone;
128
+ }
129
+ /**
130
+ * @description The actual value for the Option
131
+ */
132
+
133
+
134
+ get value() {
135
+ return _classPrivateFieldLooseBase(this, _raw)[_raw];
136
+ }
137
+ /**
138
+ * @description Compares the value of the input to see if there is a match
139
+ */
140
+
141
+
142
+ eq(other) {
143
+ if (other instanceof Option) {
144
+ return this.isSome === other.isSome && this.value.eq(other.value);
145
+ }
146
+
147
+ return this.value.eq(other);
148
+ }
149
+ /**
150
+ * @description Returns a hex string representation of the value
151
+ */
152
+
153
+
154
+ toHex() {
155
+ // This attempts to align with the JSON encoding - actually in this case
156
+ // the isSome value is correct, however the `isNone` may be problematic
157
+ return this.isNone ? '0x' : u8aToHex(this.toU8a().subarray(1));
158
+ }
159
+ /**
160
+ * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
161
+ */
162
+
163
+
164
+ toHuman(isExtended) {
165
+ return _classPrivateFieldLooseBase(this, _raw)[_raw].toHuman(isExtended);
166
+ }
167
+ /**
168
+ * @description Converts the Object to JSON, typically used for RPC transfers
169
+ */
170
+
171
+
172
+ toJSON() {
173
+ return this.isNone ? null : _classPrivateFieldLooseBase(this, _raw)[_raw].toJSON();
174
+ }
175
+ /**
176
+ * @description Returns the base runtime type name for this instance
177
+ */
178
+
179
+
180
+ toRawType(isBare) {
181
+ const wrapped = this.registry.getClassName(_classPrivateFieldLooseBase(this, _Type)[_Type]) || new (_classPrivateFieldLooseBase(this, _Type)[_Type])(this.registry).toRawType();
182
+ return isBare ? wrapped : `Option<${wrapped}>`;
183
+ }
184
+ /**
185
+ * @description Returns the string representation of the value
186
+ */
187
+
188
+
189
+ toString() {
190
+ return _classPrivateFieldLooseBase(this, _raw)[_raw].toString();
191
+ }
192
+ /**
193
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
194
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
195
+ */
196
+
197
+
198
+ toU8a(isBare) {
199
+ if (isBare) {
200
+ return _classPrivateFieldLooseBase(this, _raw)[_raw].toU8a(true);
201
+ }
202
+
203
+ const u8a = new Uint8Array(this.encodedLength);
204
+
205
+ if (this.isSome) {
206
+ u8a.set([1]);
207
+ u8a.set(_classPrivateFieldLooseBase(this, _raw)[_raw].toU8a(), 1);
208
+ }
209
+
210
+ return u8a;
211
+ }
212
+ /**
213
+ * @description Returns the value that the Option represents (if available), throws if null
214
+ */
215
+
216
+
217
+ unwrap() {
218
+ assert(this.isSome, 'Option: unwrapping a None value');
219
+ return _classPrivateFieldLooseBase(this, _raw)[_raw];
220
+ }
221
+ /**
222
+ * @description Returns the value that the Option represents (if available) or defaultValue if none
223
+ * @param defaultValue The value to return if the option isNone
224
+ */
225
+
226
+
227
+ unwrapOr(defaultValue) {
228
+ return this.isSome ? this.unwrap() : defaultValue;
229
+ }
230
+ /**
231
+ * @description Returns the value that the Option represents (if available) or defaultValue if none
232
+ * @param defaultValue The value to return if the option isNone
233
+ */
234
+
235
+
236
+ unwrapOrDefault() {
237
+ return this.isSome ? this.unwrap() : new (_classPrivateFieldLooseBase(this, _Type)[_Type])(this.registry);
238
+ }
239
+
240
+ }
@@ -0,0 +1,109 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Result = void 0;
7
+
8
+ var _util = require("@polkadot/util");
9
+
10
+ var _Enum = require("./Enum.cjs");
11
+
12
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
13
+ // SPDX-License-Identifier: Apache-2.0
14
+
15
+ /**
16
+ * @name Result
17
+ * @description
18
+ * A Result maps to the Rust Result type, that can either wrap a success or error value
19
+ */
20
+ class Result extends _Enum.Enum {
21
+ constructor(registry, Ok, Err, value) {
22
+ // NOTE This is order-dependent, Ok (with index 0) needs to be first
23
+ // eslint-disable-next-line sort-keys
24
+ super(registry, {
25
+ Ok,
26
+ Err
27
+ }, value);
28
+ }
29
+
30
+ static with(Types) {
31
+ return class extends Result {
32
+ constructor(registry, value) {
33
+ super(registry, Types.Ok, Types.Err, value);
34
+ }
35
+
36
+ };
37
+ }
38
+ /**
39
+ * @description Returns the wrapper Err value (if isErr)
40
+ */
41
+
42
+
43
+ get asErr() {
44
+ (0, _util.assert)(this.isErr, 'Cannot extract Err value from Ok result, check isErr first');
45
+ return this.value;
46
+ }
47
+ /**
48
+ * @deprecated Use asErr
49
+ */
50
+
51
+
52
+ get asError() {
53
+ return this.asErr;
54
+ }
55
+ /**
56
+ * @description Returns the wrapper Ok value (if isOk)
57
+ */
58
+
59
+
60
+ get asOk() {
61
+ (0, _util.assert)(this.isOk, 'Cannot extract Ok value from Err result, check isOk first');
62
+ return this.value;
63
+ }
64
+ /**
65
+ * @description Checks if the Result has no value
66
+ */
67
+
68
+
69
+ get isEmpty() {
70
+ return this.isOk && this.value.isEmpty;
71
+ }
72
+ /**
73
+ * @description Checks if the Result wraps an Err value
74
+ */
75
+
76
+
77
+ get isErr() {
78
+ return !this.isOk;
79
+ }
80
+ /**
81
+ * @deprecated Use isErr
82
+ */
83
+
84
+
85
+ get isError() {
86
+ return this.isErr;
87
+ }
88
+ /**
89
+ * @description Checks if the Result wraps an Ok value
90
+ */
91
+
92
+
93
+ get isOk() {
94
+ return this.index === 0;
95
+ }
96
+ /**
97
+ * @description Returns the base runtime type name for this instance
98
+ */
99
+
100
+
101
+ toRawType() {
102
+ const Types = this._toRawStruct();
103
+
104
+ return `Result<${Types.Ok},${Types.Err}>`;
105
+ }
106
+
107
+ }
108
+
109
+ exports.Result = Result;
@@ -0,0 +1,46 @@
1
+ import type { Codec, CodecClass, CodecRegistry, IResult } from '../types';
2
+ import { Enum } from './Enum';
3
+ /**
4
+ * @name Result
5
+ * @description
6
+ * A Result maps to the Rust Result type, that can either wrap a success or error value
7
+ */
8
+ export declare class Result<O extends Codec, E extends Codec> extends Enum implements IResult<O, E> {
9
+ constructor(registry: CodecRegistry, Ok: CodecClass<O> | string, Err: CodecClass<E> | string, value?: unknown);
10
+ static with<O extends Codec, E extends Codec>(Types: {
11
+ Ok: CodecClass<O> | string;
12
+ Err: CodecClass<E> | string;
13
+ }): CodecClass<Result<O, E>>;
14
+ /**
15
+ * @description Returns the wrapper Err value (if isErr)
16
+ */
17
+ get asErr(): E;
18
+ /**
19
+ * @deprecated Use asErr
20
+ */
21
+ get asError(): E;
22
+ /**
23
+ * @description Returns the wrapper Ok value (if isOk)
24
+ */
25
+ get asOk(): O;
26
+ /**
27
+ * @description Checks if the Result has no value
28
+ */
29
+ get isEmpty(): boolean;
30
+ /**
31
+ * @description Checks if the Result wraps an Err value
32
+ */
33
+ get isErr(): boolean;
34
+ /**
35
+ * @deprecated Use isErr
36
+ */
37
+ get isError(): boolean;
38
+ /**
39
+ * @description Checks if the Result wraps an Ok value
40
+ */
41
+ get isOk(): boolean;
42
+ /**
43
+ * @description Returns the base runtime type name for this instance
44
+ */
45
+ toRawType(): string;
46
+ }
package/base/Result.js ADDED
@@ -0,0 +1,98 @@
1
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { assert } from '@polkadot/util';
4
+ import { Enum } from "./Enum.js";
5
+ /**
6
+ * @name Result
7
+ * @description
8
+ * A Result maps to the Rust Result type, that can either wrap a success or error value
9
+ */
10
+
11
+ export class Result extends Enum {
12
+ constructor(registry, Ok, Err, value) {
13
+ // NOTE This is order-dependent, Ok (with index 0) needs to be first
14
+ // eslint-disable-next-line sort-keys
15
+ super(registry, {
16
+ Ok,
17
+ Err
18
+ }, value);
19
+ }
20
+
21
+ static with(Types) {
22
+ return class extends Result {
23
+ constructor(registry, value) {
24
+ super(registry, Types.Ok, Types.Err, value);
25
+ }
26
+
27
+ };
28
+ }
29
+ /**
30
+ * @description Returns the wrapper Err value (if isErr)
31
+ */
32
+
33
+
34
+ get asErr() {
35
+ assert(this.isErr, 'Cannot extract Err value from Ok result, check isErr first');
36
+ return this.value;
37
+ }
38
+ /**
39
+ * @deprecated Use asErr
40
+ */
41
+
42
+
43
+ get asError() {
44
+ return this.asErr;
45
+ }
46
+ /**
47
+ * @description Returns the wrapper Ok value (if isOk)
48
+ */
49
+
50
+
51
+ get asOk() {
52
+ assert(this.isOk, 'Cannot extract Ok value from Err result, check isOk first');
53
+ return this.value;
54
+ }
55
+ /**
56
+ * @description Checks if the Result has no value
57
+ */
58
+
59
+
60
+ get isEmpty() {
61
+ return this.isOk && this.value.isEmpty;
62
+ }
63
+ /**
64
+ * @description Checks if the Result wraps an Err value
65
+ */
66
+
67
+
68
+ get isErr() {
69
+ return !this.isOk;
70
+ }
71
+ /**
72
+ * @deprecated Use isErr
73
+ */
74
+
75
+
76
+ get isError() {
77
+ return this.isErr;
78
+ }
79
+ /**
80
+ * @description Checks if the Result wraps an Ok value
81
+ */
82
+
83
+
84
+ get isOk() {
85
+ return this.index === 0;
86
+ }
87
+ /**
88
+ * @description Returns the base runtime type name for this instance
89
+ */
90
+
91
+
92
+ toRawType() {
93
+ const Types = this._toRawStruct();
94
+
95
+ return `Result<${Types.Ok},${Types.Err}>`;
96
+ }
97
+
98
+ }
package/base/Tuple.cjs ADDED
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.Tuple = void 0;
9
+
10
+ var _classPrivateFieldLooseBase2 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldLooseBase"));
11
+
12
+ var _classPrivateFieldLooseKey2 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldLooseKey"));
13
+
14
+ var _util = require("@polkadot/util");
15
+
16
+ var _AbstractArray = require("../abstract/AbstractArray.cjs");
17
+
18
+ var _index = require("../utils/index.cjs");
19
+
20
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
21
+ // SPDX-License-Identifier: Apache-2.0
22
+
23
+ /** @internal */
24
+ function decodeTuple(registry, Classes, value) {
25
+ if ((0, _util.isU8a)(value) || (0, _util.isHex)(value)) {
26
+ return (0, _index.decodeU8a)(registry, (0, _util.u8aToU8a)(value), Classes);
27
+ }
28
+
29
+ const Types = Array.isArray(Classes) ? Classes : Object.values(Classes);
30
+ return [Types.map((Type, index) => {
31
+ try {
32
+ const entry = value === null || value === void 0 ? void 0 : value[index];
33
+
34
+ if (entry instanceof Type) {
35
+ return entry;
36
+ }
37
+
38
+ return new Type(registry, entry);
39
+ } catch (error) {
40
+ throw new Error(`Tuple: failed on ${index}:: ${error.message}`);
41
+ }
42
+ }), 0];
43
+ }
44
+ /**
45
+ * @name Tuple
46
+ * @description
47
+ * A Tuple defines an anonymous fixed-length array, where each element has its
48
+ * own type. It extends the base JS `Array` object.
49
+ */
50
+
51
+
52
+ var _Types = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("Types");
53
+
54
+ class Tuple extends _AbstractArray.AbstractArray {
55
+ constructor(registry, Types, value) {
56
+ const Classes = Array.isArray(Types) ? Types.map(t => (0, _index.typeToConstructor)(registry, t)) : (0, _util.isFunction)(Types) || (0, _util.isString)(Types) ? [(0, _index.typeToConstructor)(registry, Types)] : (0, _index.mapToTypeMap)(registry, Types);
57
+ const [values, decodedLength] = (0, _util.isU8a)(value) ? (0, _index.decodeU8a)(registry, value, Classes) : decodeTuple(registry, Classes, value);
58
+ super(registry, values, decodedLength);
59
+ Object.defineProperty(this, _Types, {
60
+ writable: true,
61
+ value: void 0
62
+ });
63
+ (0, _classPrivateFieldLooseBase2.default)(this, _Types)[_Types] = Classes;
64
+ }
65
+
66
+ static with(Types) {
67
+ return class extends Tuple {
68
+ constructor(registry, value) {
69
+ super(registry, Types, value);
70
+ }
71
+
72
+ };
73
+ }
74
+ /**
75
+ * @description The length of the value when encoded as a Uint8Array
76
+ */
77
+
78
+
79
+ get encodedLength() {
80
+ let total = 0;
81
+
82
+ for (let i = 0; i < this.length; i++) {
83
+ total += this[i].encodedLength;
84
+ }
85
+
86
+ return total;
87
+ }
88
+ /**
89
+ * @description The types definition of the tuple
90
+ */
91
+
92
+
93
+ get Types() {
94
+ return Array.isArray((0, _classPrivateFieldLooseBase2.default)(this, _Types)[_Types]) ? (0, _classPrivateFieldLooseBase2.default)(this, _Types)[_Types].map(T => new T(this.registry).toRawType()) : Object.keys((0, _classPrivateFieldLooseBase2.default)(this, _Types)[_Types]);
95
+ }
96
+ /**
97
+ * @description Returns the base runtime type name for this instance
98
+ */
99
+
100
+
101
+ toRawType() {
102
+ const types = (Array.isArray((0, _classPrivateFieldLooseBase2.default)(this, _Types)[_Types]) ? (0, _classPrivateFieldLooseBase2.default)(this, _Types)[_Types] : Object.values((0, _classPrivateFieldLooseBase2.default)(this, _Types)[_Types])).map(T => this.registry.getClassName(T) || new T(this.registry).toRawType());
103
+ return `(${types.join(',')})`;
104
+ }
105
+ /**
106
+ * @description Returns the string representation of the value
107
+ */
108
+
109
+
110
+ toString() {
111
+ // Overwrite the default toString representation of Array.
112
+ return (0, _util.stringify)(this.toJSON());
113
+ }
114
+ /**
115
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
116
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
117
+ */
118
+
119
+
120
+ toU8a(isBare) {
121
+ const encoded = new Array(this.length);
122
+
123
+ for (let i = 0; i < this.length; i++) {
124
+ encoded[i] = this[i].toU8a(isBare);
125
+ }
126
+
127
+ return (0, _util.u8aConcat)(...encoded);
128
+ }
129
+
130
+ }
131
+
132
+ exports.Tuple = Tuple;
@@ -0,0 +1,39 @@
1
+ import type { AnyTupleValue, Codec, CodecClass, CodecRegistry, ITuple } from '../types';
2
+ import { AbstractArray } from '../abstract/AbstractArray';
3
+ declare type TupleType = (CodecClass | string);
4
+ declare type TupleTypes = TupleType[] | {
5
+ [index: string]: CodecClass | string;
6
+ };
7
+ /**
8
+ * @name Tuple
9
+ * @description
10
+ * A Tuple defines an anonymous fixed-length array, where each element has its
11
+ * own type. It extends the base JS `Array` object.
12
+ */
13
+ export declare class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
14
+ #private;
15
+ constructor(registry: CodecRegistry, Types: TupleTypes | TupleType, value?: AnyTupleValue);
16
+ static with(Types: TupleTypes | TupleType): CodecClass<Tuple>;
17
+ /**
18
+ * @description The length of the value when encoded as a Uint8Array
19
+ */
20
+ get encodedLength(): number;
21
+ /**
22
+ * @description The types definition of the tuple
23
+ */
24
+ get Types(): string[];
25
+ /**
26
+ * @description Returns the base runtime type name for this instance
27
+ */
28
+ toRawType(): string;
29
+ /**
30
+ * @description Returns the string representation of the value
31
+ */
32
+ toString(): string;
33
+ /**
34
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
35
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
36
+ */
37
+ toU8a(isBare?: boolean): Uint8Array;
38
+ }
39
+ export {};