ReadKeep/readeck/Data/Utils/LabelUtils.swift
Ilyas Hallak 3ea4e49686 fix: Properly URL-encode labels parameter for API requests
- Add quotes around label values to match API requirements
- Fix label filtering for labels with spaces (e.g. 'aa aa')
- Ensure proper URL encoding as required by server
- Maintains existing pagination and filtering functionality
2025-10-01 21:36:59 +02:00

27 lines
1.3 KiB
Swift

import Foundation
struct LabelUtils {
/// Processes a label input string and returns it as a single trimmed label
/// - Parameter input: The input string containing a label (spaces are allowed)
/// - Returns: Array containing the trimmed label, or empty array if input is empty
static func splitLabelsFromInput(_ input: String) -> [String] {
let trimmed = input.trimmingCharacters(in: .whitespacesAndNewlines)
return trimmed.isEmpty ? [] : [trimmed]
}
/// Filters out labels that already exist in current or available labels
/// - Parameters:
/// - labels: Array of labels to filter
/// - currentLabels: Currently selected labels
/// - availableLabels: Available labels (optional)
/// - Returns: Array of unique labels that don't already exist
static func filterUniqueLabels(_ labels: [String], currentLabels: [String], availableLabels: [String] = []) -> [String] {
let currentSet = Set(currentLabels.map { $0.lowercased() })
let availableSet = Set(availableLabels.map { $0.lowercased() })
return labels.filter { label in
let lowercased = label.lowercased()
return !currentSet.contains(lowercased) && !availableSet.contains(lowercased)
}
}
}