go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/pkg/funcs/length.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package funcs
     9  
    10  import (
    11  	"context"
    12  	"reflect"
    13  )
    14  
    15  func Length[T any](ctx context.Context, input T) (int64, error) {
    16  	return int64(LengthAny(input)), nil
    17  }
    18  
    19  func LengthMany[T any](ctx context.Context, input ...T) (output int64, err error) {
    20  	for _, i := range input {
    21  		output += int64(LengthAny(i))
    22  	}
    23  	return
    24  }
    25  
    26  func LengthAny(input any) int {
    27  	switch typed := input.(type) {
    28  	case string:
    29  		return len(typed)
    30  	default:
    31  		iv := reflect.ValueOf(input)
    32  		return iv.Len()
    33  	}
    34  }