feature: added file name utils

master
amorozov 2024-06-21 19:06:09 +03:00
parent e85baa72f6
commit 33bc425e90
3 changed files with 87 additions and 0 deletions

View File

@ -6,3 +6,10 @@ func NewArrayList[T any]() *ArrayList[T] {
size: 0,
}
}
func NewArrayListWithContent[T any](content ...T) *ArrayList[T] {
result := NewArrayList[T]()
result.AddAllS(&content)
return result
}

View File

@ -3,6 +3,7 @@ package array_list
import (
"errors"
"fmt"
"slices"
)
func (self *ArrayList[T]) Add(element T) {
@ -98,6 +99,22 @@ func (self *ArrayList[T]) AddAll(list *ArrayList[T]) {
})
}
func (self *ArrayList[T]) AddAllS(slice *[]T) {
for _, v := range *slice {
self.Add(v)
}
}
func (self *ArrayList[T]) SetAll(list *ArrayList[T]) {
self.Clear()
self.AddAll(list)
}
func (self *ArrayList[T]) SetAllS(list *[]T) {
self.Clear()
self.AddAllS(list)
}
func (self *ArrayList[T]) Any(test func(T) bool) bool {
for index, value := range self.content {
if index >= self.size {
@ -148,3 +165,28 @@ func (self *ArrayList[T]) IsEmpty() bool {
func (self *ArrayList[T]) String() string {
return fmt.Sprintf("%v", self.content)
}
func (self *ArrayList[T]) Clear() {
self.content = self.content[:0]
}
func (self *ArrayList[T]) GetLast() T {
if self.IsEmpty() {
panic(any(errors.New("trying to get last element from empty array list")))
}
return self.Get(self.Size() - 1)
}
func (self *ArrayList[T]) GetFirst() T {
if self.IsEmpty() {
panic(any(errors.New("trying to get first element from empty array list")))
}
return self.Get(0)
}
func (self *ArrayList[T]) ToSlice() []T {
result := slices.Clone(self.content)[:self.size]
return result
}

View File

@ -0,0 +1,38 @@
package files
import (
"path/filepath"
"slices"
"strings"
"git.tswf.io/incredible-go/incredible-go-core/pkg/collections/array_list"
)
func GetSimpleName(filePath string) string {
return SplitFileName(filePath).GetLast()
}
func GetExtension(filePath string) string {
split := strings.Split(filePath, ".")
return split[len(split)-1]
}
func GetSimpleNameWithoutExtension(filePath string) string {
simpleName := GetSimpleName(filePath)
split := slices.DeleteFunc(strings.Split(simpleName, "."), func(s string) bool {
return s == ""
})
if len(split) == 1 {
return filePath
}
return filepath.Join(split[:len(split)-1]...)
}
func SplitFileName(filePath string) *array_list.ArrayList[string] {
filePath = filepath.ToSlash(filePath)
split := strings.Split(filePath, "/")
return array_list.NewArrayListWithContent[string](split...)
}