github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/objects/values.go (about)

     1  package objects
     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  // VALUES return the attribute values of the object as an array.
    12  // @param {Object} object - Target object.
    13  // @return {Any[]} - Values of document returned in any order.
    14  func Values(_ context.Context, args ...core.Value) (core.Value, error) {
    15  	err := core.ValidateArgs(args, 1, 1)
    16  
    17  	if err != nil {
    18  		return values.None, err
    19  	}
    20  
    21  	err = core.ValidateType(args[0], types.Object)
    22  
    23  	if err != nil {
    24  		return values.None, err
    25  	}
    26  
    27  	obj := args[0].(*values.Object)
    28  	vals := values.NewArray(0)
    29  
    30  	obj.ForEach(func(val core.Value, key string) bool {
    31  		cloneable, ok := val.(core.Cloneable)
    32  
    33  		if ok {
    34  			val = cloneable.Clone()
    35  		}
    36  
    37  		vals.Push(val)
    38  
    39  		return true
    40  	})
    41  
    42  	return vals, nil
    43  }