github.com/searKing/golang/go@v1.2.74/container/slice/limit.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package slice
     6  
     7  import (
     8  	"github.com/searKing/golang/go/util/object"
     9  )
    10  
    11  // LimitFunc Returns a slice consisting of the elements of this slice, truncated
    12  // to be no longer than {@code maxSize} in length.
    13  func LimitFunc(s interface{}, maxSize int) interface{} {
    14  	return normalizeSlice(limitFunc(Of(s), maxSize), s)
    15  }
    16  
    17  // limitFunc is the same as LimitFunc.
    18  func limitFunc(s []interface{}, maxSize int) []interface{} {
    19  	object.RequireNonNil(s, "limitFunc called on nil slice")
    20  	m := len(s)
    21  	if m > maxSize {
    22  		m = maxSize
    23  	}
    24  	return s[:m]
    25  }