github.com/m4gshm/gollections@v0.0.13-0.20240331203319-a34a86e58a24/internal/examples/collection/functions/collection_functions_test.go (about)

     1  package examples
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/m4gshm/gollections/collection/immutable/ordered/set"
     9  	"github.com/m4gshm/gollections/convert/as"
    10  	"github.com/m4gshm/gollections/kv/loop/group"
    11  	"github.com/m4gshm/gollections/loop"
    12  	"github.com/m4gshm/gollections/predicate/more"
    13  )
    14  
    15  func Test_group_orderset_with_filtering_by_string_len(t *testing.T) {
    16  	var groupedByLength = group.Of(loop.KeyValue(set.Of(
    17  		"seventh", "seventh", //duplicate
    18  		"first", "second", "third", "fourth",
    19  		"fifth", "sixth", "eighth",
    20  		"ninth", "tenth", "one", "two", "three", "1",
    21  	).Loop(), func(v string) int { return len(v) }, as.Is,
    22  	).FilterKey(
    23  		more.Than(3),
    24  	).ConvertValue(
    25  		func(v string) string { return v + "_" },
    26  	))
    27  
    28  	assert.Equal(t, []string{"first_", "third_", "fifth_", "sixth_", "ninth_", "tenth_", "three_"}, groupedByLength[5])
    29  	assert.Equal(t, []string{"second_", "fourth_", "eighth_"}, groupedByLength[6])
    30  	assert.Equal(t, []string{"seventh_"}, groupedByLength[7])
    31  
    32  }