Move .isEmpty to $isEmpty (#5424)
* Move .isEmpty to $isEmpty * Adjust usages
This commit is contained in:
@@ -16,7 +16,7 @@ import { compareArray, warnGet } from '../utils';
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export abstract class AbstractArray<T extends Codec> extends Array<T> implements IVec<T> {
|
||||
public readonly registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
@@ -46,6 +46,11 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
@@ -71,7 +76,7 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
public get $isEmpty (): boolean {
|
||||
return this.length === 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import { warnGet } from '../utils';
|
||||
* @description A type extends the Base class, when it holds a value
|
||||
*/
|
||||
export abstract class AbstractBase<T extends Codec> implements Codec {
|
||||
public readonly registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
@@ -35,6 +35,11 @@ export abstract class AbstractBase<T extends Codec> implements Codec {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
@@ -56,8 +61,8 @@ export abstract class AbstractBase<T extends Codec> implements Codec {
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
return this.#raw.isEmpty;
|
||||
public get $isEmpty (): boolean {
|
||||
return this.#raw.$isEmpty;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -74,13 +74,13 @@ function decodeAbstractInt (value: Exclude<AnyNumber, Uint8Array> | Record<strin
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export abstract class AbstractInt extends BN implements INumber {
|
||||
public readonly registry: Registry;
|
||||
public readonly encodedLength: number;
|
||||
public readonly isUnsigned: boolean;
|
||||
readonly registry: Registry;
|
||||
readonly encodedLength: number;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
public $isStorageFallback?: boolean;
|
||||
readonly $isUnsigned: boolean;
|
||||
|
||||
readonly #bitLength: UIntBitLength;
|
||||
|
||||
@@ -99,9 +99,8 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
|
||||
this.registry = registry;
|
||||
this.#bitLength = bitLength;
|
||||
this.encodedLength = this.#bitLength / 8;
|
||||
this.$initialU8aLength = this.#bitLength / 8;
|
||||
this.isUnsigned = !isSigned;
|
||||
this.encodedLength = this.$initialU8aLength = this.#bitLength / 8;
|
||||
this.$isUnsigned = !isSigned;
|
||||
|
||||
const isNegative = this.isNeg();
|
||||
const maxBits = bitLength - (isSigned && !isNegative ? 1 : 0);
|
||||
@@ -123,6 +122,16 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isUnsigned instead. This getter will be removed in a future version */
|
||||
public get isUnsigned (): boolean {
|
||||
return warnGet(this, 'isUnsigned');
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
@@ -133,7 +142,7 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
/**
|
||||
* @description Checks if the value is a zero value (align elsewhere)
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
public get $isEmpty (): boolean {
|
||||
return this.isZero();
|
||||
}
|
||||
|
||||
@@ -153,7 +162,7 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
// number and BN inputs (no `.eqn` needed) - numbers will be converted
|
||||
return super.eq(
|
||||
isHex(other)
|
||||
? hexToBn(other.toString(), { isLe: false, isNegative: !this.isUnsigned })
|
||||
? hexToBn(other.toString(), { isLe: false, isNegative: !this.$isUnsigned })
|
||||
: bnToBn(other as string)
|
||||
);
|
||||
}
|
||||
@@ -198,7 +207,7 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
return bnToHex(this, {
|
||||
bitLength: this.bitLength(),
|
||||
isLe,
|
||||
isNegative: !this.isUnsigned
|
||||
isNegative: !this.$isUnsigned
|
||||
});
|
||||
}
|
||||
|
||||
@@ -255,7 +264,7 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
// underlying it always matches (no matter which length it actually is)
|
||||
return this instanceof this.registry.createClassUnsafe('Balance')
|
||||
? 'Balance'
|
||||
: `${this.isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
|
||||
: `${this.$isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -276,7 +285,7 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
return bnToU8a(this, {
|
||||
bitLength: this.bitLength(),
|
||||
isLe: true,
|
||||
isNegative: !this.isUnsigned
|
||||
isNegative: !this.$isUnsigned
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ function decodeCompact<T extends INumber> (registry: Registry, Type: CodecClass<
|
||||
* a number and making the compact representation thereof
|
||||
*/
|
||||
export class Compact<T extends INumber> implements ICompact<T> {
|
||||
public readonly registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
@@ -76,6 +76,11 @@ export class Compact<T extends INumber> implements ICompact<T> {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
public static with<O extends INumber> (Type: CodecClass<O> | string): CodecClass<Compact<O>> {
|
||||
let definition: CodecClass<O> | undefined;
|
||||
|
||||
@@ -107,8 +112,8 @@ export class Compact<T extends INumber> implements ICompact<T> {
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
return this.#raw.isEmpty;
|
||||
public get $isEmpty (): boolean {
|
||||
return this.#raw.$isEmpty;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,7 +12,7 @@ import { warnGet } from '../utils';
|
||||
* An unknown type that fails on construction with the type info
|
||||
*/
|
||||
export class DoNotConstruct implements Codec {
|
||||
public readonly registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $isStorageFallback?: boolean;
|
||||
@@ -36,6 +36,11 @@ export class DoNotConstruct implements Codec {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
public static with (typeName?: string): CodecClass {
|
||||
return class extends DoNotConstruct {
|
||||
constructor (registry: Registry) {
|
||||
@@ -61,7 +66,7 @@ export class DoNotConstruct implements Codec {
|
||||
/**
|
||||
* @description Checks if the value is an empty value (always true)
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
public get $isEmpty (): boolean {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
|
||||
@@ -293,7 +293,7 @@ describe('Enum', (): void => {
|
||||
it('has isNone set, with correct index (i.e. no values are used)', (): void => {
|
||||
const test = new Enum(registry, ['foo', 'bar'], 1);
|
||||
|
||||
expect(test.isNone).toBe(true);
|
||||
expect(test.$isNone).toBe(true);
|
||||
expect(test.index).toEqual(1);
|
||||
});
|
||||
|
||||
|
||||
@@ -183,16 +183,16 @@ function decodeEnum (registry: Registry, def: TypesDef, value?: unknown, index?:
|
||||
* an extension to enum where the value type is determined by the actual index.
|
||||
*/
|
||||
export class Enum implements IEnum {
|
||||
public readonly registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
readonly $isBasic: boolean;
|
||||
public $isStorageFallback?: boolean;
|
||||
|
||||
readonly #def: TypesDef;
|
||||
readonly #entryIndex: number;
|
||||
readonly #indexes: number[];
|
||||
readonly #isBasic: boolean;
|
||||
readonly #isIndexed: boolean;
|
||||
readonly #raw: Codec;
|
||||
|
||||
@@ -204,9 +204,10 @@ export class Enum implements IEnum {
|
||||
? createFromU8a(registry, def, value[0], value.subarray(1))
|
||||
: decodeEnum(registry, def, value, index);
|
||||
|
||||
this.$isBasic = isBasic;
|
||||
|
||||
this.registry = registry;
|
||||
this.#def = def;
|
||||
this.#isBasic = isBasic;
|
||||
this.#isIndexed = isIndexed;
|
||||
this.#indexes = Object.values(def).map(({ index }) => index);
|
||||
this.#entryIndex = this.#indexes.indexOf(decoded.index);
|
||||
@@ -227,6 +228,21 @@ export class Enum implements IEnum {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isBasic instead. This getter will be removed in a future version */
|
||||
public get isBasic (): boolean {
|
||||
return warnGet(this, 'isBasic');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isNone instead. This getter will be removed in a future version */
|
||||
public get isNone (): boolean {
|
||||
return warnGet(this, 'isNone');
|
||||
}
|
||||
|
||||
public static with (Types: Record<string, string | CodecClass> | Record<string, number> | string[]): EnumCodecClass<Enum> {
|
||||
let definition: Definition | undefined;
|
||||
|
||||
@@ -296,24 +312,17 @@ export class Enum implements IEnum {
|
||||
return this.#raw;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description true if this is a basic enum (no values)
|
||||
*/
|
||||
public get isBasic (): boolean {
|
||||
return this.#isBasic;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
return this.#raw.isEmpty;
|
||||
public get $isEmpty (): boolean {
|
||||
return this.#raw.$isEmpty;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Checks if the Enum points to a [[Null]] type
|
||||
*/
|
||||
public get isNone (): boolean {
|
||||
public get $isNone (): boolean {
|
||||
return this.#raw instanceof Null;
|
||||
}
|
||||
|
||||
@@ -354,7 +363,7 @@ export class Enum implements IEnum {
|
||||
return !this.toU8a().some((entry, index) => entry !== other[index]);
|
||||
} else if (isNumber(other)) {
|
||||
return this.toNumber() === other;
|
||||
} else if (this.#isBasic && isString(other)) {
|
||||
} else if (this.$isBasic && isString(other)) {
|
||||
return this.type === other;
|
||||
} else if (isHex(other)) {
|
||||
return this.toHex() === other;
|
||||
@@ -372,7 +381,7 @@ export class Enum implements IEnum {
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspect (): Inspect {
|
||||
if (this.#isBasic) {
|
||||
if (this.$isBasic) {
|
||||
return { outer: [new Uint8Array([this.index])] };
|
||||
}
|
||||
|
||||
@@ -395,7 +404,7 @@ export class Enum implements IEnum {
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
*/
|
||||
public toHuman (isExtended?: boolean): AnyJson {
|
||||
return this.#isBasic || this.isNone
|
||||
return this.$isBasic || this.$isNone
|
||||
? this.type
|
||||
: { [this.type]: this.#raw.toHuman(isExtended) };
|
||||
}
|
||||
@@ -404,7 +413,7 @@ export class Enum implements IEnum {
|
||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||
*/
|
||||
public toJSON (): AnyJson {
|
||||
return this.#isBasic
|
||||
return this.$isBasic
|
||||
? this.type
|
||||
: { [stringCamelCase(this.type)]: this.#raw.toJSON() };
|
||||
}
|
||||
@@ -420,7 +429,7 @@ export class Enum implements IEnum {
|
||||
* @description Converts the value in a best-fit primitive form
|
||||
*/
|
||||
public toPrimitive (): AnyJson {
|
||||
return this.#isBasic
|
||||
return this.$isBasic
|
||||
? this.type
|
||||
: { [stringCamelCase(this.type)]: this.#raw.toPrimitive() };
|
||||
}
|
||||
@@ -429,7 +438,7 @@ export class Enum implements IEnum {
|
||||
* @description Returns a raw struct representation of the enum types
|
||||
*/
|
||||
protected _toRawStruct (): string[] | Record<string, string | number> {
|
||||
if (this.#isBasic) {
|
||||
if (this.$isBasic) {
|
||||
return this.#isIndexed
|
||||
? this.defKeys.reduce((out: Record<string, number>, key, index): Record<string, number> => {
|
||||
out[key] = this.#indexes[index];
|
||||
@@ -460,7 +469,7 @@ export class Enum implements IEnum {
|
||||
* @description Returns the string representation of the value
|
||||
*/
|
||||
public toString (): string {
|
||||
return this.isNone
|
||||
return this.$isNone
|
||||
? this.type
|
||||
: stringify(this.toJSON());
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ describe('Null', (): void => {
|
||||
});
|
||||
|
||||
it('isEmpty', (): void => {
|
||||
expect(new Null(registry).isEmpty).toBe(true);
|
||||
expect(new Null(registry).$isEmpty).toBe(true);
|
||||
});
|
||||
|
||||
it('has an empty hex', (): void => {
|
||||
|
||||
@@ -14,13 +14,13 @@ import { warnGet } from '../utils';
|
||||
* Implements a type that does not contain anything (apart from `null`)
|
||||
*/
|
||||
export class Null implements Codec {
|
||||
public readonly encodedLength = 0;
|
||||
public readonly isEmpty = true;
|
||||
public readonly registry: Registry;
|
||||
readonly encodedLength = 0;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength = 0;
|
||||
public $isStorageFallback?: boolean;
|
||||
readonly $isEmpty = true;
|
||||
|
||||
constructor (registry: Registry) {
|
||||
this.registry = registry;
|
||||
@@ -36,6 +36,11 @@ export class Null implements Codec {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
|
||||
@@ -37,7 +37,7 @@ describe('Option', (): void => {
|
||||
].forEach((test): void => {
|
||||
expect(test.isSome).toBe(true);
|
||||
expect(test.isNone).toBe(false);
|
||||
expect(test.isEmpty).toBe(false);
|
||||
expect(test.$isEmpty).toBe(false);
|
||||
expect(test.toU8a()).toEqual(new Uint8Array([1]));
|
||||
expect(test.unwrap().toHex()).toEqual('0x');
|
||||
});
|
||||
|
||||
@@ -66,7 +66,7 @@ function decodeOption (registry: Registry, Type: CodecClass, value?: unknown): C
|
||||
* with a value if/as required/found.
|
||||
*/
|
||||
export class Option<T extends Codec> implements IOption<T> {
|
||||
public readonly registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
@@ -102,6 +102,11 @@ export class Option<T extends Codec> implements IOption<T> {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
public static with<O extends Codec> (Type: CodecClass<O> | string): CodecClass<Option<O>> {
|
||||
let definition: CodecClass<O> | undefined;
|
||||
|
||||
@@ -136,7 +141,7 @@ export class Option<T extends Codec> implements IOption<T> {
|
||||
/**
|
||||
* @description Checks if the Option has no value
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
public get $isEmpty (): boolean {
|
||||
return this.isNone;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ export class Result<O extends Codec, E extends Codec> extends Enum implements IR
|
||||
* @description Checks if the Result has no value
|
||||
*/
|
||||
public override get isEmpty (): boolean {
|
||||
return this.isOk && this.value.isEmpty;
|
||||
return this.isOk && this.value.$isEmpty;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -70,7 +70,7 @@ function decodeSet<V extends Codec> (registry: Registry, valType: CodecClass<V>
|
||||
}
|
||||
|
||||
export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V> {
|
||||
public readonly registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
@@ -98,6 +98,11 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
public static with<V extends Codec> (valType: CodecClass<V> | string): CodecClass<BTreeSet<V>> {
|
||||
return class extends BTreeSet<V> {
|
||||
constructor (registry: Registry, value?: Uint8Array | string | Set<any>) {
|
||||
@@ -129,7 +134,7 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
public get $isEmpty (): boolean {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@ export class Linkage<T extends Codec> extends Struct {
|
||||
public override toU8a (): Uint8Array {
|
||||
// As part of a storage query (where these appear), in the case of empty, the values
|
||||
// are NOT populated by the node - follow the same logic, leaving it empty
|
||||
return this.isEmpty
|
||||
return this.$isEmpty
|
||||
? EMPTY
|
||||
: super.toU8a();
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ function decodeMap<K extends Codec, V extends Codec> (registry: Registry, keyTyp
|
||||
}
|
||||
|
||||
export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends Map<K, V> implements IMap<K, V> {
|
||||
public readonly registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
@@ -126,6 +126,11 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
@@ -149,7 +154,7 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
public get $isEmpty (): boolean {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -58,9 +58,9 @@ describe('Bool', (): void => {
|
||||
});
|
||||
|
||||
it('has sane isEmpty aligning with the rest', (): void => {
|
||||
expect(new Bool(registry).isEmpty).toBe(true);
|
||||
expect(new Bool(registry, false).isEmpty).toBe(true);
|
||||
expect(new Bool(registry, true).isEmpty).toBe(false);
|
||||
expect(new Bool(registry).$isEmpty).toBe(true);
|
||||
expect(new Bool(registry, false).$isEmpty).toBe(true);
|
||||
expect(new Bool(registry, true).$isEmpty).toBe(false);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
|
||||
@@ -15,7 +15,7 @@ import { warnGet } from '../utils';
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export class bool extends Boolean implements Codec {
|
||||
public readonly registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength = 1;
|
||||
@@ -43,6 +43,11 @@ export class bool extends Boolean implements Codec {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
@@ -60,7 +65,7 @@ export class bool extends Boolean implements Codec {
|
||||
/**
|
||||
* @description Checks if the value is an empty value (true when it wraps false/default)
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
public get $isEmpty (): boolean {
|
||||
return this.isFalse;
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ function decodeDate (value: CodecDate | Date | AnyNumber): Date {
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export class CodecDate extends Date implements INumber {
|
||||
public readonly registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength = BITLENGTH / 8;
|
||||
@@ -58,6 +58,11 @@ export class CodecDate extends Date implements INumber {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
@@ -75,7 +80,7 @@ export class CodecDate extends Date implements INumber {
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
public get $isEmpty (): boolean {
|
||||
return this.getTime() === 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ describe('Float', (): void => {
|
||||
|
||||
it('triggers isEmpty on 0', (): void => {
|
||||
expect(
|
||||
new F32(registry, 0).isEmpty
|
||||
new F32(registry, 0).$isEmpty
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
|
||||
@@ -21,8 +21,8 @@ interface Options {
|
||||
* in some eth_* RPCs
|
||||
*/
|
||||
export class Float extends Number implements IFloat {
|
||||
public readonly encodedLength: number;
|
||||
public readonly registry: Registry;
|
||||
readonly encodedLength: number;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
@@ -55,6 +55,11 @@ export class Float extends Number implements IFloat {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
public static with (bitLength: 32 | 64): CodecClass<Float> {
|
||||
return class extends Float {
|
||||
constructor (registry: Registry, value?: AnyFloat) {
|
||||
@@ -73,7 +78,7 @@ export class Float extends Number implements IFloat {
|
||||
/**
|
||||
* @description Returns true if the type wraps an empty/default all-0 value
|
||||
*/
|
||||
get isEmpty (): boolean {
|
||||
public get $isEmpty (): boolean {
|
||||
return this.valueOf() === 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ function decodeJson (value?: Record<string, unknown> | null): [string, any][] {
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export class Json extends Map<string, any> implements Codec {
|
||||
public readonly registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
@@ -48,6 +48,11 @@ export class Json extends Map<string, any> implements Codec {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Always 0, never encodes as a Uint8Array
|
||||
*/
|
||||
@@ -65,7 +70,7 @@ export class Json extends Map<string, any> implements Codec {
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
public get $isEmpty (): boolean {
|
||||
return [...this.keys()].length === 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ import { warnGet } from '../utils';
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export class Raw extends Uint8Array implements IU8a {
|
||||
public readonly registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
@@ -49,6 +49,11 @@ export class Raw extends Uint8Array implements IU8a {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
@@ -73,7 +78,7 @@ export class Raw extends Uint8Array implements IU8a {
|
||||
/**
|
||||
* @description Returns true if the type wraps an empty/default all-0 value
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
public get $isEmpty (): boolean {
|
||||
return !this.length || isUndefined(this.find((b) => !!b));
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ describe('Set', (): void => {
|
||||
it('constructs via an string[]', (): void => {
|
||||
const set = new CodecSet(registry, SET_ROLES, ['full', 'authority']);
|
||||
|
||||
expect(set.isEmpty).toEqual(false);
|
||||
expect(set.$isEmpty).toEqual(false);
|
||||
expect(set.toString()).toEqual(
|
||||
'[full, authority]'
|
||||
);
|
||||
|
||||
@@ -92,7 +92,7 @@ function decodeSet (setValues: SetValues, value: string[] | Set<string> | Uint8A
|
||||
* a bitwise representation of the values.
|
||||
*/
|
||||
export class CodecSet extends Set<string> implements ISet<string> {
|
||||
public readonly registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
@@ -119,6 +119,11 @@ export class CodecSet extends Set<string> implements ISet<string> {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
public static with (values: SetValues, bitLength?: number): CodecClass<CodecSet> {
|
||||
return class extends CodecSet {
|
||||
static {
|
||||
@@ -157,7 +162,7 @@ export class CodecSet extends Set<string> implements ISet<string> {
|
||||
/**
|
||||
* @description true is the Set contains no values
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
public get $isEmpty (): boolean {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -103,7 +103,7 @@ export class Struct<
|
||||
V extends { [K in keyof S]: any } = { [K in keyof S]: any },
|
||||
// type names, mapped by key, name of Class in S
|
||||
E extends { [K in keyof S]: string } = { [K in keyof S]: string }> extends Map<keyof S, Codec> implements IStruct<keyof S> {
|
||||
public readonly registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
@@ -138,6 +138,11 @@ export class Struct<
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
public static with<S extends TypesDef> (Types: S, jsonMap?: Map<string, string>): CodecClass<Struct<S>> {
|
||||
let definition: Definition | undefined;
|
||||
|
||||
@@ -170,9 +175,9 @@ export class Struct<
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
public get $isEmpty (): boolean {
|
||||
for (const v of this.values()) {
|
||||
if (!v.isEmpty) {
|
||||
if (!v.$isEmpty) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ function decodeText (value?: null | AnyString | AnyU8a | { toString: () => strin
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export class Text extends String implements IText {
|
||||
public readonly registry: Registry;
|
||||
readonly registry: Registry;
|
||||
|
||||
public $createdAtHash?: IU8a;
|
||||
public $initialU8aLength?: number;
|
||||
@@ -77,6 +77,11 @@ export class Text extends String implements IText {
|
||||
return warnGet(this, 'initialU8aLength');
|
||||
}
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
|
||||
public get isEmpty (): boolean {
|
||||
return warnGet(this, 'isEmpty');
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
@@ -94,7 +99,7 @@ export class Text extends String implements IText {
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
public get isEmpty (): boolean {
|
||||
public get $isEmpty (): boolean {
|
||||
return this.length === 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,5 +10,5 @@ import { Float } from '../native/Float';
|
||||
*/
|
||||
export class f32 extends Float.with(32) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __FloatType = 'f32';
|
||||
readonly __FloatType = 'f32';
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ import { Float } from '../native/Float';
|
||||
*/
|
||||
export class f64 extends Float.with(64) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __FloatType = 'f64';
|
||||
readonly __FloatType = 'f64';
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ import { Int } from '../base/Int';
|
||||
*/
|
||||
export class i128 extends Int.with(128) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __IntType = 'i128';
|
||||
readonly __IntType = 'i128';
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ import { Int } from '../base/Int';
|
||||
*/
|
||||
export class i16 extends Int.with(16) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __IntType = 'i16';
|
||||
readonly __IntType = 'i16';
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ import { Int } from '../base/Int';
|
||||
*/
|
||||
export class i256 extends Int.with(256) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __IntType = 'i256';
|
||||
readonly __IntType = 'i256';
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ import { Int } from '../base/Int';
|
||||
*/
|
||||
export class i32 extends Int.with(32) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __IntType = 'i32';
|
||||
readonly __IntType = 'i32';
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ import { Int } from '../base/Int';
|
||||
*/
|
||||
export class i64 extends Int.with(64) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __IntType = 'i64';
|
||||
readonly __IntType = 'i64';
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ import { Int } from '../base/Int';
|
||||
*/
|
||||
export class i8 extends Int.with(8) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __IntType = 'i8';
|
||||
readonly __IntType = 'i8';
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ import { UInt } from '../base/UInt';
|
||||
*/
|
||||
export class u128 extends UInt.with(128) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __UIntType = 'u128';
|
||||
readonly __UIntType = 'u128';
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ import { UInt } from '../base/UInt';
|
||||
*/
|
||||
export class u16 extends UInt.with(16) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __UIntType = 'u16';
|
||||
readonly __UIntType = 'u16';
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ import { UInt } from '../base/UInt';
|
||||
*/
|
||||
export class u256 extends UInt.with(256) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __UIntType = 'u256';
|
||||
readonly __UIntType = 'u256';
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ import { UInt } from '../base/UInt';
|
||||
*/
|
||||
export class u32 extends UInt.with(32) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __UIntType = 'u32';
|
||||
readonly __UIntType = 'u32';
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ import { UInt } from '../base/UInt';
|
||||
*/
|
||||
export class u64 extends UInt.with(64) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __UIntType = 'u64';
|
||||
readonly __UIntType = 'u64';
|
||||
}
|
||||
|
||||
@@ -10,5 +10,5 @@ import { UInt } from '../base/UInt';
|
||||
*/
|
||||
export class u8 extends UInt.with(8) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __UIntType = 'u8';
|
||||
readonly __UIntType = 'u8';
|
||||
}
|
||||
|
||||
@@ -20,6 +20,9 @@ interface CodecDeprecated {
|
||||
|
||||
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
|
||||
readonly initialU8aLength: number | undefined;
|
||||
|
||||
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version. */
|
||||
readonly isEmpty: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,6 +48,12 @@ export interface Codec extends CodecDeprecated {
|
||||
*/
|
||||
$initialU8aLength?: number;
|
||||
|
||||
/**
|
||||
* @description
|
||||
* Checks if the value is an empty value, with the internal bytes all-zero
|
||||
*/
|
||||
$isEmpty: boolean;
|
||||
|
||||
/**
|
||||
* @description
|
||||
* (internal usage) Indicates that the value was created via a fallback. This
|
||||
@@ -67,11 +76,6 @@ export interface Codec extends CodecDeprecated {
|
||||
*/
|
||||
readonly hash: IU8a;
|
||||
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
readonly isEmpty: boolean;
|
||||
|
||||
/**
|
||||
* @description The registry associated with this object
|
||||
*/
|
||||
|
||||
@@ -14,10 +14,11 @@ export interface ICompact<T extends INumber = INumber> extends Codec {
|
||||
}
|
||||
|
||||
export interface IEnum extends Codec {
|
||||
readonly $isBasic: boolean;
|
||||
|
||||
readonly defIndexes: number[];
|
||||
readonly defKeys: string[];
|
||||
readonly index: number;
|
||||
readonly isBasic: boolean;
|
||||
readonly type: string;
|
||||
readonly value: Codec;
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
const hasWarned: Record<string, boolean> = {};
|
||||
|
||||
/** @description Warns (once, per class) on the use of a deprecated interface */
|
||||
export function warnGet <T> (obj: Record<string, any>, name: 'createdAtHash' | 'initialU8aLength'): T {
|
||||
export function warnGet <T> (obj: Record<string, any>, name: 'createdAtHash' | 'initialU8aLength' | 'isBasic' | 'isEmpty' | 'isNone' | 'isUnsigned'): T {
|
||||
const clazz = `${obj.constructor?.name || 'Codec'}`;
|
||||
const check = `${clazz}.${name}`;
|
||||
const pass = `$${name}`;
|
||||
|
||||
Reference in New Issue
Block a user