pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/pluralize/pluralize.go (about)

     1  // Package pluralize provides methods for pluralization
     2  package pluralize
     3  
     4  // ////////////////////////////////////////////////////////////////////////////////// //
     5  //                                                                                    //
     6  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     7  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     8  //                                                                                    //
     9  // ////////////////////////////////////////////////////////////////////////////////// //
    10  
    11  import (
    12  	"fmt"
    13  	"strings"
    14  )
    15  
    16  // ////////////////////////////////////////////////////////////////////////////////// //
    17  
    18  // DefaultPluralizer holds default pluralization function
    19  var DefaultPluralizer = En
    20  
    21  // ////////////////////////////////////////////////////////////////////////////////// //
    22  
    23  // P pluralizes a word based on the passed number with custom format
    24  func P(format string, n interface{}, data ...string) string {
    25  	return PS(DefaultPluralizer, format, n, data...)
    26  }
    27  
    28  // PS pluralizes a word based on the passed number with custom pluralizer and format
    29  func PS(p Pluralizer, format string, n interface{}, data ...string) string {
    30  	nk, ok := convertNumber(n)
    31  
    32  	if !ok {
    33  		return format
    34  	}
    35  
    36  	if isNumberFirst(format) {
    37  		return fmt.Sprintf(format, n, PluralizeSpecial(p, nk, data...))
    38  	}
    39  
    40  	return fmt.Sprintf(format, PluralizeSpecial(p, nk, data...), n)
    41  }
    42  
    43  // Pluralize pluralizes a word based on the passed number
    44  func Pluralize(n int, data ...string) string {
    45  	return PluralizeSpecial(DefaultPluralizer, n, data...)
    46  }
    47  
    48  // PluralizeSpecial pluralizes a word based on the passed number with custom pluralizer
    49  func PluralizeSpecial(p Pluralizer, n int, data ...string) string {
    50  	return safeSliceGet(data, p(n))
    51  }
    52  
    53  // ////////////////////////////////////////////////////////////////////////////////// //
    54  
    55  func safeSliceGet(data []string, index int) string {
    56  	if len(data) < index {
    57  		return ""
    58  	}
    59  
    60  	return data[index]
    61  }
    62  
    63  func convertNumber(n interface{}) (int, bool) {
    64  	switch u := n.(type) {
    65  	case int32:
    66  		return int(u), true
    67  	case int64:
    68  		return int(u), true
    69  	case uint:
    70  		return int(u), true
    71  	case uint32:
    72  		return int(u), true
    73  	case uint64:
    74  		return int(u), true
    75  	case float32:
    76  		return int(u), true
    77  	case float64:
    78  		return int(u), true
    79  	case int:
    80  		return n.(int), true
    81  	default:
    82  		return 0, false
    83  	}
    84  }
    85  
    86  func isNumberFirst(format string) bool {
    87  	return strings.Index(format, "%s") == strings.LastIndex(format, "%")
    88  }