|
@@ -1,26 +1,5 @@
|
|
|
import Foundation
|
|
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
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
extension String {
|
|
extension String {
|
|
|
var wordCount: Int {
|
|
var wordCount: Int {
|
|
|
let trimmed = trimmingCharacters(in: .whitespacesAndNewlines)
|
|
let trimmed = trimmingCharacters(in: .whitespacesAndNewlines)
|