From 33bc425e90cc1ad96279616b714fd604fceac3ec Mon Sep 17 00:00:00 2001 From: amorozov Date: Fri, 21 Jun 2024 19:06:09 +0300 Subject: [PATCH] feature: added file name utils --- .../array_list/array_list_factories.go | 7 ++++ .../array_list/array_list_methods.go | 42 +++++++++++++++++++ pkg/io/files/file_name_utils.go | 38 +++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 pkg/io/files/file_name_utils.go diff --git a/pkg/collections/array_list/array_list_factories.go b/pkg/collections/array_list/array_list_factories.go index f3410e2..47fefa1 100644 --- a/pkg/collections/array_list/array_list_factories.go +++ b/pkg/collections/array_list/array_list_factories.go @@ -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 +} diff --git a/pkg/collections/array_list/array_list_methods.go b/pkg/collections/array_list/array_list_methods.go index 604a268..6fe81a1 100644 --- a/pkg/collections/array_list/array_list_methods.go +++ b/pkg/collections/array_list/array_list_methods.go @@ -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 +} diff --git a/pkg/io/files/file_name_utils.go b/pkg/io/files/file_name_utils.go new file mode 100644 index 0000000..068c62f --- /dev/null +++ b/pkg/io/files/file_name_utils.go @@ -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...) +}