github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/arrays/sorted_unique.go (about)

     1  package arrays
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/MontFerret/ferret/pkg/runtime/core"
     7  	"github.com/MontFerret/ferret/pkg/runtime/values"
     8  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
     9  )
    10  
    11  // SORTED_UNIQUE sorts all elements in anyArray.
    12  // The function will use the default comparison order for FQL value types.
    13  // Additionally, the values in the result array will be made unique
    14  // @param {Any[]} array - Target array.
    15  // @return {Any[]} - Sorted array.
    16  func SortedUnique(_ context.Context, args ...core.Value) (core.Value, error) {
    17  	err := core.ValidateArgs(args, 1, 1)
    18  
    19  	if err != nil {
    20  		return values.None, err
    21  	}
    22  
    23  	err = core.ValidateType(args[0], types.Array)
    24  
    25  	if err != nil {
    26  		return values.None, err
    27  	}
    28  
    29  	arr := args[0].(*values.Array)
    30  
    31  	if arr.Length() == 0 {
    32  		return values.NewArray(0), nil
    33  	}
    34  
    35  	return ToUniqueArray(arr.Sort()), nil
    36  }