@polkadot/types-codec 8.5.2-1 → 8.5.2-4

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/base/Option.d.ts CHANGED
@@ -41,7 +41,7 @@ export declare class Option<T extends Codec> implements IOption<T> {
41
41
  /**
42
42
  * @description The actual value for the Option
43
43
  */
44
- get value(): Codec;
44
+ get value(): T;
45
45
  /**
46
46
  * @description Compares the value of the input to see if there is a match
47
47
  */
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.OptionBool = void 0;
7
+
8
+ var _util = require("@polkadot/util");
9
+
10
+ var _Option = require("../base/Option");
11
+
12
+ var _Bool = require("../native/Bool");
13
+
14
+ // Copyright 2017-2022 @polkadot/types-codec authors & contributors
15
+ // SPDX-License-Identifier: Apache-2.0
16
+ function decodeU8a(registry, value) {
17
+ // Encoded as -
18
+ // - 0 = None
19
+ // - 1 = True
20
+ // - 2 = False
21
+ return value[0] === 0 ? null : new _Bool.bool(registry, value[0] === 1);
22
+ }
23
+ /**
24
+ * @name OptionBool
25
+ * @description A specific implementation of Option<bool> than allows for single-byte encoding
26
+ */
27
+
28
+
29
+ class OptionBool extends _Option.Option {
30
+ constructor(registry, value) {
31
+ super(registry, _Bool.bool, (0, _util.isU8a)(value) || (0, _util.isHex)(value) ? decodeU8a(registry, (0, _util.u8aToU8a)(value)) : value);
32
+ }
33
+ /**
34
+ * @description The length of the value when encoded as a Uint8Array
35
+ */
36
+
37
+
38
+ get encodedLength() {
39
+ return 1;
40
+ }
41
+ /**
42
+ * @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
43
+ */
44
+
45
+
46
+ get initialU8aLength() {
47
+ return 1;
48
+ }
49
+ /**
50
+ * @description Checks if the value is an empty value (always false)
51
+ */
52
+
53
+
54
+ get isFalse() {
55
+ return this.isSome ? !this.value.valueOf() : false;
56
+ }
57
+ /**
58
+ * @description Checks if the value is an empty value (always false)
59
+ */
60
+
61
+
62
+ get isTrue() {
63
+ return this.isSome ? this.value.valueOf() : false;
64
+ }
65
+ /**
66
+ * @description Returns a breakdown of the hex encoding for this Codec
67
+ */
68
+
69
+
70
+ inspect() {
71
+ return {
72
+ outer: [this.toU8a()]
73
+ };
74
+ }
75
+ /**
76
+ * @description Returns the base runtime type name for this instance
77
+ */
78
+
79
+
80
+ toRawType(isBare) {
81
+ return isBare ? 'bool' : 'Option<bool>';
82
+ }
83
+ /**
84
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
85
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
86
+ */
87
+
88
+
89
+ toU8a(isBare) {
90
+ if (isBare) {
91
+ return super.toU8a(true);
92
+ }
93
+
94
+ return this.isSome ? new Uint8Array([this.isTrue ? 1 : 2]) : new Uint8Array([0]);
95
+ }
96
+
97
+ }
98
+
99
+ exports.OptionBool = OptionBool;
@@ -51,6 +51,12 @@ Object.defineProperty(exports, "Map", {
51
51
  return _Map.CodecMap;
52
52
  }
53
53
  });
54
+ Object.defineProperty(exports, "OptionBool", {
55
+ enumerable: true,
56
+ get: function () {
57
+ return _OptionBool.OptionBool;
58
+ }
59
+ });
54
60
  Object.defineProperty(exports, "Range", {
55
61
  enumerable: true,
56
62
  get: function () {
@@ -102,6 +108,8 @@ var _Linkage = require("./Linkage");
102
108
 
103
109
  var _Map = require("./Map");
104
110
 
111
+ var _OptionBool = require("./OptionBool");
112
+
105
113
  var _Range = require("./Range");
106
114
 
107
115
  var _RangeInclusive = require("./RangeInclusive");
@@ -11,6 +11,6 @@ const packageInfo = {
11
11
  name: '@polkadot/types-codec',
12
12
  path: typeof __dirname === 'string' ? __dirname : 'auto',
13
13
  type: 'cjs',
14
- version: '8.5.2-1'
14
+ version: '8.5.2-4'
15
15
  };
16
16
  exports.packageInfo = packageInfo;
@@ -14,6 +14,7 @@ exports.removePairOf = removePairOf;
14
14
  exports.removeTraits = removeTraits;
15
15
  exports.removeWrap = removeWrap;
16
16
  exports.sanitize = sanitize;
17
+ exports.trim = trim;
17
18
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
18
19
  // SPDX-License-Identifier: Apache-2.0
19
20
  const BOUNDED = ['BTreeMap', 'BTreeSet', 'HashMap', 'Vec'];
@@ -35,7 +36,13 @@ alias('RawAddress', 'Address'), // lookups, mapped to Address/AccountId as appro
35
36
  alias('Lookup::Source', 'LookupSource'), alias('Lookup::Target', 'LookupTarget'), // HACK duplication between contracts & primitives, however contracts prefixed with exec
36
37
  alias('exec::StorageKey', 'ContractStorageKey'), // flattens tuples with one value, `(AccountId)` -> `AccountId`
37
38
  flattenSingleTuple(), // converts ::Type to Type, <T as Trait<I>>::Proposal -> Proposal
38
- removeColons()]; // given a starting index, find the closing >
39
+ removeColons(), // remove all trailing spaces - this should always be the last
40
+ trim()]; // given a string, trim it
41
+
42
+ function trim() {
43
+ return value => value.trim();
44
+ } // given a starting index, find the closing >
45
+
39
46
 
40
47
  function findClosing(value, start) {
41
48
  let depth = 0;
@@ -218,10 +225,11 @@ function removeWrap(check) {
218
225
  const sanitizeMap = new Map(); // eslint-disable-next-line @typescript-eslint/ban-types
219
226
 
220
227
  function sanitize(value, options) {
221
- let result = value.toString();
228
+ const startValue = value.toString();
229
+ let result = startValue;
222
230
 
223
231
  if (!options) {
224
- const memoized = sanitizeMap.get(result);
232
+ const memoized = sanitizeMap.get(startValue);
225
233
 
226
234
  if (memoized) {
227
235
  return memoized;
@@ -232,10 +240,8 @@ function sanitize(value, options) {
232
240
  result = mappings[i](result, options);
233
241
  }
234
242
 
235
- result = result.trim();
236
-
237
243
  if (!options) {
238
- sanitizeMap.set(value.toString(), result);
244
+ sanitizeMap.set(startValue, result);
239
245
  }
240
246
 
241
247
  return result;
@@ -0,0 +1,40 @@
1
+ import type { HexString } from '@polkadot/util/types';
2
+ import type { AnyBool, Inspect, Registry } from '../types';
3
+ import { Option } from '../base/Option';
4
+ import { bool as Bool } from '../native/Bool';
5
+ /**
6
+ * @name OptionBool
7
+ * @description A specific implementation of Option<bool> than allows for single-byte encoding
8
+ */
9
+ export declare class OptionBool extends Option<Bool> {
10
+ constructor(registry: Registry, value?: Option<Bool> | AnyBool | Uint8Array | HexString | null);
11
+ /**
12
+ * @description The length of the value when encoded as a Uint8Array
13
+ */
14
+ get encodedLength(): number;
15
+ /**
16
+ * @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
17
+ */
18
+ get initialU8aLength(): number;
19
+ /**
20
+ * @description Checks if the value is an empty value (always false)
21
+ */
22
+ get isFalse(): boolean;
23
+ /**
24
+ * @description Checks if the value is an empty value (always false)
25
+ */
26
+ get isTrue(): boolean;
27
+ /**
28
+ * @description Returns a breakdown of the hex encoding for this Codec
29
+ */
30
+ inspect(): Inspect;
31
+ /**
32
+ * @description Returns the base runtime type name for this instance
33
+ */
34
+ toRawType(isBare?: boolean): string;
35
+ /**
36
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
37
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
38
+ */
39
+ toU8a(isBare?: boolean): Uint8Array;
40
+ }
@@ -0,0 +1,88 @@
1
+ // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { isHex, isU8a, u8aToU8a } from '@polkadot/util';
4
+ import { Option } from "../base/Option.js";
5
+ import { bool as Bool } from "../native/Bool.js";
6
+
7
+ function decodeU8a(registry, value) {
8
+ // Encoded as -
9
+ // - 0 = None
10
+ // - 1 = True
11
+ // - 2 = False
12
+ return value[0] === 0 ? null : new Bool(registry, value[0] === 1);
13
+ }
14
+ /**
15
+ * @name OptionBool
16
+ * @description A specific implementation of Option<bool> than allows for single-byte encoding
17
+ */
18
+
19
+
20
+ export class OptionBool extends Option {
21
+ constructor(registry, value) {
22
+ super(registry, Bool, isU8a(value) || isHex(value) ? decodeU8a(registry, u8aToU8a(value)) : value);
23
+ }
24
+ /**
25
+ * @description The length of the value when encoded as a Uint8Array
26
+ */
27
+
28
+
29
+ get encodedLength() {
30
+ return 1;
31
+ }
32
+ /**
33
+ * @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
34
+ */
35
+
36
+
37
+ get initialU8aLength() {
38
+ return 1;
39
+ }
40
+ /**
41
+ * @description Checks if the value is an empty value (always false)
42
+ */
43
+
44
+
45
+ get isFalse() {
46
+ return this.isSome ? !this.value.valueOf() : false;
47
+ }
48
+ /**
49
+ * @description Checks if the value is an empty value (always false)
50
+ */
51
+
52
+
53
+ get isTrue() {
54
+ return this.isSome ? this.value.valueOf() : false;
55
+ }
56
+ /**
57
+ * @description Returns a breakdown of the hex encoding for this Codec
58
+ */
59
+
60
+
61
+ inspect() {
62
+ return {
63
+ outer: [this.toU8a()]
64
+ };
65
+ }
66
+ /**
67
+ * @description Returns the base runtime type name for this instance
68
+ */
69
+
70
+
71
+ toRawType(isBare) {
72
+ return isBare ? 'bool' : 'Option<bool>';
73
+ }
74
+ /**
75
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
76
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
77
+ */
78
+
79
+
80
+ toU8a(isBare) {
81
+ if (isBare) {
82
+ return super.toU8a(true);
83
+ }
84
+
85
+ return this.isSome ? new Uint8Array([this.isTrue ? 1 : 2]) : new Uint8Array([0]);
86
+ }
87
+
88
+ }
@@ -5,6 +5,7 @@ export { Bytes } from './Bytes';
5
5
  export { HashMap } from './HashMap';
6
6
  export { Linkage } from './Linkage';
7
7
  export { CodecMap, CodecMap as Map } from './Map';
8
+ export { OptionBool } from './OptionBool';
8
9
  export { Range } from './Range';
9
10
  export { RangeInclusive } from './RangeInclusive';
10
11
  export { Type } from './Type';
package/extended/index.js CHANGED
@@ -7,6 +7,7 @@ export { Bytes } from "./Bytes.js";
7
7
  export { HashMap } from "./HashMap.js";
8
8
  export { Linkage } from "./Linkage.js";
9
9
  export { CodecMap, CodecMap as Map } from "./Map.js";
10
+ export { OptionBool } from "./OptionBool.js";
10
11
  export { Range } from "./Range.js";
11
12
  export { RangeInclusive } from "./RangeInclusive.js";
12
13
  export { Type } from "./Type.js";
package/native/Text.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { HexString } from '@polkadot/util/types';
2
- import type { AnyU8a, Inspect, IText, IU8a, Registry } from '../types';
2
+ import type { AnyString, AnyU8a, Inspect, IText, IU8a, Registry } from '../types';
3
3
  /**
4
4
  * @name Text
5
5
  * @description
@@ -12,7 +12,7 @@ export declare class Text extends String implements IText {
12
12
  #private;
13
13
  readonly registry: Registry;
14
14
  createdAtHash?: IU8a;
15
- constructor(registry: Registry, value?: null | Text | string | AnyU8a | {
15
+ constructor(registry: Registry, value?: null | AnyString | AnyU8a | {
16
16
  toString: () => string;
17
17
  });
18
18
  /**
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "./cjs/detectPackage.js"
21
21
  ],
22
22
  "type": "module",
23
- "version": "8.5.2-1",
23
+ "version": "8.5.2-4",
24
24
  "main": "./cjs/index.js",
25
25
  "module": "./index.js",
26
26
  "types": "./index.d.ts",
@@ -172,6 +172,11 @@
172
172
  "require": "./cjs/extended/Map.js",
173
173
  "default": "./extended/Map.js"
174
174
  },
175
+ "./extended/OptionBool": {
176
+ "types": "./extended/OptionBool.d.ts",
177
+ "require": "./cjs/extended/OptionBool.js",
178
+ "default": "./extended/OptionBool.js"
179
+ },
175
180
  "./extended/Range": {
176
181
  "types": "./extended/Range.d.ts",
177
182
  "require": "./cjs/extended/Range.js",
package/packageInfo.js CHANGED
@@ -5,5 +5,5 @@ export const packageInfo = {
5
5
  name: '@polkadot/types-codec',
6
6
  path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto',
7
7
  type: 'esm',
8
- version: '8.5.2-1'
8
+ version: '8.5.2-4'
9
9
  };
@@ -2,6 +2,7 @@ interface SanitizeOptions {
2
2
  allowNamespaces?: boolean;
3
3
  }
4
4
  declare type Mapper = (value: string, options?: SanitizeOptions) => string;
5
+ export declare function trim(): Mapper;
5
6
  export declare function findClosing(value: string, start: number): number;
6
7
  export declare function alias(src: string, dest: string, withChecks?: boolean): Mapper;
7
8
  export declare function cleanupCompact(): Mapper;
package/utils/sanitize.js CHANGED
@@ -19,7 +19,12 @@ alias('RawAddress', 'Address'), // lookups, mapped to Address/AccountId as appro
19
19
  alias('Lookup::Source', 'LookupSource'), alias('Lookup::Target', 'LookupTarget'), // HACK duplication between contracts & primitives, however contracts prefixed with exec
20
20
  alias('exec::StorageKey', 'ContractStorageKey'), // flattens tuples with one value, `(AccountId)` -> `AccountId`
21
21
  flattenSingleTuple(), // converts ::Type to Type, <T as Trait<I>>::Proposal -> Proposal
22
- removeColons()]; // given a starting index, find the closing >
22
+ removeColons(), // remove all trailing spaces - this should always be the last
23
+ trim()]; // given a string, trim it
24
+
25
+ export function trim() {
26
+ return value => value.trim();
27
+ } // given a starting index, find the closing >
23
28
 
24
29
  export function findClosing(value, start) {
25
30
  let depth = 0;
@@ -191,10 +196,11 @@ export function removeWrap(check) {
191
196
  const sanitizeMap = new Map(); // eslint-disable-next-line @typescript-eslint/ban-types
192
197
 
193
198
  export function sanitize(value, options) {
194
- let result = value.toString();
199
+ const startValue = value.toString();
200
+ let result = startValue;
195
201
 
196
202
  if (!options) {
197
- const memoized = sanitizeMap.get(result);
203
+ const memoized = sanitizeMap.get(startValue);
198
204
 
199
205
  if (memoized) {
200
206
  return memoized;
@@ -205,10 +211,8 @@ export function sanitize(value, options) {
205
211
  result = mappings[i](result, options);
206
212
  }
207
213
 
208
- result = result.trim();
209
-
210
214
  if (!options) {
211
- sanitizeMap.set(value.toString(), result);
215
+ sanitizeMap.set(startValue, result);
212
216
  }
213
217
 
214
218
  return result;