Use static get [Symbol.species] () for Array & Uint8Array types (#4964)

This commit is contained in:
Jaco
2022-06-20 13:33:24 +03:00
committed by GitHub
parent 13f7e853e8
commit 6d07803003
3 changed files with 22 additions and 68 deletions
+6 -1
View File
@@ -2,16 +2,21 @@
## master
Contributed:
- Bump `@substrate/connect` to 0.7.7 (Thanks to https://github.com/wirednkod)
Changes:
- Align `BeefyPayload` with Substrate master
- Use `static get [Symbol.species] ()` for `Array` & `Uint8Array` types
## 8.9.1 Jun 19, 2022
Contributed:
- Update `@substrate/connect` to latest (Thanks to https://github.com/tomaka)
- Update `@substrate/connect` to 0.7.6 (Thanks to https://github.com/tomaka)
Changes:
+8 -39
View File
@@ -20,6 +20,14 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
public createdAtHash?: IU8a;
/**
* @description This ensures that operators such as clice, filter, map, etc. return
* new Array instances (without this we need to apply overrides)
*/
static get [Symbol.species] (): typeof Array {
return Array;
}
protected constructor (registry: Registry, length: number) {
super(length);
@@ -169,43 +177,4 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
return encoded;
}
// Below are methods that we override. When we do a `new Vec(...).map()`,
// we want it to return an Array. We only override the methods that return a
// new instance.
/**
* @description Concatenates two arrays
*/
public override concat (other: T[]): T[] {
return this.toArray().concat(other instanceof AbstractArray ? other.toArray() : other);
}
/**
* @description Filters the array with the callback
*/
public override filter (callbackfn: (value: T, index: number, array: T[]) => boolean, thisArg?: unknown): T[] {
return this.toArray().filter(callbackfn, thisArg);
}
/**
* @description Maps the array with the callback
*/
public override map<U> (callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: unknown): U[] {
return this.toArray().map(callbackfn, thisArg);
}
/**
* @description Checks if the array includes a specific value
*/
public override includes (check: unknown): boolean {
return this.some((value: T) => value.eq(check));
}
/**
* @description Returns a slice of an array
*/
public override slice (start?: number, end?: number): T[] {
return this.toArray().slice(start, end);
}
}
+8 -28
View File
@@ -22,6 +22,14 @@ export class Raw extends Uint8Array implements IU8a {
public readonly registry: Registry;
/**
* @description This ensures that operators such as clice, filter, map, etc. return
* new Array instances (without this we need to apply overrides)
*/
static get [Symbol.species] (): typeof Uint8Array {
return Uint8Array;
}
constructor (registry: Registry, value?: AnyU8a, initialU8aLength?: number) {
super(u8aToU8a(value));
@@ -64,14 +72,6 @@ export class Raw extends Uint8Array implements IU8a {
return isUtf8(this);
}
/**
* @description The length of the value
*/
public override get length (): number {
// only included here since we ignore inherited docs
return super.length;
}
/**
* @description Returns the number of bits in the value
*/
@@ -100,26 +100,6 @@ export class Raw extends Uint8Array implements IU8a {
};
}
/**
* @description Create a new slice from the actual buffer. (compat)
* @param start The position to start at
* @param end The position to end at
*/
public override slice (start?: number, end?: number): Uint8Array {
// Like subarray below, we have to follow this approach since we are extending the TypeArray.
// This happens especially when it comes to further extensions, the length may be an override
return Uint8Array.from(this).slice(start, end);
}
/**
* @description Create a new subarray from the actual buffer. (compat)
* @param begin The position to start at
* @param end The position to end at
*/
public override subarray (begin?: number, end?: number): Uint8Array {
return Uint8Array.from(this).subarray(begin, end);
}
/**
* @description Returns a hex string representation of the value
*/