Skip to content

Commit f89363b

Browse files
authored
Merge pull request #4 from apiheat/frn
Func from FRN
2 parents 651299d + 66236e3 commit f89363b

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

common.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ func VerifyArgumentByName(c *cli.Context, argName string) {
7474
}
7575
}
7676

77+
// SetStringId value
78+
func SetStringId(c *cli.Context) string {
79+
var id string
80+
if c.NArg() == 0 {
81+
log.Fatal("Please provide user e-mail")
82+
}
83+
84+
id = c.Args().Get(0)
85+
return id
86+
}
87+
88+
// SetIntID value and verify that it is int
7789
func SetIntID(c *cli.Context, errMessage string) string {
7890
var id string
7991
if c.NArg() == 0 {

strings.go

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
11
package commonCLI
22

3-
import "strings"
3+
import (
4+
"sort"
5+
"strconv"
6+
"strings"
7+
)
48

5-
// StringToStringsArr transform string of comma seperated word to array of words
9+
// StringToStringsArr transform string of comma seperated words to array of strings
610
func StringToStringsArr(str string) (strArr []string) {
711
for _, s := range strings.Split(str, ",") {
812
strArr = append(strArr, s)
913
}
1014
return strArr
1115
}
1216

17+
// StringToIntArr transform string of comma seperated words to array of ints
18+
func StringToIntArr(str string) (intArr []int) {
19+
for _, s := range strings.Split(str, ",") {
20+
num, _ := strconv.Atoi(s)
21+
intArr = append(intArr, num)
22+
}
23+
24+
sort.Ints(intArr)
25+
return intArr
26+
}
27+
1328
// StringsSlicesDifference give you diff between 2 strings slices
1429
func StringsSlicesDifference(slice1 []string, slice2 []string) []string {
1530
var diff []string
@@ -29,3 +44,72 @@ func StringsSlicesDifference(slice1 []string, slice2 []string) []string {
2944

3045
return diff
3146
}
47+
48+
// IsStringInSlice returns TRUE is slice contains string and false if not
49+
func IsStringInSlice(a string, list []string) bool {
50+
// We need that to not filter for empty list
51+
if len(list) > 0 {
52+
for _, b := range list {
53+
if b == a {
54+
return true
55+
}
56+
}
57+
return false
58+
}
59+
return true
60+
}
61+
62+
// RemoveIntDuplicates removes duplicated elements from int array
63+
func RemoveIntDuplicates(elements []int) []int {
64+
// Use map to record duplicates as we find them.
65+
encountered := map[int]bool{}
66+
result := []int{}
67+
68+
for v := range elements {
69+
if encountered[elements[v]] == true {
70+
// Do not add duplicate.
71+
} else {
72+
// Record this element as an encountered element.
73+
encountered[elements[v]] = true
74+
// Append to result slice.
75+
result = append(result, elements[v])
76+
}
77+
}
78+
// Return the new slice.
79+
return result
80+
}
81+
82+
// RemoveStringDuplicates removes duplicated elements from strings array
83+
func RemoveStringDuplicates(str []string) []string {
84+
// Use map to record duplicates as we find them.
85+
encountered := map[string]bool{}
86+
result := []string{}
87+
88+
for v := range str {
89+
if encountered[str[v]] == true {
90+
// Do not add duplicate.
91+
} else {
92+
// Record this element as an encountered element.
93+
encountered[str[v]] = true
94+
// Append to result slice.
95+
if str[v] != "" {
96+
result = append(result, str[v])
97+
}
98+
}
99+
}
100+
// Return the new slice.
101+
return result
102+
}
103+
104+
// DeleteSlicefromSlice deletes one slice from another and removes duplicate objects
105+
func DeleteSlicefromSlice(slice, delete []int) []int {
106+
for _, d := range delete {
107+
for i := len(slice) - 1; i >= 0; i-- {
108+
if slice[i] == d {
109+
slice = append(slice[:i], slice[i+1:]...)
110+
}
111+
}
112+
}
113+
114+
return RemoveIntDuplicates(slice)
115+
}

0 commit comments

Comments
 (0)