@novasamatech/scale 0.6.3 → 0.6.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.
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/optionBool.d.ts +4 -0
- package/dist/optionBool.js +21 -0
- package/dist/optionBool.spec.d.ts +1 -0
- package/dist/optionBool.spec.js +15 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export type { HexString } from './hex.js';
|
|
|
3
3
|
export { Hex } from './hex.js';
|
|
4
4
|
export { Nullable } from './nullable.js';
|
|
5
5
|
export { Record } from './record.js';
|
|
6
|
+
export { OptionBool } from './optionBool.js';
|
|
6
7
|
export { Status } from './status.js';
|
|
7
8
|
export type { EnumCodec } from './enum.js';
|
|
8
9
|
export { Enum } from './enum.js';
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@ export { lazy } from './lazy.js';
|
|
|
2
2
|
export { Hex } from './hex.js';
|
|
3
3
|
export { Nullable } from './nullable.js';
|
|
4
4
|
export { Record } from './record.js';
|
|
5
|
+
export { OptionBool } from './optionBool.js';
|
|
5
6
|
export { Status } from './status.js';
|
|
6
7
|
export { Enum } from './enum.js';
|
|
7
8
|
export { Err } from './err.js';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { enhanceCodec, u8 } from 'scale-ts';
|
|
2
|
+
/**
|
|
3
|
+
* Optimized version of `Option(bool)`
|
|
4
|
+
*/
|
|
5
|
+
export const OptionBool = enhanceCodec(u8, value => {
|
|
6
|
+
if (value === undefined) {
|
|
7
|
+
return 0;
|
|
8
|
+
}
|
|
9
|
+
return value ? 2 : 1;
|
|
10
|
+
}, v => {
|
|
11
|
+
switch (v) {
|
|
12
|
+
case 0:
|
|
13
|
+
return undefined;
|
|
14
|
+
case 1:
|
|
15
|
+
return false;
|
|
16
|
+
case 2:
|
|
17
|
+
return true;
|
|
18
|
+
default:
|
|
19
|
+
throw new Error(`Unknown value for optionBool: ${v}. Should be ether 0, 1 or 2.`);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { OptionBool } from './optionBool.js';
|
|
3
|
+
describe('OptionBool', () => {
|
|
4
|
+
it('should correctly encode/decode OptionBool', () => {
|
|
5
|
+
expect(OptionBool.enc(undefined)).toEqual(new Uint8Array([0]));
|
|
6
|
+
expect(OptionBool.enc(false)).toEqual(new Uint8Array([1]));
|
|
7
|
+
expect(OptionBool.enc(true)).toEqual(new Uint8Array([2]));
|
|
8
|
+
expect(OptionBool.dec(new Uint8Array([0]))).toEqual(undefined);
|
|
9
|
+
expect(OptionBool.dec(new Uint8Array([1]))).toEqual(false);
|
|
10
|
+
expect(OptionBool.dec(new Uint8Array([2]))).toEqual(true);
|
|
11
|
+
});
|
|
12
|
+
it('should throw in bytes has incorrect value', () => {
|
|
13
|
+
expect(() => OptionBool.dec(new Uint8Array([3]))).toThrowErrorMatchingInlineSnapshot(`[Error: Unknown value for optionBool: 3. Should be ether 0, 1 or 2.]`);
|
|
14
|
+
});
|
|
15
|
+
});
|