Expose map and filter on Tuple (#436)

This commit is contained in:
Amaury Martiny
2018-12-04 11:42:08 +01:00
committed by Jaco Greeff
parent ff96e8ee79
commit e2c029cc9e
2 changed files with 14 additions and 10 deletions
+4 -4
View File
@@ -69,12 +69,12 @@ describe('Tuple', () => {
expect(tuple.Types).toEqual(['Text', 'U32']);
});
it("doesn't expose filter", () => {
expect(tuple.filter).toThrowError(/is not implemented on Tuple/);
it('exposes filter', () => {
expect(tuple.filter((v) => v.toJSON() === 69)).toEqual([new U32(69)]);
});
it("doesn't expose map", () => {
expect(tuple.map).toThrowError(/is not implemented on Tuple/);
it('exposes map', () => {
expect(tuple.map(v => v.toString())).toEqual(['bazzing', '69']);
});
});
+10 -6
View File
@@ -94,7 +94,7 @@ export default class Tuple<
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
return this.toArray().map((entry) =>
return this.map((entry) =>
entry.toJSON()
);
}
@@ -113,7 +113,7 @@ export default class Tuple<
*/
toU8a (isBare?: boolean): Uint8Array {
return u8aConcat(
...this.toArray().map((entry) =>
...this.map((entry) =>
entry.toU8a(isBare)
)
);
@@ -124,16 +124,20 @@ export default class Tuple<
// new instance.
/**
* @ignore
* @description Filters the array with the callback
* @param callbackfn The filter function
* @param thisArg The `this` object to apply the result to
*/
filter (callbackfn: (value: Codec, index: number, array: Array<Codec>) => any, thisArg?: any): Array<Codec> {
throw new Error("Method 'filter' is not implemented on Tuple.");
return this.toArray().filter(callbackfn, thisArg);
}
/**
* @ignore
* @description Maps the array with the callback
* @param callbackfn The mapping function
* @param thisArg The `this` onject to apply the result to
*/
map<U> (callbackfn: (value: Codec, index: number, array: Array<Codec>) => U, thisArg?: any): Array<U> {
throw new Error("Method 'map' is not implemented on Tuple.");
return this.toArray().map(callbackfn, thisArg);
}
}