summaryrefslogtreecommitdiff
path: root/internal/split/split.go
blob: e03335c59eef031835014a261b23ada38a489898 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package split

import (
	"strings"
)

func FormatInput(tokenArray []string, delimeterArray []string) []string {
	for _, delimeter := range delimeterArray {
		for index, element := range tokenArray {
			tokenArray = NewArrayWithSplit(tokenArray, index, element, delimeter)
		}
	}

	return tokenArray
}

func checkArrayElementsEmpty(array []string) bool {
	for _, str := range array {
		if str != "" {
			return false
		}
	}
	return true
}

func NewArrayWithSplit(initialArray []string, index int, token string, delimeter string) (result []string) {
	split := strings.Split(token, delimeter)
	splitLength := len(split)

	/*
		When a token only consists of delimeter * N (N >= 0),
		the resulting split consists of N empty elements.
		Here we check if it is so and essentialy remove that token
		from resulting array.
	*/

	splitIsEmpty := checkArrayElementsEmpty(split)
	if splitIsEmpty {
		result = append(initialArray[:index], initialArray[index+1:]...)
		return result
	}

	if splitLength > 1 {
		if split[splitLength-1] == "" {
			split = split[:splitLength-1]
		}

		result = append(initialArray[:index], append(split, initialArray[index+1:]...)...)
	}
	if splitLength == 1 {
		result = initialArray
	}

	return result
}