config-lsp/common/utils.go
2024-07-28 20:03:11 +02:00

103 lines
1.6 KiB
Go

package common
import (
"os"
"strings"
)
func GetLine(path string, line int) (string, error) {
path = path[len("file://"):]
readBytes, err := os.ReadFile(path)
if err != nil {
return "", err
}
// Split file into lines
lines := strings.Split(string(readBytes), "\n")
return lines[line], nil
}
func Map[T any, O any](values []T, f func(T) O) []O {
result := make([]O, len(values))
for index, value := range values {
result[index] = f(value)
}
return result
}
func SliceToSet[T comparable](values []T) map[T]struct{} {
set := make(map[T]struct{})
for _, value := range values {
set[value] = struct{}{}
}
return set
}
func SliceToMap[T comparable, O any](values []T, defaultValue O) map[T]O {
set := make(map[T]O)
for _, value := range values {
set[value] = defaultValue
}
return set
}
func FilterWhere[T any](values []T, f func(T) bool) []T {
result := make([]T, 0)
for _, value := range values {
if f(value) {
result = append(result, value)
}
}
return result
}
func FilterMapWhere[T comparable, O any](values map[T]O, f func(T, O) bool) map[T]O {
result := make(map[T]O)
for key, value := range values {
if f(key, value) {
result[key] = value
}
}
return result
}
func KeysOfMap[T comparable, O any](values map[T]O) []T {
keys := make([]T, 0)
for key := range values {
keys = append(keys, key)
}
return keys
}
func DoesPathExist(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func IsPathDirectory(path string) bool {
info, err := os.Stat(path)
return err == nil && info.IsDir()
}
func IsPathFile(path string) bool {
info, err := os.Stat(path)
return err == nil && !info.IsDir()
}