1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

js: add more tests for array, support array insert_many, minor fixes for references (#10983)

This commit is contained in:
playX
2021-07-28 13:01:00 +03:00
committed by GitHub
parent 66bc8bc0cb
commit e3cf95b058
7 changed files with 342 additions and 10 deletions

View File

@ -4,6 +4,7 @@ struct array {
arr JS.Array
pub:
len int
cap int
}
#function flatIntoArray(target, source, sourceLength, targetIndex, depth) {
@ -50,6 +51,14 @@ pub fn (a array) repeat_to_depth(count int, depth int) array {
return arr
}
// last returns the last element of the array.
pub fn (a array) last() voidptr {
mut res := voidptr(0)
#res = a.arr[a.len-1];
return res
}
fn (a array) get(ix int) voidptr {
mut result := voidptr(0)
#result = a.arr[ix]
@ -104,6 +113,10 @@ pub fn (mut a array) insert(i int, val voidptr) {
#a.arr.splice(i,0,val)
}
pub fn (mut a array) insert_many(i int, val voidptr, size int) {
#a.arr.splice(i,0,...val.slice(0,+size))
}
pub fn (mut a array) join(separator string) string {
mut res := ''
#res = new builtin.string(a.arr.join(separator +''));
@ -115,7 +128,24 @@ fn (a array) push(val voidptr) {
#a.arr.push(val)
}
pub fn (a array) str() string {
mut res := ''
#res = new builtin.string(a + '')
return res
}
#array.prototype[Symbol.iterator] = function () { return this.arr[Symbol.iterator](); }
#array.prototype.entries = function () { return this.arr.entries(); }
#array.prototype.map = function(callback) { return this.arr.map(callback); }
#array.prototype.filter = function(callback) { return this.arr.filter(callback); }
#Object.defineProperty(array.prototype,'cap',{ get: function () { return this.len; } })
// delete deletes array element at index `i`.
pub fn (mut a array) delete(i int) {
a.delete_many(i, 1)
}
// delete_many deletes `size` elements beginning with index `i`
pub fn (mut a array) delete_many(i int, size int) {
#a.arr.splice(i.valueOf(),size.valueOf())
}