Add Range and RangeInclusive types (#3791)

* Add Range and RangeInclusive types

* Lint

* Typo
This commit is contained in:
Caio
2021-08-13 08:13:14 +02:00
committed by GitHub
parent 0e22308954
commit f86ace2680
5 changed files with 160 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeRegistry } from '../create';
import { U32 } from '../primitive';
import { Range } from '.';
describe('Range', (): void => {
const registry = new TypeRegistry();
const Type = Range.with([U32, U32]);
let range: Range<U32>;
beforeEach((): void => {
range = new Range(
registry,
U32,
[new U32(registry, 1), new U32(registry, 2)]
);
});
it('decodes', (): void => {
const rslt = new Type(registry, new Uint8Array([1, 0, 0, 0, 2, 0, 0, 0]));
expect(rslt.toJSON()).toEqual([1, 2]);
});
it('encodes', (): void => {
expect(range.toU8a()).toEqual(new Uint8Array([1, 0, 0, 0, 2, 0, 0, 0]));
});
it('has a sane toRawType representation', (): void => {
expect(range.toRawType()).toEqual('Range<u32>');
});
});
+45
View File
@@ -0,0 +1,45 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, Constructor, InterfaceTypes, Registry } from '../types';
import { Tuple } from '../codec/Tuple';
/**
* @name Range
* @description
* Rust `Range<T>` representation
*/
export class Range<T extends Codec> extends Tuple {
constructor (registry: Registry, ty: Constructor<T> | keyof InterfaceTypes, value: [T, T]) {
super(
registry,
{
end: ty,
start: ty
},
value
);
}
/**
* @description Returns the starting range value
*/
public get start (): T {
return this[0] as T;
}
/**
* @description Returns the ending range value
*/
public get end (): T {
return this[1] as T;
}
/**
* @description Returns the base runtime type name for this instance
*/
public override toRawType (): string {
return `Range<${this.start.toRawType()}>`;
}
}
@@ -0,0 +1,34 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeRegistry } from '../create';
import { U32 } from '../primitive';
import { RangeInclusive } from '.';
describe('RangeInclusive', (): void => {
const registry = new TypeRegistry();
const Type = RangeInclusive.with([U32, U32]);
let range: RangeInclusive<U32>;
beforeEach((): void => {
range = new RangeInclusive(
registry,
U32,
[new U32(registry, 1), new U32(registry, 2)]
);
});
it('decodes', (): void => {
const rslt = new Type(registry, new Uint8Array([1, 0, 0, 0, 2, 0, 0, 0]));
expect(rslt.toJSON()).toEqual([1, 2]);
});
it('encodes', (): void => {
expect(range.toU8a()).toEqual(new Uint8Array([1, 0, 0, 0, 2, 0, 0, 0]));
});
it('has a sane toRawType representation', (): void => {
expect(range.toRawType()).toEqual('RangeInclusive<u32>');
});
});
@@ -0,0 +1,45 @@
// Copyright 2017-2021 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, Constructor, InterfaceTypes, Registry } from '../types';
import { Tuple } from '../codec/Tuple';
/**
* @name RangeInclusive
* @description
* Rust `RangeInclusive<T>` representation
*/
export class RangeInclusive<T extends Codec> extends Tuple {
constructor (registry: Registry, ty: Constructor<T> | keyof InterfaceTypes, value: [T, T]) {
super(
registry,
{
end: ty,
start: ty
},
value
);
}
/**
* @description Returns the starting range value
*/
public get start (): T {
return this[0] as T;
}
/**
* @description Returns the ending range value
*/
public get end (): T {
return this[1] as T;
}
/**
* @description Returns the base runtime type name for this instance
*/
public override toRawType (): string {
return `RangeInclusive<${this.start.toRawType()}>`;
}
}
+2
View File
@@ -29,3 +29,5 @@ export { U8aFixed } from './U8aFixed';
export { Vec } from './Vec';
// export { VecAny } from './VecAny';
export { VecFixed } from './VecFixed';
export { Range } from './Range';
export { RangeInclusive } from './RangeInclusive';