github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/types/to_int.go (about)

     1  package types
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/MontFerret/ferret/pkg/runtime/core"
     7  	"github.com/MontFerret/ferret/pkg/runtime/values"
     8  )
     9  
    10  // TO_INT takes an input value of any type and convert it into an integer value.
    11  // None and false are converted to the value 0
    12  // true is converted to 1
    13  // Numbers keep their original value
    14  // Strings are converted to their numeric equivalent if the string contains a valid representation of a number.
    15  // String values that do not contain any valid representation of a number will be converted to the number 0.
    16  // An empty array is converted to 0, an array with one member is converted into the result of TO_NUMBER() for its sole member.
    17  // An array with two or more members is converted to the number 0.
    18  // An object / HTML node is converted to the number 0.
    19  // @param {Any} value - Input value of arbitrary type.
    20  // @return {Int} - An integer value.
    21  func ToInt(_ context.Context, args ...core.Value) (core.Value, error) {
    22  	err := core.ValidateArgs(args, 1, 1)
    23  
    24  	if err != nil {
    25  		return values.None, err
    26  	}
    27  
    28  	return values.ToInt(args[0]), nil
    29  }