mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-12 22:41:27 +00:00
Add array methods: findIndex, reduce, some, every (#49)
This commit is contained in:
4
std/assembly.d.ts
vendored
4
std/assembly.d.ts
vendored
@ -273,12 +273,16 @@ declare class Array<T> {
|
||||
length: i32;
|
||||
/** Constructs a new array. */
|
||||
constructor(capacity?: i32);
|
||||
every(callbackfn: (element: T, index: i32, array?: Array<T>) => bool): bool;
|
||||
findIndex(predicate: (element: T, index: i32, array?: Array<T>) => bool): i32;
|
||||
includes(searchElement: T, fromIndex?: i32): bool;
|
||||
indexOf(searchElement: T, fromIndex?: i32): i32;
|
||||
lastIndexOf(searchElement: T, fromIndex?: i32): i32;
|
||||
push(element: T): void;
|
||||
pop(): T;
|
||||
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: i32, array: Array<T>) => U, initialValue: U): U;
|
||||
shift(): T;
|
||||
some(callbackfn: (element: T, index: i32, array?: Array<T>) => bool): bool;
|
||||
unshift(element: T): i32;
|
||||
slice(from: i32, to?: i32): T[];
|
||||
splice(start: i32, deleteCount?: i32): void;
|
||||
|
@ -25,6 +25,30 @@ export class Array<T> {
|
||||
this.__capacity = this.__length = capacity;
|
||||
}
|
||||
|
||||
every(callbackfn: (element: T, index: i32, array: Array<T>) => bool): bool {
|
||||
var toIndex: i32 = this.__length;
|
||||
var i: i32 = 0;
|
||||
while (i < toIndex && i < this.__length) {
|
||||
if (!callbackfn(load<T>(this.__memory + <usize>i * sizeof<T>()), i, this)) {
|
||||
return false;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
findIndex(predicate: (element: T, index: i32, array: Array<T>) => bool): i32 {
|
||||
var toIndex: i32 = this.__length;
|
||||
var i: i32 = 0;
|
||||
while (i < toIndex && i < this.__length) {
|
||||
if (predicate(load<T>(this.__memory + <usize>i * sizeof<T>()), i, this)) {
|
||||
return i;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
get length(): i32 {
|
||||
return this.__length;
|
||||
}
|
||||
@ -129,6 +153,20 @@ export class Array<T> {
|
||||
return load<T>(this.__memory + <usize>--this.__length * sizeof<T>());
|
||||
}
|
||||
|
||||
reduce<U>(
|
||||
callbackfn: (previousValue: U, currentValue: T, currentIndex: i32, array: Array<T>) => U,
|
||||
initialValue: U
|
||||
): U {
|
||||
var accumulator: U = initialValue;
|
||||
var toIndex: i32 = this.__length;
|
||||
var i: i32 = 0;
|
||||
while (i < toIndex && i < this.__length) {
|
||||
accumulator = callbackfn(accumulator, load<T>(this.__memory + <usize>i * sizeof<T>()), i, this);
|
||||
i += 1;
|
||||
}
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
shift(): T {
|
||||
if (this.__length < 1) {
|
||||
throw new RangeError("Array is empty"); // return changetype<T>(0) ?
|
||||
@ -148,6 +186,18 @@ export class Array<T> {
|
||||
return element;
|
||||
}
|
||||
|
||||
some(callbackfn: (element: T, index: i32, array: Array<T>) => bool): bool {
|
||||
var toIndex: i32 = this.__length;
|
||||
var i: i32 = 0;
|
||||
while (i < toIndex && i < this.__length) {
|
||||
if (callbackfn(load<T>(this.__memory + <usize>i * sizeof<T>()), i, this)) {
|
||||
return true;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
unshift(element: T): i32 {
|
||||
var oldCapacity = this.__capacity;
|
||||
if (this.__length == oldCapacity) {
|
||||
|
4
std/portable.d.ts
vendored
4
std/portable.d.ts
vendored
@ -218,12 +218,16 @@ declare class Array<T> {
|
||||
[key: number]: T;
|
||||
length: i32;
|
||||
constructor(capacity?: i32);
|
||||
every(callbackfn: (element: T, index: i32, array?: Array<T>) => bool): bool;
|
||||
findIndex(predicate: (element: T, index: i32, array?: Array<T>) => bool): i32;
|
||||
includes(searchElement: T, fromIndex?: i32): bool;
|
||||
indexOf(searchElement: T, fromIndex?: i32): i32;
|
||||
lastIndexOf(searchElement: T, fromIndex?: i32): i32;
|
||||
push(element: T): void;
|
||||
pop(): T;
|
||||
reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: i32, array: Array<T>) => U, initialValue: U): U;
|
||||
shift(): T;
|
||||
some(callbackfn: (element: T, index: i32, array?: Array<T>) => bool): bool;
|
||||
unshift(element: T): i32;
|
||||
slice(from: i32, to?: i32): T[];
|
||||
splice(start: i32, deleteCount?: i32): void;
|
||||
|
Reference in New Issue
Block a user