diff --git a/packages/types/src/codec/Range.spec.ts b/packages/types/src/codec/Range.spec.ts new file mode 100644 index 0000000000..fff10ad849 --- /dev/null +++ b/packages/types/src/codec/Range.spec.ts @@ -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; + + 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'); + }); +}); diff --git a/packages/types/src/codec/Range.ts b/packages/types/src/codec/Range.ts new file mode 100644 index 0000000000..4e13c38543 --- /dev/null +++ b/packages/types/src/codec/Range.ts @@ -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` representation + */ +export class Range extends Tuple { + constructor (registry: Registry, ty: Constructor | 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()}>`; + } +} diff --git a/packages/types/src/codec/RangeInclusive.spec.ts b/packages/types/src/codec/RangeInclusive.spec.ts new file mode 100644 index 0000000000..1fc7a94580 --- /dev/null +++ b/packages/types/src/codec/RangeInclusive.spec.ts @@ -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; + + 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'); + }); +}); diff --git a/packages/types/src/codec/RangeInclusive.ts b/packages/types/src/codec/RangeInclusive.ts new file mode 100644 index 0000000000..aa0909003e --- /dev/null +++ b/packages/types/src/codec/RangeInclusive.ts @@ -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` representation + */ +export class RangeInclusive extends Tuple { + constructor (registry: Registry, ty: Constructor | 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()}>`; + } +} diff --git a/packages/types/src/codec/index.ts b/packages/types/src/codec/index.ts index 6bdab0f57b..abf8264218 100644 --- a/packages/types/src/codec/index.ts +++ b/packages/types/src/codec/index.ts @@ -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';