1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-17 21:18:37 +02:00
ferret/pkg/stdlib/arrays/flatten.go
Tim Voronov 24d8eedd4c
Feature/doc markup (#543)
* Added release notes

* #509 fixedOCOD typo

* Updated values

* Updated comments

* Changed stdlib docs format

* Changed format of array in docs

* Use 'any' instead of 'value' in docs

* New format for optional params

* Updated docs for testing package

* Added namespace information
2020-08-07 21:49:29 -04:00

68 lines
1.4 KiB
Go

package arrays
import (
"context"
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
)
// FLATTEN turns an array of arrays into a flat array.
// All array elements in array will be expanded in the result array.
// Non-array elements are added as they are.
// The function will recurse into sub-arrays up to the specified depth.
// Duplicates will not be removed.
// @param {Any[]} arr - Target array.
// @param {Int} [depth] - Depth level.
// @return {Any[]} - Flat array.
func Flatten(_ context.Context, args ...core.Value) (core.Value, error) {
err := core.ValidateArgs(args, 1, 2)
if err != nil {
return values.None, err
}
err = core.ValidateType(args[0], types.Array)
if err != nil {
return values.None, err
}
arr := args[0].(*values.Array)
level := 1
if len(args) > 1 {
err = core.ValidateType(args[1], types.Int)
if err != nil {
return values.None, err
}
level = int(args[1].(values.Int))
}
currentLevel := 0
result := values.NewArray(int(arr.Length()) * 2)
var unwrap func(input *values.Array)
unwrap = func(input *values.Array) {
currentLevel++
input.ForEach(func(value core.Value, idx int) bool {
if value.Type() != types.Array || currentLevel > level {
result.Push(value)
} else {
unwrap(value.(*values.Array))
currentLevel--
}
return true
})
}
unwrap(arr)
return result, nil
}