| 12345678910111213141516171819202122 |
- import Foundation
- enum WordLimit {
- static func message(maxWords: Int) -> String {
- "Maximum \(maxWords) words allowed."
- }
- static func apply(_ text: String, maxWords: Int) -> (text: String, exceeded: Bool) {
- guard text.wordCount > maxWords else { return (text, false) }
- return (text.truncated(toMaxWords: maxWords), true)
- }
- static func applyImported(
- _ text: String,
- maxWords: Int,
- setError: (String?) -> Void
- ) -> String {
- let (limited, exceeded) = apply(text, maxWords: maxWords)
- setError(exceeded ? message(maxWords: maxWords) : nil)
- return limited
- }
- }
|