@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,22 @@
1
+ // Copyright 2017-2025 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { isUndefined } from '@pezkuwi/util';
5
+
6
+ import { hasEq } from './util.js';
7
+
8
+ // NOTE These are used internally and when comparing objects, we expect that
9
+ // when the second is an Codec[] that the first has to be as well
10
+ export function compareArray (a: unknown[], b?: unknown): boolean {
11
+ if (Array.isArray(b)) {
12
+ return (a.length === b.length) && isUndefined(
13
+ a.find((v, index): boolean =>
14
+ hasEq(v)
15
+ ? !v.eq(b[index])
16
+ : v !== b[index]
17
+ )
18
+ );
19
+ }
20
+
21
+ return false;
22
+ }
@@ -0,0 +1,40 @@
1
+ // Copyright 2017-2025 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { isObject, isUndefined } from '@pezkuwi/util';
5
+
6
+ import { hasEq } from './util.js';
7
+
8
+ function hasMismatch (a?: unknown, b?: unknown): boolean {
9
+ return isUndefined(a) || (
10
+ hasEq(a)
11
+ ? !a.eq(b)
12
+ : a !== b
13
+ );
14
+ }
15
+
16
+ function notEntry (value: unknown): boolean {
17
+ return !Array.isArray(value) || value.length !== 2;
18
+ }
19
+
20
+ function compareMapArray (a: Map<unknown, unknown>, b: [unknown, unknown][]): boolean {
21
+ // equal number of entries and each entry in the array should match
22
+ return (a.size === b.length) && !b.some((e) =>
23
+ notEntry(e) ||
24
+ hasMismatch(a.get(e[0]), e[1])
25
+ );
26
+ }
27
+
28
+ // NOTE These are used internally and when comparing objects, we expect that
29
+ // when the second is an Map<string, Codec> that the first has to be as well
30
+ export function compareMap (a: Map<unknown, unknown>, b?: unknown): boolean {
31
+ if (Array.isArray(b)) {
32
+ return compareMapArray(a, b as [unknown, unknown][]);
33
+ } else if (b instanceof Map) {
34
+ return compareMapArray(a, [...b.entries()]);
35
+ } else if (isObject(b)) {
36
+ return compareMapArray(a, Object.entries(b));
37
+ }
38
+
39
+ return false;
40
+ }
@@ -0,0 +1,23 @@
1
+ // Copyright 2017-2025 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { isObject } from '@pezkuwi/util';
5
+
6
+ function compareSetArray (a: Set<unknown>, b: any[]): boolean {
7
+ // equal number of entries and each entry in the array should match
8
+ return (a.size === b.length) && !b.some((e) => !a.has(e));
9
+ }
10
+
11
+ // NOTE These are used internally and when comparing objects, we expect that
12
+ // when the second is an Set<string, Codec> that the first has to be as well
13
+ export function compareSet (a: Set<unknown>, b?: unknown): boolean {
14
+ if (Array.isArray(b)) {
15
+ return compareSetArray(a, b);
16
+ } else if (b instanceof Set) {
17
+ return compareSetArray(a, [...b.values()]);
18
+ } else if (isObject(b)) {
19
+ return compareSetArray(a, Object.values(b));
20
+ }
21
+
22
+ return false;
23
+ }
@@ -0,0 +1,123 @@
1
+ // Copyright 2017-2025 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Codec, CodecClass, Registry } from '../types/index.js';
5
+
6
+ import { u8aToHex } from '@pezkuwi/util';
7
+
8
+ const MAX_DEPTH = 1024;
9
+
10
+ /** @internal */
11
+ function isComplexType (Type: CodecClass): boolean {
12
+ const typeName = Type.name?.toLowerCase() || '';
13
+
14
+ return ['enum', 'hashmap', 'linkage', 'null', 'option', 'range', 'rangeinclusive', 'result', 'struct', 'tuple', 'vec', 'vecfixed'].includes(typeName);
15
+ }
16
+
17
+ /** @internal */
18
+ function formatFailure (registry: Registry, fn: 'decodeU8a' | 'decodeU8aStruct' | 'decodeU8aVec', _result: unknown[], { message }: Error, u8a: Uint8Array, i: number, count: number, Type: CodecClass, key?: string): string {
19
+ let type = '';
20
+
21
+ try {
22
+ type = `: ${new Type(registry).toRawType()}`;
23
+ } catch {
24
+ // ignore
25
+ }
26
+
27
+ // This is extra debugging info (we most-probably want this in in some way, shape or form,
28
+ // but at this point not quite sure how to include and format it (it can be quite massive)
29
+ // console.error(JSON.stringify(result, null, 2));
30
+
31
+ return `${fn}: failed at ${u8aToHex(u8a.subarray(0, 16))}…${key ? ` on ${key}` : ''} (index ${i + 1}/${count})${type}:: ${message}`;
32
+ }
33
+
34
+ /**
35
+ * @internal
36
+ *
37
+ * Given an u8a, and an array of Type constructors, decode the u8a against the
38
+ * types, and return an array of decoded values.
39
+ *
40
+ * @param u8a - The u8a to decode.
41
+ * @param result - The result array (will be returned with values pushed)
42
+ * @param types - The array of CodecClass to decode the U8a against.
43
+ */
44
+ export function decodeU8a <T extends Codec = Codec> (registry: Registry, result: T[], u8a: Uint8Array, [Types, keys]: [CodecClass<T>[], string[]]): [T[], number] {
45
+ const count = result.length;
46
+ let offset = 0;
47
+ let i = 0;
48
+
49
+ try {
50
+ while (i < count) {
51
+ const value = new Types[i](registry, u8a.subarray(offset));
52
+
53
+ offset += value.initialU8aLength || value.encodedLength;
54
+ result[i] = value;
55
+ i++;
56
+ }
57
+ } catch (error) {
58
+ throw new Error(formatFailure(registry, 'decodeU8a', result, error as Error, u8a.subarray(offset), i, count, Types[i], keys[i]));
59
+ }
60
+
61
+ return [result, offset];
62
+ }
63
+
64
+ /**
65
+ * @internal
66
+ *
67
+ * Split from decodeU8a since this is specialized to zip returns ... while we duplicate, this
68
+ * is all on the hot-path, so it is not great, however there is (some) method behind the madness
69
+ */
70
+ export function decodeU8aStruct (registry: Registry, result: [string, Codec][], u8a: Uint8Array, [Types, keys]: [CodecClass[], string[]]): [[string, Codec][], number] {
71
+ const count = result.length;
72
+ let offset = 0;
73
+ let i = 0;
74
+
75
+ if (count > MAX_DEPTH && isComplexType(Types[i])) {
76
+ throw new Error(`decodeU8aStruct: Maximum depth exceeded, received ${count} elements, limit ${MAX_DEPTH}`);
77
+ }
78
+
79
+ try {
80
+ while (i < count) {
81
+ const value = new Types[i](registry, u8a.subarray(offset));
82
+
83
+ offset += value.initialU8aLength || value.encodedLength;
84
+ result[i] = [keys[i], value];
85
+ i++;
86
+ }
87
+ } catch (error) {
88
+ throw new Error(formatFailure(registry, 'decodeU8aStruct', result, error as Error, u8a.subarray(offset), i, count, Types[i], keys[i]));
89
+ }
90
+
91
+ return [result, offset];
92
+ }
93
+
94
+ /**
95
+ * @internal
96
+ *
97
+ * Split from decodeU8a since this is specialized to 1 instance ... while we duplicate, this
98
+ * is all on the hot-path, so it is not great, however there is (some) method behind the madness
99
+ */
100
+ export function decodeU8aVec <T extends Codec = Codec> (registry: Registry, result: unknown[], u8a: Uint8Array, startAt: number, Type: CodecClass<T>): [number, number] {
101
+ const count = result.length;
102
+
103
+ if (count > MAX_DEPTH && isComplexType(Type)) {
104
+ throw new Error(`decodeU8aVec: Maximum depth exceeded, received ${count} elements, limit ${MAX_DEPTH}`);
105
+ }
106
+
107
+ let offset = startAt;
108
+ let i = 0;
109
+
110
+ try {
111
+ while (i < count) {
112
+ const value = new Type(registry, u8a.subarray(offset));
113
+
114
+ offset += value.initialU8aLength || value.encodedLength;
115
+ result[i] = value;
116
+ i++;
117
+ }
118
+ } catch (error) {
119
+ throw new Error(formatFailure(registry, 'decodeU8aVec', result, error as Error, u8a.subarray(offset), i, count, Type));
120
+ }
121
+
122
+ return [offset, offset - startAt];
123
+ }
@@ -0,0 +1,11 @@
1
+ // Copyright 2017-2025 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { compareArray } from './compareArray.js';
5
+ export { compareMap } from './compareMap.js';
6
+ export { compareSet } from './compareSet.js';
7
+ export { decodeU8a, decodeU8aStruct, decodeU8aVec } from './decodeU8a.js';
8
+ export { sanitize } from './sanitize.js';
9
+ export { sortAsc, sortMap, sortSet } from './sortValues.js';
10
+ export { mapToTypeMap, typesToConstructors, typeToConstructor } from './toConstructors.js';
11
+ export { typesToMap } from './typesToMap.js';
@@ -0,0 +1,89 @@
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 { alias, flattenSingleTuple, removeColons, removeExtensions } from './sanitize.js';
7
+
8
+ describe('sanitize', (): void => {
9
+ describe('alias', (): void => {
10
+ const fn = alias('String', 'Text');
11
+
12
+ it('replaces all occurrences for types', (): void => {
13
+ expect(fn('(String,Address,MasterString,String)')).toEqual(
14
+ '(Text,Address,MasterString,Text)'
15
+ );
16
+ });
17
+
18
+ it('replaces actual types, but leaves struct names', (): void => {
19
+ expect(fn('{"system":"String","versionString":"String"}')).toEqual(
20
+ '{"system":"Text","versionString":"Text"}'
21
+ );
22
+ });
23
+
24
+ it('handles the preceding correctly', (): void => {
25
+ // NOTE This type doesn't make sense
26
+ expect(fn('String String (String,[String;32],String)"String<String>')).toEqual(
27
+ 'Text Text (Text,[Text;32],Text)"Text<Text>'
28
+ );
29
+ });
30
+
31
+ it('handles embedded Vec/Tuples', (): void => {
32
+ const ann = alias('Announcement', 'ProxyAnnouncement');
33
+
34
+ expect(ann('(Vec<Announcement>,BalanceOf)')).toEqual(
35
+ '(Vec<ProxyAnnouncement>,BalanceOf)'
36
+ );
37
+ });
38
+ });
39
+
40
+ describe('removeColons', (): void => {
41
+ const fn = removeColons();
42
+
43
+ it('removes preceding ::Text -> Text', (): void => {
44
+ expect(fn('::Text')).toEqual('Text');
45
+ });
46
+
47
+ it('removes middle voting::TallyType -> TallyType', (): void => {
48
+ expect(fn('voting::TallyType')).toEqual('TallyType');
49
+ });
50
+
51
+ it('removes on embedded values (one)', (): void => {
52
+ expect(fn('(T::AccountId, SpanIndex)')).toEqual('(AccountId, SpanIndex)');
53
+ });
54
+
55
+ it('removes on embedded values (all)', (): void => {
56
+ expect(fn('(T::AccountId, slashing::SpanIndex)')).toEqual('(AccountId, SpanIndex)');
57
+ });
58
+ });
59
+
60
+ describe('bounded', (): void => {
61
+ const fn = removeExtensions('Bounded', true);
62
+
63
+ it('correctly cleans up bounded values', (): void => {
64
+ expect(fn('BoundedVec<u32, 256>')).toEqual('Vec<u32>');
65
+ });
66
+
67
+ it('correctly cleans up nested bounded values', (): void => {
68
+ expect(
69
+ fn('BoundedBTreeMap<BoundedVec<BoundedVec<u32, 1>, 2>, BoundedBTreeSet<u32, BoundedVec<u64, 3>, 4>, 5>')
70
+ ).toEqual('BTreeMap<Vec<Vec<u32>>,BTreeSet<u32,Vec<u64>>>');
71
+ });
72
+
73
+ it('cleans up values with trailing commas', (): void => {
74
+ expect(
75
+ flattenSingleTuple()(
76
+ fn('(BoundedVec<Announcement<T::AccountId, CallHashOf<T>, T::BlockNumber>, T::MaxPending,>,BalanceOf<T>,)')
77
+ )
78
+ ).toEqual('(Vec<Announcement<T::AccountId,CallHashOf<T>,T::BlockNumber>>,BalanceOf<T>)');
79
+ });
80
+ });
81
+
82
+ describe('weak', (): void => {
83
+ const fn = removeExtensions('Weak', false);
84
+
85
+ it('correctly cleans up weak values', (): void => {
86
+ expect(fn('WeakVec<u32>')).toEqual('Vec<u32>');
87
+ });
88
+ });
89
+ });
@@ -0,0 +1,290 @@
1
+ // Copyright 2017-2025 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { AnyString } from '../types/index.js';
5
+
6
+ type Mapper = (value: string) => string;
7
+
8
+ const BOUNDED = ['BTreeMap', 'BTreeSet', 'HashMap', 'Vec'];
9
+ const ALLOWED_BOXES = BOUNDED.concat(['Compact', 'DoNotConstruct', 'Int', 'Linkage', 'Range', 'RangeInclusive', 'Result', 'Opaque', 'Option', 'UInt', 'WrapperKeepOpaque', 'WrapperOpaque']);
10
+ const BOX_PRECEDING = ['<', '(', '[', '"', ',', ' ']; // start of vec, tuple, fixed array, part of struct def or in tuple
11
+
12
+ const mappings: Mapper[] = [
13
+ // alias <T::InherentOfflineReport as InherentOfflineReport>::Inherent -> InherentOfflineReport
14
+ alias('<T::InherentOfflineReport as InherentOfflineReport>::Inherent', 'InherentOfflineReport', false),
15
+ alias('VecDeque<', 'Vec<', false),
16
+ // <T::Balance as HasCompact>
17
+ cleanupCompact(),
18
+ // Change BoundedVec<Type, Size> to Vec<Type>
19
+ removeExtensions('Bounded', true),
20
+ // Change WeakVec<Type> to Vec<Type>
21
+ removeExtensions('Weak', false),
22
+ // Remove all the trait prefixes
23
+ removeTraits(),
24
+ // remove PairOf<T> -> (T, T)
25
+ removePairOf(),
26
+ // remove boxing, `Box<Proposal>` -> `Proposal`
27
+ removeWrap('Box<'),
28
+ // remove generics, `MisbehaviorReport<Hash, BlockNumber>` -> `MisbehaviorReport`
29
+ removeGenerics(),
30
+ // alias String -> Text (compat with jsonrpc methods)
31
+ alias('String', 'Text'),
32
+ // alias Vec<u8> -> Bytes
33
+ alias('Vec<u8>', 'Bytes'),
34
+ alias('&\\[u8\\]', 'Bytes'),
35
+ alias("&'static\\[u8\\]", 'Bytes'),
36
+ // alias RawAddress -> Address
37
+ alias('RawAddress', 'Address'),
38
+ // lookups, mapped to Address/AccountId as appropriate in runtime
39
+ alias('Lookup::Source', 'LookupSource'),
40
+ alias('Lookup::Target', 'LookupTarget'),
41
+ // HACK duplication between contracts & primitives, however contracts prefixed with exec
42
+ alias('exec::StorageKey', 'ContractStorageKey'),
43
+ // flattens tuples with one value, `(AccountId)` -> `AccountId`
44
+ flattenSingleTuple(),
45
+ // converts ::Type to Type, <T as Trait<I>>::Proposal -> Proposal
46
+ removeColons(),
47
+ // remove all trailing spaces - this should always be the last
48
+ trim()
49
+ ];
50
+
51
+ // given a string, trim it
52
+ export function trim (): Mapper {
53
+ return (value: string): string =>
54
+ value.trim();
55
+ }
56
+
57
+ // given a starting index, find the closing >
58
+ export function findClosing (value: string, start: number): number {
59
+ let depth = 0;
60
+
61
+ for (let i = start, count = value.length; i < count; i++) {
62
+ if (value[i] === '>') {
63
+ if (!depth) {
64
+ return i;
65
+ }
66
+
67
+ depth--;
68
+ } else if (value[i] === '<') {
69
+ depth++;
70
+ }
71
+ }
72
+
73
+ throw new Error(`Unable to find closing matching <> on '${value}' (start ${start})`);
74
+ }
75
+
76
+ export function alias (src: string, dest: string, withChecks = true): Mapper {
77
+ const from = new RegExp(`(^${src}|${BOX_PRECEDING.map((box) => `\\${box}${src}`).join('|')})`, 'g');
78
+
79
+ const to = (src: string): string => {
80
+ from.lastIndex = 0;
81
+
82
+ return withChecks && BOX_PRECEDING.includes(src[0])
83
+ ? `${src[0]}${dest}`
84
+ : dest;
85
+ };
86
+
87
+ return (value: string): string =>
88
+ value.replace(from, to);
89
+ }
90
+
91
+ export function cleanupCompact (): Mapper {
92
+ return (value: string): string => {
93
+ if (value.includes(' as HasCompact')) {
94
+ for (let i = 0, count = value.length; i < count; i++) {
95
+ if (value[i] === '<') {
96
+ const end = findClosing(value, i + 1) - 14;
97
+
98
+ if (value.substring(end, end + 14) === ' as HasCompact') {
99
+ value = `Compact<${value.substring(i + 1, end)}>`;
100
+ }
101
+ }
102
+ }
103
+ }
104
+
105
+ return value;
106
+ };
107
+ }
108
+
109
+ export function flattenSingleTuple (): Mapper {
110
+ const from1 = /,\)/g;
111
+ const from2 = /\(([^,]+)\)/;
112
+
113
+ return (value: string) => {
114
+ from1.lastIndex = 0;
115
+
116
+ return value
117
+ // tuples may have trailing commas, e.g. (u32, BlockNumber, )
118
+ .replace(from1, ')')
119
+ // change (u32) -> u32
120
+ .replace(from2, '$1');
121
+ };
122
+ }
123
+
124
+ function replaceTagWith (value: string, matcher: string, replacer: (inner: string) => string): string {
125
+ let index = -1;
126
+
127
+ while (true) {
128
+ index = value.indexOf(matcher, index + 1);
129
+
130
+ if (index === -1) {
131
+ return value;
132
+ }
133
+
134
+ const start = index + matcher.length;
135
+ const end = findClosing(value, start);
136
+
137
+ value = `${value.substring(0, index)}${replacer(value.substring(start, end))}${value.substring(end + 1)}`;
138
+ }
139
+ }
140
+
141
+ // remove the Bounded* or Weak* wrappers
142
+ export function removeExtensions (type: string, isSized: boolean): Mapper {
143
+ return (value: string): string => {
144
+ for (let i = 0, count = BOUNDED.length; i < count; i++) {
145
+ const tag = BOUNDED[i];
146
+
147
+ value = replaceTagWith(value, `${type}${tag}<`, (v: string): string => {
148
+ const parts = v
149
+ .split(',')
150
+ .map((s) => s.trim())
151
+ .filter((s) => s);
152
+
153
+ if (isSized) {
154
+ parts.pop();
155
+ }
156
+
157
+ return `${tag}<${parts.join(',')}>`;
158
+ });
159
+ }
160
+
161
+ return value;
162
+ };
163
+ }
164
+
165
+ export function removeColons (): Mapper {
166
+ return (value: string): string => {
167
+ let index = 0;
168
+
169
+ while (index !== -1) {
170
+ index = value.indexOf('::');
171
+
172
+ if (index === 0) {
173
+ value = value.substring(2);
174
+ } else if (index !== -1) {
175
+ let start = index;
176
+
177
+ while (start !== -1 && !BOX_PRECEDING.includes(value[start])) {
178
+ start--;
179
+ }
180
+
181
+ value = `${value.substring(0, start + 1)}${value.substring(index + 2)}`;
182
+ }
183
+ }
184
+
185
+ return value;
186
+ };
187
+ }
188
+
189
+ export function removeGenerics (): Mapper {
190
+ return (value: string): string => {
191
+ for (let i = 0, count = value.length; i < count; i++) {
192
+ if (value[i] === '<') {
193
+ // check against the allowed wrappers, be it Vec<..>, Option<...> ...
194
+ const box = ALLOWED_BOXES.find((box): boolean => {
195
+ const start = i - box.length;
196
+
197
+ return (
198
+ (
199
+ start >= 0 &&
200
+ value.substring(start, i) === box
201
+ ) && (
202
+ // make sure it is stand-alone, i.e. don't catch ElectionResult<...> as Result<...>
203
+ start === 0 ||
204
+ BOX_PRECEDING.includes(value[start - 1])
205
+ )
206
+ );
207
+ });
208
+
209
+ // we have not found anything, unwrap generic innards
210
+ if (!box) {
211
+ const end = findClosing(value, i + 1);
212
+
213
+ value = `${value.substring(0, i)}${value.substring(end + 1)}`;
214
+ }
215
+ }
216
+ }
217
+
218
+ return value;
219
+ };
220
+ }
221
+
222
+ // remove the PairOf wrappers
223
+ export function removePairOf (): Mapper {
224
+ const replacer = (v: string) => `(${v},${v})`;
225
+
226
+ return (value: string) =>
227
+ replaceTagWith(value, 'PairOf<', replacer);
228
+ }
229
+
230
+ // remove the type traits
231
+ export function removeTraits (): Mapper {
232
+ const from1 = /\s/g;
233
+ const from2 = /(T|Self)::/g;
234
+ const from3 = /<(T|Self)asTrait>::/g;
235
+ const from4 = /<Tas[a-z]+::Trait>::/g;
236
+ const from5 = /<LookupasStaticLookup>/g;
237
+ const from6 = /::Type/g;
238
+
239
+ return (value: string): string => {
240
+ from1.lastIndex = 0;
241
+ from2.lastIndex = 0;
242
+ from3.lastIndex = 0;
243
+ from4.lastIndex = 0;
244
+ from5.lastIndex = 0;
245
+ from6.lastIndex = 0;
246
+
247
+ return value
248
+ // remove all whitespaces
249
+ .replace(from1, '')
250
+ // anything `T::<type>` to end up as `<type>`
251
+ .replace(from2, '')
252
+ // replace `<T as Trait>::` (whitespaces were removed above)
253
+ .replace(from3, '')
254
+ // replace `<T as something::Trait>::` (whitespaces were removed above)
255
+ .replace(from4, '')
256
+ // replace <Lookup as StaticLookup>
257
+ .replace(from5, 'Lookup')
258
+ // replace `<...>::Type`
259
+ .replace(from6, '');
260
+ };
261
+ }
262
+
263
+ // remove wrapping values, i.e. Box<Proposal> -> Proposal
264
+ export function removeWrap (check: string): Mapper {
265
+ const replacer = (v: string) => v;
266
+
267
+ return (value: string) =>
268
+ replaceTagWith(value, check, replacer);
269
+ }
270
+
271
+ const sanitizeMap = new Map<string, string>();
272
+
273
+ export function sanitize (value: AnyString): string {
274
+ const startValue = value.toString();
275
+ const memoized = sanitizeMap.get(startValue);
276
+
277
+ if (memoized) {
278
+ return memoized;
279
+ }
280
+
281
+ let result = startValue;
282
+
283
+ for (let i = 0, count = mappings.length; i < count; i++) {
284
+ result = mappings[i](result);
285
+ }
286
+
287
+ sanitizeMap.set(startValue, result);
288
+
289
+ return result;
290
+ }