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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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-2'
15
15
  };
16
16
  exports.packageInfo = packageInfo;
@@ -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/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-2",
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-2'
9
9
  };