package util

import (
	"strings"
	"unicode"
)

// TrimWhiteSpace removes all whitespace characters from the string
func TrimWhiteSpace(s string) string {
	return strings.Map(func(r rune) rune {
		if unicode.IsSpace(r) {
			return -1
		}
		return r
	}, s)
}

// FieldsAndTrimSpace splits string by the given separator function and trims space for each field
func FieldsAndTrimSpace(s string, fn func(rune) bool) []string {
	fields := strings.FieldsFunc(s, fn)
	result := make([]string, 0, len(fields))
	for _, field := range fields {
		trimmed := strings.TrimSpace(field)
		if len(trimmed) > 0 {
			result = append(result, trimmed)
		}
	}
	return result
}
