@polkadot/types-codec 6.12.2-10
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.
- package/abstract/AbstractArray.cjs +208 -0
- package/abstract/AbstractArray.d.ts +84 -0
- package/abstract/AbstractArray.js +197 -0
- package/abstract/AbstractInt.cjs +254 -0
- package/abstract/AbstractInt.d.ts +75 -0
- package/abstract/AbstractInt.js +231 -0
- package/abstract/index.cjs +21 -0
- package/abstract/index.d.ts +2 -0
- package/abstract/index.js +4 -0
- package/base/Base.cjs +137 -0
- package/base/Base.d.ts +56 -0
- package/base/Base.js +125 -0
- package/base/Compact.cjs +201 -0
- package/base/Compact.d.ts +83 -0
- package/base/Compact.js +186 -0
- package/base/DoNotConstruct.cjs +101 -0
- package/base/DoNotConstruct.d.ts +33 -0
- package/base/DoNotConstruct.js +88 -0
- package/base/Enum.cjs +445 -0
- package/base/Enum.d.ts +105 -0
- package/base/Enum.js +421 -0
- package/base/Int.cjs +45 -0
- package/base/Int.d.ts +16 -0
- package/base/Int.js +33 -0
- package/base/Null.cjs +111 -0
- package/base/Null.d.ts +54 -0
- package/base/Null.js +101 -0
- package/base/Option.cjs +256 -0
- package/base/Option.d.ts +88 -0
- package/base/Option.js +240 -0
- package/base/Result.cjs +109 -0
- package/base/Result.d.ts +46 -0
- package/base/Result.js +98 -0
- package/base/Tuple.cjs +132 -0
- package/base/Tuple.d.ts +39 -0
- package/base/Tuple.js +116 -0
- package/base/UInt.cjs +39 -0
- package/base/UInt.d.ts +15 -0
- package/base/UInt.js +29 -0
- package/base/Vec.cjs +126 -0
- package/base/Vec.d.ts +28 -0
- package/base/Vec.js +104 -0
- package/base/VecAny.cjs +31 -0
- package/base/VecAny.d.ts +15 -0
- package/base/VecAny.js +21 -0
- package/base/VecFixed.cjs +111 -0
- package/base/VecFixed.d.ts +26 -0
- package/base/VecFixed.js +93 -0
- package/base/index.cjs +109 -0
- package/base/index.d.ts +13 -0
- package/base/index.js +15 -0
- package/bundle.cjs +100 -0
- package/bundle.d.ts +7 -0
- package/bundle.js +9 -0
- package/detectPackage.cjs +9 -0
- package/detectPackage.d.ts +1 -0
- package/detectPackage.js +5 -0
- package/extended/BTreeMap.cjs +24 -0
- package/extended/BTreeMap.d.ts +5 -0
- package/extended/BTreeMap.js +14 -0
- package/extended/BTreeSet.cjs +227 -0
- package/extended/BTreeSet.d.ts +55 -0
- package/extended/BTreeSet.js +212 -0
- package/extended/BitVec.cjs +97 -0
- package/extended/BitVec.d.ts +26 -0
- package/extended/BitVec.js +82 -0
- package/extended/Bytes.cjs +86 -0
- package/extended/Bytes.d.ts +25 -0
- package/extended/Bytes.js +75 -0
- package/extended/HashMap.cjs +24 -0
- package/extended/HashMap.d.ts +5 -0
- package/extended/HashMap.js +14 -0
- package/extended/Linkage.cjs +89 -0
- package/extended/Linkage.d.ts +31 -0
- package/extended/Linkage.js +71 -0
- package/extended/Map.cjs +244 -0
- package/extended/Map.d.ts +50 -0
- package/extended/Map.js +225 -0
- package/extended/Range.cjs +92 -0
- package/extended/Range.d.ts +30 -0
- package/extended/Range.js +75 -0
- package/extended/Type.cjs +39 -0
- package/extended/Type.d.ts +16 -0
- package/extended/Type.js +27 -0
- package/extended/U8aFixed.cjs +66 -0
- package/extended/U8aFixed.d.ts +16 -0
- package/extended/U8aFixed.js +53 -0
- package/extended/WrapperOpaque.cjs +60 -0
- package/extended/WrapperOpaque.d.ts +14 -0
- package/extended/WrapperOpaque.js +49 -0
- package/extended/index.cjs +105 -0
- package/extended/index.d.ts +11 -0
- package/extended/index.js +13 -0
- package/index.cjs +20 -0
- package/index.d.ts +2 -0
- package/index.js +4 -0
- package/native/Bool.cjs +141 -0
- package/native/Bool.d.ts +62 -0
- package/native/Bool.js +130 -0
- package/native/Date.cjs +170 -0
- package/native/Date.d.ts +76 -0
- package/native/Date.js +158 -0
- package/native/Json.cjs +137 -0
- package/native/Json.d.ts +56 -0
- package/native/Json.js +121 -0
- package/native/Raw.cjs +192 -0
- package/native/Raw.d.ts +90 -0
- package/native/Raw.js +182 -0
- package/native/Set.cjs +268 -0
- package/native/Set.d.ts +76 -0
- package/native/Set.js +251 -0
- package/native/Struct.cjs +302 -0
- package/native/Struct.d.ts +93 -0
- package/native/Struct.js +286 -0
- package/native/Text.cjs +193 -0
- package/native/Text.d.ts +71 -0
- package/native/Text.js +178 -0
- package/native/index.cjs +79 -0
- package/native/index.d.ts +7 -0
- package/native/index.js +9 -0
- package/package.json +387 -0
- package/packageInfo.cjs +14 -0
- package/packageInfo.d.ts +4 -0
- package/packageInfo.js +7 -0
- package/primitive/I128.cjs +26 -0
- package/primitive/I128.d.ts +11 -0
- package/primitive/I128.js +16 -0
- package/primitive/I16.cjs +26 -0
- package/primitive/I16.d.ts +11 -0
- package/primitive/I16.js +16 -0
- package/primitive/I256.cjs +26 -0
- package/primitive/I256.d.ts +11 -0
- package/primitive/I256.js +16 -0
- package/primitive/I32.cjs +26 -0
- package/primitive/I32.d.ts +11 -0
- package/primitive/I32.js +16 -0
- package/primitive/I64.cjs +26 -0
- package/primitive/I64.d.ts +11 -0
- package/primitive/I64.js +16 -0
- package/primitive/I8.cjs +26 -0
- package/primitive/I8.d.ts +11 -0
- package/primitive/I8.js +16 -0
- package/primitive/U128.cjs +26 -0
- package/primitive/U128.d.ts +11 -0
- package/primitive/U128.js +16 -0
- package/primitive/U16.cjs +26 -0
- package/primitive/U16.d.ts +11 -0
- package/primitive/U16.js +16 -0
- package/primitive/U256.cjs +26 -0
- package/primitive/U256.d.ts +11 -0
- package/primitive/U256.js +16 -0
- package/primitive/U32.cjs +26 -0
- package/primitive/U32.d.ts +11 -0
- package/primitive/U32.js +16 -0
- package/primitive/U64.cjs +26 -0
- package/primitive/U64.d.ts +11 -0
- package/primitive/U64.js +16 -0
- package/primitive/U8.cjs +26 -0
- package/primitive/U8.d.ts +11 -0
- package/primitive/U8.js +16 -0
- package/primitive/USize.cjs +28 -0
- package/primitive/USize.d.ts +12 -0
- package/primitive/USize.js +18 -0
- package/primitive/index.cjs +187 -0
- package/primitive/index.d.ts +13 -0
- package/primitive/index.js +15 -0
- package/types/codec.cjs +1 -0
- package/types/codec.d.ts +77 -0
- package/types/codec.js +1 -0
- package/types/helpers.cjs +1 -0
- package/types/helpers.d.ts +14 -0
- package/types/helpers.js +1 -0
- package/types/index.cjs +57 -0
- package/types/index.d.ts +4 -0
- package/types/index.js +6 -0
- package/types/interfaces.cjs +1 -0
- package/types/interfaces.d.ts +69 -0
- package/types/interfaces.js +1 -0
- package/types/registry.cjs +1 -0
- package/types/registry.d.ts +61 -0
- package/types/registry.js +1 -0
- package/utils/compareArray.cjs +22 -0
- package/utils/compareArray.d.ts +1 -0
- package/utils/compareArray.js +13 -0
- package/utils/compareMap.cjs +39 -0
- package/utils/compareMap.d.ts +1 -0
- package/utils/compareMap.js +31 -0
- package/utils/compareSet.cjs +29 -0
- package/utils/compareSet.d.ts +1 -0
- package/utils/compareSet.js +22 -0
- package/utils/decodeU8a.cjs +69 -0
- package/utils/decodeU8a.d.ts +13 -0
- package/utils/decodeU8a.js +60 -0
- package/utils/index.cjs +95 -0
- package/utils/index.d.ts +9 -0
- package/utils/index.js +11 -0
- package/utils/mapToTypeMap.cjs +25 -0
- package/utils/mapToTypeMap.d.ts +5 -0
- package/utils/mapToTypeMap.js +17 -0
- package/utils/sanitize.cjs +242 -0
- package/utils/sanitize.d.ts +16 -0
- package/utils/sanitize.js +215 -0
- package/utils/sortValues.cjs +89 -0
- package/utils/sortValues.d.ts +13 -0
- package/utils/sortValues.js +73 -0
- package/utils/typeToConstructor.cjs +14 -0
- package/utils/typeToConstructor.d.ts +2 -0
- package/utils/typeToConstructor.js +6 -0
- package/utils/typesToMap.cjs +20 -0
- package/utils/typesToMap.d.ts +2 -0
- package/utils/typesToMap.js +13 -0
- package/utils/util.cjs +15 -0
- package/utils/util.d.ts +3 -0
- package/utils/util.js +7 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface SanitizeOptions {
|
|
2
|
+
allowNamespaces?: boolean;
|
|
3
|
+
}
|
|
4
|
+
declare type Mapper = (value: string, options?: SanitizeOptions) => string;
|
|
5
|
+
export declare function findClosing(value: string, start: number): number;
|
|
6
|
+
export declare function alias(src: string, dest: string, withChecks?: boolean): Mapper;
|
|
7
|
+
export declare function cleanupCompact(): Mapper;
|
|
8
|
+
export declare function flattenSingleTuple(): Mapper;
|
|
9
|
+
export declare function removeExtensions(type: string, isSized: boolean): Mapper;
|
|
10
|
+
export declare function removeColons(): Mapper;
|
|
11
|
+
export declare function removeGenerics(): Mapper;
|
|
12
|
+
export declare function removePairOf(): Mapper;
|
|
13
|
+
export declare function removeTraits(): Mapper;
|
|
14
|
+
export declare function removeWrap(check: string): Mapper;
|
|
15
|
+
export declare function sanitize(value: String | string, options?: SanitizeOptions): string;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
const BOUNDED = ['BTreeMap', 'BTreeSet', 'HashMap', 'Vec'];
|
|
4
|
+
const ALLOWED_BOXES = BOUNDED.concat(['Compact', 'DoNotConstruct', 'Int', 'Linkage', 'Range', 'RangeInclusive', 'Result', 'Option', 'UInt', 'WrapperOpaque']);
|
|
5
|
+
const BOX_PRECEDING = ['<', '(', '[', '"', ',', ' ']; // start of vec, tuple, fixed array, part of struct def or in tuple
|
|
6
|
+
|
|
7
|
+
const mappings = [// alias <T::InherentOfflineReport as InherentOfflineReport>::Inherent -> InherentOfflineReport
|
|
8
|
+
alias('<T::InherentOfflineReport as InherentOfflineReport>::Inherent', 'InherentOfflineReport', false), alias('VecDeque<', 'Vec<', false), // <T::Balance as HasCompact>
|
|
9
|
+
cleanupCompact(), // Change BoundedVec<Type, Size> to Vec<Type>
|
|
10
|
+
removeExtensions('Bounded', true), // Change WeakVec<Type> to Vec<Type>
|
|
11
|
+
removeExtensions('Weak', false), // Remove all the trait prefixes
|
|
12
|
+
removeTraits(), // remove PairOf<T> -> (T, T)
|
|
13
|
+
removePairOf(), // remove boxing, `Box<Proposal>` -> `Proposal`
|
|
14
|
+
removeWrap('Box<'), // remove generics, `MisbehaviorReport<Hash, BlockNumber>` -> `MisbehaviorReport`
|
|
15
|
+
removeGenerics(), // alias String -> Text (compat with jsonrpc methods)
|
|
16
|
+
alias('String', 'Text'), // alias Vec<u8> -> Bytes
|
|
17
|
+
alias('Vec<u8>', 'Bytes'), alias('&\\[u8\\]', 'Bytes'), alias("&'static\\[u8\\]", 'Bytes'), // alias RawAddress -> Address
|
|
18
|
+
alias('RawAddress', 'Address'), // lookups, mapped to Address/AccountId as appropriate in runtime
|
|
19
|
+
alias('Lookup::Source', 'LookupSource'), alias('Lookup::Target', 'LookupTarget'), // HACK duplication between contracts & primitives, however contracts prefixed with exec
|
|
20
|
+
alias('exec::StorageKey', 'ContractStorageKey'), // flattens tuples with one value, `(AccountId)` -> `AccountId`
|
|
21
|
+
flattenSingleTuple(), // converts ::Type to Type, <T as Trait<I>>::Proposal -> Proposal
|
|
22
|
+
removeColons()]; // given a starting index, find the closing >
|
|
23
|
+
|
|
24
|
+
export function findClosing(value, start) {
|
|
25
|
+
let depth = 0;
|
|
26
|
+
|
|
27
|
+
for (let index = start; index < value.length; index++) {
|
|
28
|
+
if (value[index] === '>') {
|
|
29
|
+
if (!depth) {
|
|
30
|
+
return index;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
depth--;
|
|
34
|
+
} else if (value[index] === '<') {
|
|
35
|
+
depth++;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
throw new Error(`Unable to find closing matching <> on '${value}' (start ${start})`);
|
|
40
|
+
}
|
|
41
|
+
export function alias(src, dest, withChecks = true) {
|
|
42
|
+
const from = new RegExp(`(^${src}|${BOX_PRECEDING.map(box => `\\${box}${src}`).join('|')})`, 'g');
|
|
43
|
+
|
|
44
|
+
const to = src => {
|
|
45
|
+
from.lastIndex = 0;
|
|
46
|
+
return withChecks && BOX_PRECEDING.includes(src[0]) ? `${src[0]}${dest}` : dest;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
return value => value.replace(from, to);
|
|
50
|
+
}
|
|
51
|
+
export function cleanupCompact() {
|
|
52
|
+
return value => {
|
|
53
|
+
for (let index = 0; index < value.length; index++) {
|
|
54
|
+
if (value[index] === '<') {
|
|
55
|
+
const end = findClosing(value, index + 1) - 14;
|
|
56
|
+
|
|
57
|
+
if (value.substr(end, 14) === ' as HasCompact') {
|
|
58
|
+
value = `Compact<${value.substr(index + 1, end - index - 1)}>`;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return value;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export function flattenSingleTuple() {
|
|
67
|
+
const from1 = /,\)/g;
|
|
68
|
+
const from2 = /\(([^,]+)\)/;
|
|
69
|
+
return value => {
|
|
70
|
+
from1.lastIndex = 0;
|
|
71
|
+
return value // tuples may have trailing commas, e.g. (u32, BlockNumber, )
|
|
72
|
+
.replace(from1, ')') // change (u32) -> u32
|
|
73
|
+
.replace(from2, '$1');
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function replaceTagWith(value, matcher, replacer) {
|
|
78
|
+
let index = -1;
|
|
79
|
+
|
|
80
|
+
while (true) {
|
|
81
|
+
index = value.indexOf(matcher, index + 1);
|
|
82
|
+
|
|
83
|
+
if (index === -1) {
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const start = index + matcher.length;
|
|
88
|
+
const end = findClosing(value, start);
|
|
89
|
+
value = `${value.substr(0, index)}${replacer(value.substr(start, end - start))}${value.substr(end + 1)}`;
|
|
90
|
+
}
|
|
91
|
+
} // remove the Bounded* or Weak* wrappers
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
export function removeExtensions(type, isSized) {
|
|
95
|
+
return value => BOUNDED.reduce((value, tag) => replaceTagWith(value, `${type}${tag}<`, inner => {
|
|
96
|
+
const parts = inner.split(',').map(s => s.trim()).filter(s => s);
|
|
97
|
+
|
|
98
|
+
if (isSized) {
|
|
99
|
+
parts.pop();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return `${tag}<${parts.join(',')}>`;
|
|
103
|
+
}), value);
|
|
104
|
+
}
|
|
105
|
+
export function removeColons() {
|
|
106
|
+
return (value, {
|
|
107
|
+
allowNamespaces
|
|
108
|
+
} = {}) => {
|
|
109
|
+
let index = 0;
|
|
110
|
+
|
|
111
|
+
while (index !== -1) {
|
|
112
|
+
index = value.indexOf('::');
|
|
113
|
+
|
|
114
|
+
if (index === 0) {
|
|
115
|
+
value = value.substr(2);
|
|
116
|
+
} else if (index !== -1) {
|
|
117
|
+
if (allowNamespaces) {
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
let start = index;
|
|
122
|
+
|
|
123
|
+
while (start !== -1 && !BOX_PRECEDING.includes(value[start])) {
|
|
124
|
+
start--;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
value = `${value.substr(0, start + 1)}${value.substr(index + 2)}`;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return value;
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
export function removeGenerics() {
|
|
135
|
+
return value => {
|
|
136
|
+
for (let index = 0; index < value.length; index++) {
|
|
137
|
+
if (value[index] === '<') {
|
|
138
|
+
// check against the allowed wrappers, be it Vec<..>, Option<...> ...
|
|
139
|
+
const box = ALLOWED_BOXES.find(box => {
|
|
140
|
+
const start = index - box.length;
|
|
141
|
+
return start >= 0 && value.substr(start, box.length) === box && ( // make sure it is stand-alone, i.e. don't catch ElectionResult<...> as Result<...>
|
|
142
|
+
start === 0 || BOX_PRECEDING.includes(value[start - 1]));
|
|
143
|
+
}); // we have not found anything, unwrap generic innards
|
|
144
|
+
|
|
145
|
+
if (!box) {
|
|
146
|
+
const end = findClosing(value, index + 1);
|
|
147
|
+
value = `${value.substr(0, index)}${value.substr(end + 1)}`;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return value;
|
|
153
|
+
};
|
|
154
|
+
} // remove the PairOf wrappers
|
|
155
|
+
|
|
156
|
+
export function removePairOf() {
|
|
157
|
+
const replacer = inner => `(${inner},${inner})`;
|
|
158
|
+
|
|
159
|
+
return value => replaceTagWith(value, 'PairOf<', replacer);
|
|
160
|
+
} // remove the type traits
|
|
161
|
+
|
|
162
|
+
export function removeTraits() {
|
|
163
|
+
const from1 = /\s/g;
|
|
164
|
+
const from2 = /(T|Self)::/g;
|
|
165
|
+
const from3 = /<(T|Self)asTrait>::/g;
|
|
166
|
+
const from4 = /<Tas[a-z]+::Trait>::/g;
|
|
167
|
+
const from5 = /<LookupasStaticLookup>/g;
|
|
168
|
+
const from6 = /::Type/g;
|
|
169
|
+
return value => {
|
|
170
|
+
from1.lastIndex = 0;
|
|
171
|
+
from2.lastIndex = 0;
|
|
172
|
+
from3.lastIndex = 0;
|
|
173
|
+
from4.lastIndex = 0;
|
|
174
|
+
from5.lastIndex = 0;
|
|
175
|
+
from6.lastIndex = 0;
|
|
176
|
+
return value // remove all whitespaces
|
|
177
|
+
.replace(from1, '') // anything `T::<type>` to end up as `<type>`
|
|
178
|
+
.replace(from2, '') // replace `<T as Trait>::` (whitespaces were removed above)
|
|
179
|
+
.replace(from3, '') // replace `<T as something::Trait>::` (whitespaces were removed above)
|
|
180
|
+
.replace(from4, '') // replace <Lookup as StaticLookup>
|
|
181
|
+
.replace(from5, 'Lookup') // replace `<...>::Type`
|
|
182
|
+
.replace(from6, '');
|
|
183
|
+
};
|
|
184
|
+
} // remove wrapping values, i.e. Box<Proposal> -> Proposal
|
|
185
|
+
|
|
186
|
+
export function removeWrap(check) {
|
|
187
|
+
const replacer = inner => inner;
|
|
188
|
+
|
|
189
|
+
return value => replaceTagWith(value, check, replacer);
|
|
190
|
+
}
|
|
191
|
+
const sanitizeMap = new Map(); // eslint-disable-next-line @typescript-eslint/ban-types
|
|
192
|
+
|
|
193
|
+
export function sanitize(value, options) {
|
|
194
|
+
let result = value.toString();
|
|
195
|
+
|
|
196
|
+
if (!options) {
|
|
197
|
+
const memoized = sanitizeMap.get(result);
|
|
198
|
+
|
|
199
|
+
if (memoized) {
|
|
200
|
+
return memoized;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
for (let i = 0; i < mappings.length; i++) {
|
|
205
|
+
result = mappings[i](result, options);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
result = result.trim();
|
|
209
|
+
|
|
210
|
+
if (!options) {
|
|
211
|
+
sanitizeMap.set(value.toString(), result);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return result;
|
|
215
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.sortAsc = sortAsc;
|
|
7
|
+
exports.sortMap = sortMap;
|
|
8
|
+
exports.sortSet = sortSet;
|
|
9
|
+
|
|
10
|
+
var _util = require("@polkadot/util");
|
|
11
|
+
|
|
12
|
+
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
|
|
13
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
14
|
+
|
|
15
|
+
/** @internal **/
|
|
16
|
+
function isArrayLike(arg) {
|
|
17
|
+
return arg instanceof Uint8Array || Array.isArray(arg);
|
|
18
|
+
}
|
|
19
|
+
/** @internal **/
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
function isCodec(arg) {
|
|
23
|
+
return (0, _util.isFunction)(arg && arg.toU8a);
|
|
24
|
+
}
|
|
25
|
+
/** @internal **/
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
function isEnum(arg) {
|
|
29
|
+
return isCodec(arg) && (0, _util.isNumber)(arg.index) && isCodec(arg.value);
|
|
30
|
+
}
|
|
31
|
+
/** @internal */
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
function isNumberLike(arg) {
|
|
35
|
+
return (0, _util.isNumber)(arg) || (0, _util.isBn)(arg) || (0, _util.isBigInt)(arg);
|
|
36
|
+
}
|
|
37
|
+
/** @internal */
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
function sortArray(a, b) {
|
|
41
|
+
// Vec, Tuple, Bytes etc.
|
|
42
|
+
let sortRes = 0;
|
|
43
|
+
const minLen = Math.min(a.length, b.length);
|
|
44
|
+
|
|
45
|
+
for (let i = 0; i < minLen; ++i) {
|
|
46
|
+
sortRes = sortAsc(a[i], b[i]);
|
|
47
|
+
|
|
48
|
+
if (sortRes !== 0) {
|
|
49
|
+
return sortRes;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return a.length - b.length;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Sort keys/values of BTreeSet/BTreeMap in ascending order for encoding compatibility with Rust's BTreeSet/BTreeMap
|
|
57
|
+
* (https://doc.rust-lang.org/stable/std/collections/struct.BTreeSet.html)
|
|
58
|
+
* (https://doc.rust-lang.org/stable/std/collections/struct.BTreeMap.html)
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
function sortAsc(a, b) {
|
|
63
|
+
if (isNumberLike(a) && isNumberLike(b)) {
|
|
64
|
+
return (0, _util.bnToBn)(a).cmp((0, _util.bnToBn)(b));
|
|
65
|
+
} else if (a instanceof Map && b instanceof Map) {
|
|
66
|
+
return sortAsc(Array.from(a.values()), Array.from(b.values()));
|
|
67
|
+
} else if (isEnum(a) && isEnum(b)) {
|
|
68
|
+
return sortAsc(a.index, b.index) || sortAsc(a.value, b.value);
|
|
69
|
+
} else if (isArrayLike(a) && isArrayLike(b)) {
|
|
70
|
+
return sortArray(a, b);
|
|
71
|
+
} else if (isCodec(a) && isCodec(b)) {
|
|
72
|
+
// Text, Bool etc.
|
|
73
|
+
return sortAsc(a.toU8a(true), b.toU8a(true));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
throw new Error(`Attempting to sort unrecognized values: ${(0, _util.stringify)(a)} (typeof ${typeof a}) <-> ${(0, _util.stringify)(b)} (typeof ${typeof b})`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function sortSet(set) {
|
|
80
|
+
return new Set(Array.from(set).sort(sortAsc));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function sortMap(map) {
|
|
84
|
+
return new Map(Array.from(map.entries()).sort((_ref, _ref2) => {
|
|
85
|
+
let [keyA] = _ref;
|
|
86
|
+
let [keyB] = _ref2;
|
|
87
|
+
return sortAsc(keyA, keyB);
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
2
|
+
import type { BN } from '@polkadot/util';
|
|
3
|
+
import type { Codec } from '../types';
|
|
4
|
+
declare type SortArg = Codec | Codec[] | number[] | BN | bigint | number | Uint8Array;
|
|
5
|
+
/**
|
|
6
|
+
* Sort keys/values of BTreeSet/BTreeMap in ascending order for encoding compatibility with Rust's BTreeSet/BTreeMap
|
|
7
|
+
* (https://doc.rust-lang.org/stable/std/collections/struct.BTreeSet.html)
|
|
8
|
+
* (https://doc.rust-lang.org/stable/std/collections/struct.BTreeMap.html)
|
|
9
|
+
*/
|
|
10
|
+
export declare function sortAsc<V extends SortArg = Codec>(a: V, b: V): number;
|
|
11
|
+
export declare function sortSet<V extends Codec = Codec>(set: Set<V>): Set<V>;
|
|
12
|
+
export declare function sortMap<K extends Codec = Codec, V extends Codec = Codec>(map: Map<K, V>): Map<K, V>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { bnToBn, isBigInt, isBn, isFunction, isNumber, stringify } from '@polkadot/util';
|
|
4
|
+
|
|
5
|
+
/** @internal **/
|
|
6
|
+
function isArrayLike(arg) {
|
|
7
|
+
return arg instanceof Uint8Array || Array.isArray(arg);
|
|
8
|
+
}
|
|
9
|
+
/** @internal **/
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
function isCodec(arg) {
|
|
13
|
+
return isFunction(arg && arg.toU8a);
|
|
14
|
+
}
|
|
15
|
+
/** @internal **/
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
function isEnum(arg) {
|
|
19
|
+
return isCodec(arg) && isNumber(arg.index) && isCodec(arg.value);
|
|
20
|
+
}
|
|
21
|
+
/** @internal */
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
function isNumberLike(arg) {
|
|
25
|
+
return isNumber(arg) || isBn(arg) || isBigInt(arg);
|
|
26
|
+
}
|
|
27
|
+
/** @internal */
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
function sortArray(a, b) {
|
|
31
|
+
// Vec, Tuple, Bytes etc.
|
|
32
|
+
let sortRes = 0;
|
|
33
|
+
const minLen = Math.min(a.length, b.length);
|
|
34
|
+
|
|
35
|
+
for (let i = 0; i < minLen; ++i) {
|
|
36
|
+
sortRes = sortAsc(a[i], b[i]);
|
|
37
|
+
|
|
38
|
+
if (sortRes !== 0) {
|
|
39
|
+
return sortRes;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return a.length - b.length;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Sort keys/values of BTreeSet/BTreeMap in ascending order for encoding compatibility with Rust's BTreeSet/BTreeMap
|
|
47
|
+
* (https://doc.rust-lang.org/stable/std/collections/struct.BTreeSet.html)
|
|
48
|
+
* (https://doc.rust-lang.org/stable/std/collections/struct.BTreeMap.html)
|
|
49
|
+
*/
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
export function sortAsc(a, b) {
|
|
53
|
+
if (isNumberLike(a) && isNumberLike(b)) {
|
|
54
|
+
return bnToBn(a).cmp(bnToBn(b));
|
|
55
|
+
} else if (a instanceof Map && b instanceof Map) {
|
|
56
|
+
return sortAsc(Array.from(a.values()), Array.from(b.values()));
|
|
57
|
+
} else if (isEnum(a) && isEnum(b)) {
|
|
58
|
+
return sortAsc(a.index, b.index) || sortAsc(a.value, b.value);
|
|
59
|
+
} else if (isArrayLike(a) && isArrayLike(b)) {
|
|
60
|
+
return sortArray(a, b);
|
|
61
|
+
} else if (isCodec(a) && isCodec(b)) {
|
|
62
|
+
// Text, Bool etc.
|
|
63
|
+
return sortAsc(a.toU8a(true), b.toU8a(true));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
throw new Error(`Attempting to sort unrecognized values: ${stringify(a)} (typeof ${typeof a}) <-> ${stringify(b)} (typeof ${typeof b})`);
|
|
67
|
+
}
|
|
68
|
+
export function sortSet(set) {
|
|
69
|
+
return new Set(Array.from(set).sort(sortAsc));
|
|
70
|
+
}
|
|
71
|
+
export function sortMap(map) {
|
|
72
|
+
return new Map(Array.from(map.entries()).sort(([keyA], [keyB]) => sortAsc(keyA, keyB)));
|
|
73
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.typeToConstructor = typeToConstructor;
|
|
7
|
+
|
|
8
|
+
var _util = require("@polkadot/util");
|
|
9
|
+
|
|
10
|
+
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
|
|
11
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
12
|
+
function typeToConstructor(registry, type) {
|
|
13
|
+
return (0, _util.isString)(type) ? registry.createClassUnsafe(type) : type;
|
|
14
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { isString } from '@polkadot/util';
|
|
4
|
+
export function typeToConstructor(registry, type) {
|
|
5
|
+
return isString(type) ? registry.createClassUnsafe(type) : type;
|
|
6
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.typesToMap = typesToMap;
|
|
7
|
+
|
|
8
|
+
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
|
|
9
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
10
|
+
function typesToMap(registry, Types) {
|
|
11
|
+
const entries = Object.entries(Types);
|
|
12
|
+
const result = {};
|
|
13
|
+
|
|
14
|
+
for (let i = 0; i < entries.length; i++) {
|
|
15
|
+
const [key, Type] = entries[i];
|
|
16
|
+
result[key] = registry.getClassName(Type) || new Type(registry).toRawType();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
export function typesToMap(registry, Types) {
|
|
4
|
+
const entries = Object.entries(Types);
|
|
5
|
+
const result = {};
|
|
6
|
+
|
|
7
|
+
for (let i = 0; i < entries.length; i++) {
|
|
8
|
+
const [key, Type] = entries[i];
|
|
9
|
+
result[key] = registry.getClassName(Type) || new Type(registry).toRawType();
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return result;
|
|
13
|
+
}
|
package/utils/util.cjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.hasEq = hasEq;
|
|
7
|
+
|
|
8
|
+
var _util = require("@polkadot/util");
|
|
9
|
+
|
|
10
|
+
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
|
|
11
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
12
|
+
function hasEq(o) {
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
14
|
+
return (0, _util.isFunction)(o.eq);
|
|
15
|
+
}
|
package/utils/util.d.ts
ADDED
package/utils/util.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { isFunction } from '@polkadot/util';
|
|
4
|
+
export function hasEq(o) {
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
6
|
+
return isFunction(o.eq);
|
|
7
|
+
}
|