github.com/developest/gtm-core@v1.0.4-0.20220111132249-cc80a3372c3f/util/string.go (about)

     1  // Copyright 2016 Michael Schenk. All rights reserved.
     2  // Use of this source code is governed by a MIT-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package util
     6  
     7  import (
     8  	"fmt"
     9  	"regexp"
    10  	"strings"
    11  	"time"
    12  	"unicode"
    13  
    14  	"github.com/hako/durafmt"
    15  )
    16  
    17  const (
    18  	SecInMinute = 60
    19  	MinInHour   = 60
    20  	HoursInDay  = 8
    21  	DaysInWeek  = 5
    22  )
    23  
    24  // Percent returns a values percent of the total
    25  func Percent(val, total int) float64 {
    26  	if total == 0 {
    27  		return float64(0)
    28  	}
    29  	return (float64(val) / float64(total)) * 100
    30  }
    31  
    32  // FormatDuration converts seconds into a duration string
    33  func FormatDuration(secs int) string {
    34  	vals := regexp.MustCompile(`\d+`)
    35  	matches := vals.FindAllString(DurationStr(secs), -1)
    36  	switch len(matches) {
    37  	case 3:
    38  		return fmt.Sprintf("%sh %2sm %2ss", matches[0], matches[1], matches[2])
    39  	case 2:
    40  		return fmt.Sprintf("%sm %2ss", matches[0], matches[1])
    41  	case 1:
    42  		return fmt.Sprintf("%ss", matches[0])
    43  	default:
    44  		return ""
    45  	}
    46  }
    47  
    48  // DurationStr returns seconds as a duration string, i.e. 9h10m30s
    49  func DurationStr(secs int) string {
    50  	return (time.Duration(secs) * time.Second).String()
    51  }
    52  
    53  // DurationStrJira returns seconds as duration string, i.e. 1d 9h 10m
    54  func DurationStrJira(secs int) string {
    55  	total := (time.Duration(secs) * time.Second).Truncate(time.Second).Seconds()
    56  	weeks := int(total / (DaysInWeek * HoursInDay * MinInHour * SecInMinute))
    57  	days := int(total/(HoursInDay*MinInHour*SecInMinute)) % DaysInWeek
    58  	hours := int(total/(MinInHour*SecInMinute)) % HoursInDay
    59  	minutes := int(total/SecInMinute) % SecInMinute
    60  	return fmt.Sprintf("%dw %dd %dh %dm", weeks, days, hours, minutes)
    61  }
    62  
    63  // DurationStrLong returns a human readable format for the duration
    64  func DurationStrLong(secs int) string {
    65  	d, err := durafmt.ParseString(DurationStr(secs))
    66  	if err != nil {
    67  		return ""
    68  	}
    69  	return d.String()
    70  }
    71  
    72  // https://github.com/DaddyOh/golang-samples/blob/master/pad.go
    73  
    74  // RightPad2Len https://github.com/DaddyOh/golang-samples/blob/master/pad.go
    75  func RightPad2Len(s string, padStr string, overallLen int) string {
    76  	var padCountInt = 1 + ((overallLen - len(padStr)) / len(padStr))
    77  	var retStr = s + strings.Repeat(padStr, padCountInt)
    78  	return retStr[:overallLen]
    79  }
    80  
    81  // LeftPad2Len https://github.com/DaddyOh/golang-samples/blob/master/pad.go
    82  func LeftPad2Len(s string, padStr string, overallLen int) string {
    83  	var padCountInt = 1 + ((overallLen - len(padStr)) / len(padStr))
    84  	var retStr = strings.Repeat(padStr, padCountInt) + s
    85  	return retStr[(len(retStr) - overallLen):]
    86  }
    87  
    88  // StringInSlice https://github.com/DaddyOh/golang-samples/blob/master/pad.go
    89  func StringInSlice(list []string, a string) bool {
    90  	for _, b := range list {
    91  		if b == a {
    92  			return true
    93  		}
    94  	}
    95  	return false
    96  }
    97  
    98  // Map applys a func to a string array
    99  func Map(vs []string, f func(string) string) []string {
   100  	vsm := make([]string, len(vs))
   101  	for i, v := range vs {
   102  		vsm[i] = f(v)
   103  	}
   104  	return vsm
   105  }
   106  
   107  // UcFirst Uppercase first letter
   108  func UcFirst(str string) string {
   109  	for i, v := range str {
   110  		return string(unicode.ToUpper(v)) + str[i+1:]
   111  	}
   112  	return ""
   113  }