2019-03-27 11:27:59 +03:00
|
|
|
is_enumerable := func(x) {
|
|
|
|
return is_array(x) || is_map(x) || is_immutable_array(x) || is_immutable_map(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
is_array_like := func(x) {
|
|
|
|
return is_array(x) || is_immutable_array(x)
|
|
|
|
}
|
|
|
|
|
2019-03-18 18:15:26 +03:00
|
|
|
export {
|
2019-03-27 11:27:59 +03:00
|
|
|
// all returns true if the given function `fn` evaluates to a truthy value on
|
|
|
|
// all of the items in `x`. It returns undefined if `x` is not enumerable.
|
|
|
|
all: func(x, fn) {
|
|
|
|
if !is_enumerable(x) { return undefined }
|
|
|
|
|
|
|
|
for k, v in x {
|
|
|
|
if !fn(k, v) { return false }
|
2019-03-18 18:15:26 +03:00
|
|
|
}
|
2019-03-27 11:27:59 +03:00
|
|
|
|
2019-03-18 18:15:26 +03:00
|
|
|
return true
|
|
|
|
},
|
2019-03-27 11:27:59 +03:00
|
|
|
// any returns true if the given function `fn` evaluates to a truthy value on
|
|
|
|
// any of the items in `x`. It returns undefined if `x` is not enumerable.
|
|
|
|
any: func(x, fn) {
|
|
|
|
if !is_enumerable(x) { return undefined }
|
|
|
|
|
|
|
|
for k, v in x {
|
|
|
|
if fn(k, v) { return true }
|
2019-03-18 18:15:26 +03:00
|
|
|
}
|
2019-03-27 11:27:59 +03:00
|
|
|
|
2019-03-18 18:15:26 +03:00
|
|
|
return false
|
|
|
|
},
|
|
|
|
// chunk returns an array of elements split into groups the length of size.
|
2019-03-27 11:27:59 +03:00
|
|
|
// If `x` can't be split evenly, the final chunk will be the remaining elements.
|
|
|
|
// It returns undefined if `x` is not array.
|
|
|
|
chunk: func(x, size) {
|
|
|
|
if !is_array_like(x) || !size { return undefined }
|
|
|
|
|
|
|
|
numElements := len(x)
|
|
|
|
if !numElements { return [] }
|
2019-03-18 18:15:26 +03:00
|
|
|
|
|
|
|
res := []
|
|
|
|
idx := 0
|
|
|
|
for idx < numElements {
|
2019-03-27 11:27:59 +03:00
|
|
|
res = append(res, x[idx:idx+size])
|
2019-03-18 18:15:26 +03:00
|
|
|
idx += size
|
|
|
|
}
|
2019-03-27 11:27:59 +03:00
|
|
|
|
2019-03-18 18:15:26 +03:00
|
|
|
return res
|
2019-03-27 11:27:59 +03:00
|
|
|
},
|
|
|
|
// at returns an element at the given index (if `x` is array) or
|
|
|
|
// key (if `x` is map). It returns undefined if `x` is not enumerable.
|
|
|
|
at: func(x, key) {
|
|
|
|
if !is_enumerable(x) { return undefined }
|
|
|
|
|
|
|
|
if is_array_like(x) {
|
|
|
|
if !is_int(key) { return undefined }
|
|
|
|
} else {
|
|
|
|
if !is_string(key) { return undefined }
|
|
|
|
}
|
|
|
|
|
|
|
|
return x[key]
|
|
|
|
},
|
|
|
|
// each iterates over elements of `x` and invokes `fn` for each element. `fn` is
|
|
|
|
// invoked with two arguments: `key` and `value`. `key` is an int index
|
|
|
|
// if `x` is array. `key` is a string key if `x` is map. It does not iterate
|
|
|
|
// and returns undefined if `x` is not enumerable.
|
|
|
|
each: func(x, fn) {
|
|
|
|
if !is_enumerable(x) { return undefined }
|
|
|
|
|
|
|
|
for k, v in x {
|
|
|
|
fn(k, v)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// filter iterates over elements of `x`, returning an array of all elements `fn`
|
|
|
|
// returns truthy for. `fn` is invoked with two arguments: `key` and `value`.
|
|
|
|
// `key` is an int index if `x` is array. `key` is a string key if `x` is map.
|
|
|
|
// It returns undefined if `x` is not enumerable.
|
|
|
|
filter: func(x, fn) {
|
|
|
|
if !is_array_like(x) { return undefined }
|
|
|
|
|
|
|
|
dst := []
|
|
|
|
for k, v in x {
|
|
|
|
if fn(k, v) { dst = append(dst, v) }
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst
|
|
|
|
},
|
|
|
|
// find iterates over elements of `x`, returning value of the first element `fn`
|
|
|
|
// returns truthy for. `fn` is invoked with two arguments: `key` and `value`.
|
|
|
|
// `key` is an int index if `x` is array. `key` is a string key if `x` is map.
|
|
|
|
// It returns undefined if `x` is not enumerable.
|
|
|
|
find: func(x, fn) {
|
|
|
|
if !is_enumerable(x) { return undefined }
|
|
|
|
|
|
|
|
for k, v in x {
|
|
|
|
if fn(k, v) { return v }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// find_key iterates over elements of `x`, returning key or index of the first
|
|
|
|
// element `fn` returns truthy for. `fn` is invoked with two arguments: `key`
|
|
|
|
// and `value`. `key` is an int index if `x` is array. `key` is a string key if
|
|
|
|
// `x` is map. It returns undefined if `x` is not enumerable.
|
|
|
|
find_key: func(x, fn) {
|
|
|
|
if !is_enumerable(x) { return undefined }
|
|
|
|
|
|
|
|
for k, v in x {
|
|
|
|
if fn(k, v) { return k }
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// map creates an array of values by running each element in `x` through `fn`.
|
|
|
|
// `fn` is invoked with two arguments: `key` and `value`. `key` is an int index
|
|
|
|
// if `x` is array. `key` is a string key if `x` is map. It returns undefined
|
|
|
|
// if `x` is not enumerable.
|
|
|
|
map: func(x, fn) {
|
|
|
|
if !is_enumerable(x) { return undefined }
|
|
|
|
|
|
|
|
dst := []
|
|
|
|
for k, v in x {
|
|
|
|
dst = append(dst, fn(k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return dst
|
|
|
|
},
|
|
|
|
// key returns the first argument.
|
|
|
|
key: func(k, _) { return k },
|
|
|
|
// value returns the second argument.
|
|
|
|
value: func(_, v) { return v }
|
2019-03-18 18:15:26 +03:00
|
|
|
}
|