@pezkuwi/types-codec 16.5.5

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 (197) hide show
  1. package/README.md +3 -0
  2. package/build/abstract/Array.d.ts +89 -0
  3. package/build/abstract/Base.d.ts +75 -0
  4. package/build/abstract/Int.d.ts +80 -0
  5. package/build/abstract/Object.d.ts +67 -0
  6. package/build/abstract/index.d.ts +3 -0
  7. package/build/base/Compact.d.ts +88 -0
  8. package/build/base/DoNotConstruct.d.ts +63 -0
  9. package/build/base/Enum.d.ts +118 -0
  10. package/build/base/Int.d.ts +16 -0
  11. package/build/base/Null.d.ts +56 -0
  12. package/build/base/Option.d.ts +94 -0
  13. package/build/base/Result.d.ts +38 -0
  14. package/build/base/Tuple.d.ts +42 -0
  15. package/build/base/UInt.d.ts +15 -0
  16. package/build/base/Vec.d.ts +28 -0
  17. package/build/base/VecAny.d.ts +15 -0
  18. package/build/base/VecFixed.d.ts +30 -0
  19. package/build/base/index.d.ts +12 -0
  20. package/build/bundle.d.ts +7 -0
  21. package/build/extended/BTreeMap.d.ts +5 -0
  22. package/build/extended/BTreeSet.d.ts +64 -0
  23. package/build/extended/BitVec.d.ts +37 -0
  24. package/build/extended/Bytes.d.ts +29 -0
  25. package/build/extended/HashMap.d.ts +5 -0
  26. package/build/extended/Linkage.d.ts +37 -0
  27. package/build/extended/Map.d.ts +59 -0
  28. package/build/extended/OptionBool.d.ts +36 -0
  29. package/build/extended/Range.d.ts +29 -0
  30. package/build/extended/RangeInclusive.d.ts +6 -0
  31. package/build/extended/Type.d.ts +16 -0
  32. package/build/extended/U8aFixed.d.ts +16 -0
  33. package/build/extended/WrapperKeepOpaque.d.ts +40 -0
  34. package/build/extended/WrapperOpaque.d.ts +10 -0
  35. package/build/extended/index.d.ts +14 -0
  36. package/build/index.d.ts +2 -0
  37. package/build/native/Bool.d.ts +71 -0
  38. package/build/native/Date.d.ts +84 -0
  39. package/build/native/Float.d.ts +68 -0
  40. package/build/native/Json.d.ts +69 -0
  41. package/build/native/Raw.d.ts +87 -0
  42. package/build/native/Set.d.ts +84 -0
  43. package/build/native/Struct.d.ts +106 -0
  44. package/build/native/Text.d.ts +77 -0
  45. package/build/native/index.d.ts +8 -0
  46. package/build/packageDetect.d.ts +1 -0
  47. package/build/packageInfo.d.ts +6 -0
  48. package/build/primitive/F32.d.ts +11 -0
  49. package/build/primitive/F64.d.ts +11 -0
  50. package/build/primitive/I128.d.ts +11 -0
  51. package/build/primitive/I16.d.ts +11 -0
  52. package/build/primitive/I256.d.ts +11 -0
  53. package/build/primitive/I32.d.ts +11 -0
  54. package/build/primitive/I64.d.ts +11 -0
  55. package/build/primitive/I8.d.ts +11 -0
  56. package/build/primitive/ISize.d.ts +12 -0
  57. package/build/primitive/U128.d.ts +11 -0
  58. package/build/primitive/U16.d.ts +11 -0
  59. package/build/primitive/U256.d.ts +11 -0
  60. package/build/primitive/U32.d.ts +11 -0
  61. package/build/primitive/U64.d.ts +11 -0
  62. package/build/primitive/U8.d.ts +11 -0
  63. package/build/primitive/USize.d.ts +12 -0
  64. package/build/primitive/index.d.ts +16 -0
  65. package/build/types/codec.d.ts +113 -0
  66. package/build/types/helpers.d.ts +27 -0
  67. package/build/types/index.d.ts +4 -0
  68. package/build/types/interfaces.d.ts +74 -0
  69. package/build/types/registry.d.ts +67 -0
  70. package/build/utils/compareArray.d.ts +1 -0
  71. package/build/utils/compareMap.d.ts +1 -0
  72. package/build/utils/compareSet.d.ts +1 -0
  73. package/build/utils/decodeU8a.d.ts +26 -0
  74. package/build/utils/index.d.ts +8 -0
  75. package/build/utils/sanitize.d.ts +15 -0
  76. package/build/utils/sortValues.d.ts +12 -0
  77. package/build/utils/toConstructors.d.ts +16 -0
  78. package/build/utils/typesToMap.d.ts +2 -0
  79. package/build/utils/util.d.ts +3 -0
  80. package/package.json +34 -0
  81. package/src/abstract/Array.ts +213 -0
  82. package/src/abstract/Base.ts +129 -0
  83. package/src/abstract/Int.ts +271 -0
  84. package/src/abstract/Object.ts +99 -0
  85. package/src/abstract/index.ts +6 -0
  86. package/src/base/Compact.spec.ts +99 -0
  87. package/src/base/Compact.ts +198 -0
  88. package/src/base/DoNotConstruct.spec.ts +23 -0
  89. package/src/base/DoNotConstruct.ts +118 -0
  90. package/src/base/Enum.spec.ts +487 -0
  91. package/src/base/Enum.ts +460 -0
  92. package/src/base/Int.spec.ts +225 -0
  93. package/src/base/Int.ts +34 -0
  94. package/src/base/Null.spec.ts +41 -0
  95. package/src/base/Null.ts +96 -0
  96. package/src/base/Option.spec.ts +216 -0
  97. package/src/base/Option.ts +275 -0
  98. package/src/base/Result.spec.ts +64 -0
  99. package/src/base/Result.ts +79 -0
  100. package/src/base/Tuple.spec.ts +161 -0
  101. package/src/base/Tuple.ts +149 -0
  102. package/src/base/UInt.spec.ts +192 -0
  103. package/src/base/UInt.ts +30 -0
  104. package/src/base/Vec.spec.ts +224 -0
  105. package/src/base/Vec.ts +133 -0
  106. package/src/base/VecAny.ts +23 -0
  107. package/src/base/VecFixed.spec.ts +78 -0
  108. package/src/base/VecFixed.ts +92 -0
  109. package/src/base/index.ts +15 -0
  110. package/src/bundle.ts +13 -0
  111. package/src/checkTypes.manual.ts +12 -0
  112. package/src/extended/BTreeMap.spec.ts +245 -0
  113. package/src/extended/BTreeMap.ts +16 -0
  114. package/src/extended/BTreeSet.spec.ts +260 -0
  115. package/src/extended/BTreeSet.ts +233 -0
  116. package/src/extended/BitVec.spec.ts +97 -0
  117. package/src/extended/BitVec.ts +137 -0
  118. package/src/extended/Bytes.spec.ts +75 -0
  119. package/src/extended/Bytes.ts +88 -0
  120. package/src/extended/HashMap.spec.ts +36 -0
  121. package/src/extended/HashMap.ts +16 -0
  122. package/src/extended/Linkage.spec.ts +43 -0
  123. package/src/extended/Linkage.ts +81 -0
  124. package/src/extended/Map.spec.ts +123 -0
  125. package/src/extended/Map.ts +255 -0
  126. package/src/extended/OptionBool.spec.ts +49 -0
  127. package/src/extended/OptionBool.ts +93 -0
  128. package/src/extended/Range.spec.ts +37 -0
  129. package/src/extended/Range.ts +56 -0
  130. package/src/extended/RangeInclusive.ts +20 -0
  131. package/src/extended/Type.spec.ts +118 -0
  132. package/src/extended/Type.ts +29 -0
  133. package/src/extended/U8aFixed.spec.ts +117 -0
  134. package/src/extended/U8aFixed.ts +57 -0
  135. package/src/extended/WrapperKeepOpaque.spec.ts +101 -0
  136. package/src/extended/WrapperKeepOpaque.ts +128 -0
  137. package/src/extended/WrapperOpaque.spec.ts +58 -0
  138. package/src/extended/WrapperOpaque.ts +27 -0
  139. package/src/extended/index.ts +17 -0
  140. package/src/index.ts +6 -0
  141. package/src/mod.ts +4 -0
  142. package/src/native/Bool.spec.ts +74 -0
  143. package/src/native/Bool.ts +137 -0
  144. package/src/native/Date.spec.ts +85 -0
  145. package/src/native/Date.ts +169 -0
  146. package/src/native/Float.spec.ts +51 -0
  147. package/src/native/Float.ts +136 -0
  148. package/src/native/Json.ts +147 -0
  149. package/src/native/Raw.spec.ts +113 -0
  150. package/src/native/Raw.ts +171 -0
  151. package/src/native/Set.spec.ts +116 -0
  152. package/src/native/Set.ts +269 -0
  153. package/src/native/Struct.data.ts +4 -0
  154. package/src/native/Struct.spec.ts +411 -0
  155. package/src/native/Struct.ts +338 -0
  156. package/src/native/Text.spec.ts +85 -0
  157. package/src/native/Text.ts +184 -0
  158. package/src/native/index.ts +11 -0
  159. package/src/packageDetect.ts +11 -0
  160. package/src/packageInfo.ts +6 -0
  161. package/src/primitive/F32.ts +14 -0
  162. package/src/primitive/F64.ts +14 -0
  163. package/src/primitive/I128.ts +14 -0
  164. package/src/primitive/I16.ts +14 -0
  165. package/src/primitive/I256.ts +14 -0
  166. package/src/primitive/I32.ts +14 -0
  167. package/src/primitive/I64.ts +14 -0
  168. package/src/primitive/I8.ts +14 -0
  169. package/src/primitive/ISize.ts +21 -0
  170. package/src/primitive/U128.ts +14 -0
  171. package/src/primitive/U16.ts +14 -0
  172. package/src/primitive/U256.ts +14 -0
  173. package/src/primitive/U32.ts +14 -0
  174. package/src/primitive/U64.ts +14 -0
  175. package/src/primitive/U8.ts +14 -0
  176. package/src/primitive/USize.ts +21 -0
  177. package/src/primitive/index.ts +19 -0
  178. package/src/test/performance.ts +61 -0
  179. package/src/types/codec.ts +140 -0
  180. package/src/types/helpers.ts +50 -0
  181. package/src/types/index.ts +7 -0
  182. package/src/types/interfaces.ts +98 -0
  183. package/src/types/registry.ts +86 -0
  184. package/src/utils/compareArray.ts +22 -0
  185. package/src/utils/compareMap.ts +40 -0
  186. package/src/utils/compareSet.ts +23 -0
  187. package/src/utils/decodeU8a.ts +123 -0
  188. package/src/utils/index.ts +11 -0
  189. package/src/utils/sanitize.spec.ts +89 -0
  190. package/src/utils/sanitize.ts +290 -0
  191. package/src/utils/sortValues.ts +103 -0
  192. package/src/utils/toConstructors.ts +46 -0
  193. package/src/utils/typesToMap.ts +14 -0
  194. package/src/utils/util.ts +8 -0
  195. package/tsconfig.build.json +16 -0
  196. package/tsconfig.build.tsbuildinfo +1 -0
  197. package/tsconfig.spec.json +21 -0
@@ -0,0 +1,460 @@
1
+ // Copyright 2017-2025 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { HexString } from '@pezkuwi/util/types';
5
+ import type { AnyJson, Codec, CodecClass, DefinitionSetter, IEnum, Inspect, IU8a, Registry } from '../types/index.js';
6
+
7
+ import { identity, isHex, isNumber, isObject, isString, isU8a, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcatStrict, u8aToHex, u8aToU8a } from '@pezkuwi/util';
8
+
9
+ import { mapToTypeMap, typesToMap } from '../utils/index.js';
10
+ import { Null } from './Null.js';
11
+
12
+ // export interface, this is used in Enum.with, so required as public by TS
13
+ export type EnumCodecClass<T = Codec> = new(registry: Registry, value?: any, index?: number) => T;
14
+
15
+ interface Definition {
16
+ def: TypesDef;
17
+ isBasic: boolean;
18
+ isIndexed: boolean;
19
+ }
20
+
21
+ interface EntryDef {
22
+ Type: CodecClass;
23
+ index: number;
24
+ }
25
+
26
+ type TypesDef = Record<string, EntryDef>;
27
+
28
+ interface Decoded {
29
+ index: number;
30
+ value: Codec;
31
+ }
32
+
33
+ function isRustEnum (def: Record<string, string | CodecClass> | Record<string, number>): def is Record<string, string | CodecClass> {
34
+ const defValues = Object.values(def);
35
+
36
+ if (defValues.some((v) => isNumber(v))) {
37
+ if (!defValues.every((v) => isNumber(v) && v >= 0 && v <= 255)) {
38
+ throw new Error('Invalid number-indexed enum definition');
39
+ }
40
+
41
+ return false;
42
+ }
43
+
44
+ return true;
45
+ }
46
+
47
+ function extractDef (registry: Registry, _def: Record<string, string | CodecClass> | Record<string, number> | string[]): Definition {
48
+ const def: TypesDef = {};
49
+ let isBasic: boolean;
50
+ let isIndexed: boolean;
51
+
52
+ if (Array.isArray(_def)) {
53
+ for (let i = 0, count = _def.length; i < count; i++) {
54
+ def[_def[i]] = { Type: Null, index: i };
55
+ }
56
+
57
+ isBasic = true;
58
+ isIndexed = false;
59
+ } else if (isRustEnum(_def)) {
60
+ const [Types, keys] = mapToTypeMap(registry, _def);
61
+
62
+ for (let i = 0, count = keys.length; i < count; i++) {
63
+ def[keys[i]] = { Type: Types[i], index: i };
64
+ }
65
+
66
+ isBasic = !Object.values(def).some(({ Type }) => Type !== Null);
67
+ isIndexed = false;
68
+ } else {
69
+ const entries = Object.entries(_def);
70
+
71
+ for (let i = 0, count = entries.length; i < count; i++) {
72
+ const [key, index] = entries[i];
73
+
74
+ def[key] = { Type: Null, index };
75
+ }
76
+
77
+ isBasic = true;
78
+ isIndexed = true;
79
+ }
80
+
81
+ return {
82
+ def,
83
+ isBasic,
84
+ isIndexed
85
+ };
86
+ }
87
+
88
+ function getEntryType (def: TypesDef, checkIdx: number): CodecClass {
89
+ const values = Object.values(def);
90
+
91
+ for (let i = 0, count = values.length; i < count; i++) {
92
+ const { Type, index } = values[i];
93
+
94
+ if (index === checkIdx) {
95
+ return Type;
96
+ }
97
+ }
98
+
99
+ throw new Error(`Unable to create Enum via index ${checkIdx}, in ${Object.keys(def).join(', ')}`);
100
+ }
101
+
102
+ function createFromU8a (registry: Registry, def: TypesDef, index: number, value: Uint8Array): Decoded {
103
+ const Type = getEntryType(def, index);
104
+
105
+ return {
106
+ index,
107
+ value: new Type(registry, value)
108
+ };
109
+ }
110
+
111
+ function createFromValue (registry: Registry, def: TypesDef, index = 0, value?: unknown): Decoded {
112
+ const Type = getEntryType(def, index);
113
+
114
+ return {
115
+ index,
116
+ value: value instanceof Type
117
+ ? value
118
+ : new Type(registry, value)
119
+ };
120
+ }
121
+
122
+ function decodeFromJSON (registry: Registry, def: TypesDef, key: string, value?: unknown): Decoded {
123
+ // JSON comes in the form of { "<type (camelCase)>": "<value for type>" }, here we
124
+ // additionally force to lower to ensure forward compat
125
+ const keys = Object.keys(def).map((k) => k.toLowerCase());
126
+ const keyLower = key.toLowerCase();
127
+ const index = keys.indexOf(keyLower);
128
+
129
+ if (index === -1) {
130
+ throw new Error(`Cannot map Enum JSON, unable to find '${key}' in ${keys.join(', ')}`);
131
+ }
132
+
133
+ try {
134
+ return createFromValue(registry, def, Object.values(def)[index].index, value);
135
+ } catch (error) {
136
+ throw new Error(`Enum(${key}):: ${(error as Error).message}`);
137
+ }
138
+ }
139
+
140
+ function decodeEnum (registry: Registry, def: TypesDef, value?: unknown, index?: number): Decoded {
141
+ // NOTE We check the index path first, before looking at values - this allows treating
142
+ // the optional indexes before anything else, more-specific > less-specific
143
+ if (isNumber(index)) {
144
+ return createFromValue(registry, def, index, value);
145
+ } else if (isU8a(value) || isHex(value)) {
146
+ const u8a = u8aToU8a(value);
147
+
148
+ // nested, we don't want to match isObject below
149
+ if (u8a.length) {
150
+ return createFromU8a(registry, def, u8a[0], u8a.subarray(1));
151
+ }
152
+ } else if (value instanceof Enum) {
153
+ return createFromValue(registry, def, value.index, value.value);
154
+ } else if (isNumber(value)) {
155
+ return createFromValue(registry, def, value);
156
+ } else if (isString(value)) {
157
+ return decodeFromJSON(registry, def, value.toString());
158
+ } else if (isObject(value)) {
159
+ const key = Object.keys(value)[0];
160
+
161
+ return decodeFromJSON(registry, def, key, value[key]);
162
+ }
163
+
164
+ // Worst-case scenario, return the first with default
165
+ return createFromValue(registry, def, Object.values(def)[0].index);
166
+ }
167
+
168
+ /**
169
+ * @name Enum
170
+ * @description
171
+ * This implements an enum, that based on the value wraps a different type. It is effectively
172
+ * an extension to enum where the value type is determined by the actual index.
173
+ */
174
+ export class Enum implements IEnum {
175
+ readonly registry: Registry;
176
+
177
+ public createdAtHash?: IU8a;
178
+ public initialU8aLength?: number;
179
+ public isStorageFallback?: boolean;
180
+
181
+ readonly #def: TypesDef;
182
+ readonly #entryIndex: number;
183
+ readonly #indexes: number[];
184
+ readonly #isBasic: boolean;
185
+ readonly #isIndexed: boolean;
186
+ readonly #raw: Codec;
187
+
188
+ constructor (registry: Registry, Types: Record<string, string | CodecClass> | Record<string, number> | string[], value?: unknown, index?: number, { definition, setDefinition = identity }: DefinitionSetter<Definition> = {}) {
189
+ const { def, isBasic, isIndexed } = definition || setDefinition(extractDef(registry, Types));
190
+
191
+ // shortcut isU8a as used in SCALE decoding
192
+ const decoded = isU8a(value) && value.length && !isNumber(index)
193
+ ? createFromU8a(registry, def, value[0], value.subarray(1))
194
+ : decodeEnum(registry, def, value, index);
195
+
196
+ this.registry = registry;
197
+ this.#def = def;
198
+ this.#isBasic = isBasic;
199
+ this.#isIndexed = isIndexed;
200
+ this.#indexes = Object.values(def).map(({ index }) => index);
201
+ this.#entryIndex = this.#indexes.indexOf(decoded.index);
202
+ this.#raw = decoded.value;
203
+
204
+ if (this.#raw.initialU8aLength) {
205
+ this.initialU8aLength = 1 + this.#raw.initialU8aLength;
206
+ }
207
+ }
208
+
209
+ public static with (Types: Record<string, string | CodecClass> | Record<string, number> | string[]): EnumCodecClass<Enum> {
210
+ let definition: Definition | undefined;
211
+
212
+ // eslint-disable-next-line no-return-assign
213
+ const setDefinition = (d: Definition) =>
214
+ definition = d;
215
+
216
+ return class extends Enum {
217
+ static {
218
+ const keys = Array.isArray(Types)
219
+ ? Types
220
+ : Object.keys(Types);
221
+ const count = keys.length;
222
+ const asKeys = new Array<string>(count);
223
+ const isKeys = new Array<string>(count);
224
+
225
+ for (let i = 0; i < count; i++) {
226
+ const name = stringPascalCase(keys[i]);
227
+
228
+ asKeys[i] = `as${name}`;
229
+ isKeys[i] = `is${name}`;
230
+ }
231
+
232
+ objectProperties(this.prototype, isKeys, (_: string, i: number, self: Enum) =>
233
+ self.type === keys[i]
234
+ );
235
+
236
+ objectProperties(this.prototype, asKeys, (k: string, i: number, self: Enum): Codec => {
237
+ if (self.type !== keys[i]) {
238
+ throw new Error(`Cannot convert '${self.type}' via ${k}`);
239
+ }
240
+
241
+ return self.value;
242
+ });
243
+ }
244
+
245
+ constructor (registry: Registry, value?: unknown, index?: number) {
246
+ super(registry, Types, value, index, { definition, setDefinition });
247
+ }
248
+ };
249
+ }
250
+
251
+ /**
252
+ * @description The length of the value when encoded as a Uint8Array
253
+ */
254
+ public get encodedLength (): number {
255
+ return 1 + this.#raw.encodedLength;
256
+ }
257
+
258
+ /**
259
+ * @description returns a hash of the contents
260
+ */
261
+ public get hash (): IU8a {
262
+ return this.registry.hash(this.toU8a());
263
+ }
264
+
265
+ /**
266
+ * @description The index of the enum value
267
+ */
268
+ public get index (): number {
269
+ return this.#indexes[this.#entryIndex];
270
+ }
271
+
272
+ /**
273
+ * @description The value of the enum
274
+ */
275
+ public get inner (): Codec {
276
+ return this.#raw;
277
+ }
278
+
279
+ /**
280
+ * @description true if this is a basic enum (no values)
281
+ */
282
+ public get isBasic (): boolean {
283
+ return this.#isBasic;
284
+ }
285
+
286
+ /**
287
+ * @description Checks if the value is an empty value
288
+ */
289
+ public get isEmpty (): boolean {
290
+ return this.#raw.isEmpty;
291
+ }
292
+
293
+ /**
294
+ * @description Checks if the Enum points to a [[Null]] type
295
+ */
296
+ public get isNone (): boolean {
297
+ return this.#raw instanceof Null;
298
+ }
299
+
300
+ /**
301
+ * @description The available keys for this enum
302
+ */
303
+ public get defIndexes (): number[] {
304
+ return this.#indexes;
305
+ }
306
+
307
+ /**
308
+ * @description The available keys for this enum
309
+ */
310
+ public get defKeys (): string[] {
311
+ return Object.keys(this.#def);
312
+ }
313
+
314
+ /**
315
+ * @description The name of the type this enum value represents
316
+ */
317
+ public get type (): string {
318
+ return this.defKeys[this.#entryIndex];
319
+ }
320
+
321
+ /**
322
+ * @description The value of the enum
323
+ */
324
+ public get value (): Codec {
325
+ return this.#raw;
326
+ }
327
+
328
+ /**
329
+ * @description Compares the value of the input to see if there is a match
330
+ */
331
+ public eq (other?: unknown): boolean {
332
+ // cater for the case where we only pass the enum index
333
+ if (isU8a(other)) {
334
+ return !this.toU8a().some((entry, index) => entry !== other[index]);
335
+ } else if (isNumber(other)) {
336
+ return this.toNumber() === other;
337
+ } else if (this.#isBasic && isString(other)) {
338
+ return this.type === other;
339
+ } else if (isHex(other)) {
340
+ return this.toHex() === other;
341
+ } else if (other instanceof Enum) {
342
+ return this.index === other.index && this.value.eq(other.value);
343
+ } else if (isObject(other)) {
344
+ return this.value.eq(other[this.type]);
345
+ }
346
+
347
+ // compare the actual wrapper value
348
+ return this.value.eq(other);
349
+ }
350
+
351
+ /**
352
+ * @description Returns a breakdown of the hex encoding for this Codec
353
+ */
354
+ public inspect (): Inspect {
355
+ if (this.#isBasic) {
356
+ return { outer: [new Uint8Array([this.index])] };
357
+ }
358
+
359
+ const { inner, outer = [] } = this.#raw.inspect();
360
+
361
+ return {
362
+ inner,
363
+ outer: [new Uint8Array([this.index]), ...outer]
364
+ };
365
+ }
366
+
367
+ /**
368
+ * @description Returns a hex string representation of the value
369
+ */
370
+ public toHex (): HexString {
371
+ return u8aToHex(this.toU8a());
372
+ }
373
+
374
+ /**
375
+ * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
376
+ */
377
+ public toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson {
378
+ return this.#isBasic || this.isNone
379
+ ? this.type
380
+ : { [this.type]: this.#raw.toHuman(isExtended, disableAscii) };
381
+ }
382
+
383
+ /**
384
+ * @description Converts the Object to JSON, typically used for RPC transfers
385
+ */
386
+ public toJSON (): AnyJson {
387
+ return this.#isBasic
388
+ ? this.type
389
+ : { [stringCamelCase(this.type)]: this.#raw.toJSON() };
390
+ }
391
+
392
+ /**
393
+ * @description Returns the number representation for the value
394
+ */
395
+ public toNumber (): number {
396
+ return this.index;
397
+ }
398
+
399
+ /**
400
+ * @description Converts the value in a best-fit primitive form
401
+ */
402
+ public toPrimitive (disableAscii?: boolean): AnyJson {
403
+ return this.#isBasic
404
+ ? this.type
405
+ : { [stringCamelCase(this.type)]: this.#raw.toPrimitive(disableAscii) };
406
+ }
407
+
408
+ /**
409
+ * @description Returns a raw struct representation of the enum types
410
+ */
411
+ protected _toRawStruct (): string[] | Record<string, string | number> {
412
+ if (this.#isBasic) {
413
+ return this.#isIndexed
414
+ ? this.defKeys.reduce((out: Record<string, number>, key, index): Record<string, number> => {
415
+ out[key] = this.#indexes[index];
416
+
417
+ return out;
418
+ }, {})
419
+ : this.defKeys;
420
+ }
421
+
422
+ const entries = Object.entries(this.#def);
423
+
424
+ return typesToMap(this.registry, entries.reduce<[CodecClass[], string[]]>((out, [key, { Type }], i) => {
425
+ out[0][i] = Type;
426
+ out[1][i] = key;
427
+
428
+ return out;
429
+ }, [new Array<CodecClass>(entries.length), new Array<string>(entries.length)]));
430
+ }
431
+
432
+ /**
433
+ * @description Returns the base runtime type name for this instance
434
+ */
435
+ public toRawType (): string {
436
+ return stringify({ _enum: this._toRawStruct() });
437
+ }
438
+
439
+ /**
440
+ * @description Returns the string representation of the value
441
+ */
442
+ public toString (): string {
443
+ return this.isNone
444
+ ? this.type
445
+ : stringify(this.toJSON());
446
+ }
447
+
448
+ /**
449
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
450
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
451
+ */
452
+ public toU8a (isBare?: boolean): Uint8Array {
453
+ return isBare
454
+ ? this.#raw.toU8a(isBare)
455
+ : u8aConcatStrict([
456
+ new Uint8Array([this.index]),
457
+ this.#raw.toU8a(isBare)
458
+ ]);
459
+ }
460
+ }
@@ -0,0 +1,225 @@
1
+ // Copyright 2017-2025 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /// <reference types="@pezkuwi/dev-test/globals.d.ts" />
5
+
6
+ import type { UIntBitLength } from '../types/index.js';
7
+
8
+ import { TypeRegistry } from '@pezkuwi/types';
9
+ import { Int } from '@pezkuwi/types-codec';
10
+ import { BN } from '@pezkuwi/util';
11
+
12
+ const TESTS: [bitLength: UIntBitLength, value: string | number | Uint8Array, expected?: string][] = [
13
+ [8, 0],
14
+ [8, 127],
15
+ [8, -128],
16
+ [8, new Uint8Array([0])],
17
+ [8, new Uint8Array([127])],
18
+ [8, new Uint8Array([128]), '-128'],
19
+ [32, 0],
20
+ [32, 2147483647],
21
+ [32, -2147483648]
22
+ ];
23
+
24
+ describe('Int', (): void => {
25
+ const registry = new TypeRegistry();
26
+
27
+ it('can construct via a single-entry struct', (): void => {
28
+ expect(
29
+ // @ts-expect-error We could receive these via JSON
30
+ new Int(registry, { ref_time: 1234 }).toNumber()
31
+ ).toEqual(1234);
32
+ expect(
33
+ // @ts-expect-error We could receive these via JSON
34
+ () => new Int(registry, { ref_time: 1234, zoo: 4567 }).toNumber()
35
+ ).toThrow(/Unable to construct number from/);
36
+ });
37
+
38
+ it('converts to Little Endian from the provided value', (): void => {
39
+ expect(
40
+ new Int(registry, -1234).toU8a()
41
+ ).toEqual(new Uint8Array([46, 251, 255, 255, 255, 255, 255, 255]));
42
+ });
43
+
44
+ it('decodes edge case to bytes correctly', (): void => {
45
+ // Zero
46
+ expect(
47
+ new Int(registry, 0, 8).toU8a()
48
+ ).toEqual(new Uint8Array([0]));
49
+
50
+ expect(
51
+ new Int(registry, 0, 16).toU8a()
52
+ ).toEqual(new Uint8Array([0, 0]));
53
+
54
+ expect(
55
+ new Int(registry, 0, 32).toU8a()
56
+ ).toEqual(new Uint8Array([0, 0, 0, 0]));
57
+
58
+ expect(
59
+ new Int(registry, 0, 64).toU8a()
60
+ ).toEqual(new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0]));
61
+
62
+ expect(
63
+ new Int(registry, 0, 128).toU8a()
64
+ ).toEqual(new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
65
+
66
+ // One
67
+ expect(
68
+ new Int(registry, 0, 8).toU8a()
69
+ ).toEqual(new Uint8Array([0]));
70
+
71
+ expect(
72
+ new Int(registry, 1, 16).toU8a()
73
+ ).toEqual(new Uint8Array([1, 0]));
74
+
75
+ expect(
76
+ new Int(registry, 1, 32).toU8a()
77
+ ).toEqual(new Uint8Array([1, 0, 0, 0]));
78
+
79
+ expect(
80
+ new Int(registry, 1, 64).toU8a()
81
+ ).toEqual(new Uint8Array([1, 0, 0, 0, 0, 0, 0, 0]));
82
+
83
+ expect(
84
+ new Int(registry, 1, 128).toU8a()
85
+ ).toEqual(new Uint8Array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
86
+
87
+ // MIN
88
+ expect(
89
+ new Int(registry, -128, 8).toU8a()
90
+ ).toEqual(new Uint8Array([128]));
91
+
92
+ expect(
93
+ new Int(registry, -32768, 16).toU8a()
94
+ ).toEqual(new Uint8Array([0, 128]));
95
+
96
+ // MAX
97
+ expect(
98
+ new Int(registry, 127, 8).toU8a()
99
+ ).toEqual(new Uint8Array([127]));
100
+
101
+ expect(
102
+ new Int(registry, 32767, 16).toU8a()
103
+ ).toEqual(new Uint8Array([255, 127]));
104
+ });
105
+
106
+ it('decodes edge case to js number', (): void => {
107
+ // Zero
108
+ expect(
109
+ new Int(registry, new Uint8Array([0]), 8).toNumber()
110
+ ).toEqual(0);
111
+
112
+ expect(
113
+ new Int(registry, new Uint8Array([0, 0]), 16).toNumber()
114
+ ).toEqual(0);
115
+
116
+ // One
117
+ expect(
118
+ new Int(registry, new Uint8Array([1]), 8).toNumber()
119
+ ).toEqual(1);
120
+
121
+ expect(
122
+ new Int(registry, new Uint8Array([1, 0]), 16).toNumber()
123
+ ).toEqual(1);
124
+
125
+ // MIN
126
+ expect(
127
+ new Int(registry, new Uint8Array([128]), 8).toNumber()
128
+ ).toEqual(-128);
129
+
130
+ expect(
131
+ new Int(registry, new Uint8Array([128, 255]), 16).toNumber()
132
+ ).toEqual(-128);
133
+
134
+ // MAX
135
+ expect(
136
+ new Int(registry, new Uint8Array([127]), 8).toNumber()
137
+ ).toEqual(127);
138
+
139
+ expect(
140
+ new Int(registry, new Uint8Array([255, 127]), 16).toNumber()
141
+ ).toEqual(32767);
142
+ });
143
+
144
+ it('converts to Little Endian from the provided value (bitLength)', (): void => {
145
+ expect(
146
+ new Int(registry, -1234, 32).toU8a()
147
+ ).toEqual(new Uint8Array([46, 251, 255, 255]));
148
+ });
149
+
150
+ it('converts to equivalents', (): void => {
151
+ const a = new Int(registry, '-123');
152
+
153
+ expect(
154
+ new Int(registry, a).toNumber()
155
+ ).toEqual(-123);
156
+ });
157
+
158
+ it('allows null/undefined', (): void => {
159
+ expect(
160
+ new Int(registry).toNumber()
161
+ ).toEqual(0);
162
+ expect(
163
+ new Int(registry, null).toNumber()
164
+ ).toEqual(0);
165
+ });
166
+
167
+ describe('utilities', (): void => {
168
+ it('provides a toBigInt interface', (): void => {
169
+ expect(
170
+ new Int(registry, -1234).toBigInt()
171
+ ).toEqual(-1234n);
172
+ });
173
+
174
+ it('provides a toBn interface', (): void => {
175
+ expect(
176
+ new Int(registry, -1234).toBn().toNumber()
177
+ ).toEqual(-1234);
178
+ });
179
+
180
+ it('provides a toNumber interface', (): void => {
181
+ expect(
182
+ new Int(registry, -1234).toNumber()
183
+ ).toEqual(-1234);
184
+ });
185
+
186
+ it('has a sane inspect', (): void => {
187
+ expect(
188
+ new Int(registry, '0x12', 16).inspect()
189
+ ).toEqual({
190
+ outer: [new Uint8Array([0x12, 0x00])]
191
+ });
192
+ });
193
+
194
+ it('converts to hex/string', (): void => {
195
+ const i = new Int(registry, '0x12', 16);
196
+
197
+ expect(i.toHex()).toEqual('0x0012');
198
+ expect(i.toString()).toEqual('18');
199
+ });
200
+ });
201
+
202
+ describe('static with', (): void => {
203
+ it('allows default toRawType', (): void => {
204
+ expect(
205
+ new (Int.with(64))(registry).toRawType()
206
+ ).toEqual('i64');
207
+ });
208
+
209
+ it('allows toRawType override', (): void => {
210
+ expect(
211
+ new (Int.with(64, 'SomethingElse'))(registry).toRawType()
212
+ ).toEqual('SomethingElse');
213
+ });
214
+ });
215
+
216
+ describe('conversion tests', (): void => {
217
+ TESTS.forEach(([bitLength, input, expected], i): void => {
218
+ it(`#${i}: converts ${input as string}`, (): void => {
219
+ expect(
220
+ new Int(registry, Array.isArray(input) ? new Uint8Array(input) : input, bitLength).toString()
221
+ ).toEqual(expected || new BN(input).toString());
222
+ });
223
+ });
224
+ });
225
+ });
@@ -0,0 +1,34 @@
1
+ // Copyright 2017-2025 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { AnyNumber, CodecClass, Registry, UIntBitLength } from '../types/index.js';
5
+
6
+ import { AbstractInt } from '../abstract/Int.js';
7
+
8
+ /**
9
+ * @name Int
10
+ * @description
11
+ * A generic signed integer codec. For Substrate all numbers are Little Endian encoded,
12
+ * this handles the encoding and decoding of those numbers. Upon construction
13
+ * the bitLength is provided and any additional use keeps the number to this
14
+ * length. This extends `BN`, so all methods available on a normal `BN` object
15
+ * is available here.
16
+ * @noInheritDoc
17
+ */
18
+ export class Int extends AbstractInt {
19
+ constructor (registry: Registry, value: AnyNumber | null = 0, bitLength?: UIntBitLength) {
20
+ super(registry, value, bitLength, true);
21
+ }
22
+
23
+ public static with (bitLength: UIntBitLength, typeName?: string): CodecClass<Int> {
24
+ return class extends Int {
25
+ constructor (registry: Registry, value?: AnyNumber | null) {
26
+ super(registry, value, bitLength);
27
+ }
28
+
29
+ public override toRawType (): string {
30
+ return typeName || super.toRawType();
31
+ }
32
+ };
33
+ }
34
+ }