//
//  NoteEditorTextViewDelegate.swift
//  RichTextExample
//
//  Created by Clay Tinnell on 7/27/15.
//  Copyright (c) 2015 Clay Tinnell. All rights reserved.
//

import UIKit
import Foundation

class NoteEditorTextViewDelegate: NSObject {
    
    func characterIsAWordTerminator(text: String) -> Bool {
        return text == " " || text == "." || text == "," || text == ":" || text == ";" || text == "\n" || text == "!" || text == "?"
    }
    
    func stripSpecialCharacters(text: String) -> [String]? {
        let strippedLowerCaseText = " ".join(text.componentsSeparatedByCharactersInSet(NSCharacterSet.letterCharacterSet().invertedSet)).lowercaseString
        let trimmedWhiteSpace = strippedLowerCaseText.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()).filter({!isEmpty($0)})
        return trimmedWhiteSpace
    }
    
}

extension NoteEditorTextViewDelegate : UITextViewDelegate {
    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
        
        println("text changed: \(text)")
        if characterIsAWordTerminator(text) {
            println("word terminator")
            if let strippedText = stripSpecialCharacters(textView.text) {
                println(",".join(strippedText))
            }
        }
        return true
    }
}

//
//  NoteEditorTextViewDelegate.swift
//  RichTextExample
//
//  Created by Clay Tinnell on 7/27/15.
//  Copyright (c) 2015 Clay Tinnell. All rights reserved.
//

import UIKit
import Foundation

class NoteEditorTextViewDelegate: NSObject {
    
}

extension NoteEditorTextViewDelegate : UITextViewDelegate {
    
    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
        if text.isAWordTerminator() {
            println(textView.text.stripSpecialCharacters().trimExtraWhiteSpace())
        }
        return true
    }
}

private extension String {
    func isAWordTerminator() -> Bool {
        return self == " " || self == "." || self == "," || self == ":" || self == ";" || self == "\n" || self == "!" || self == "?"
    }
    
    func stripSpecialCharacters() -> String {
        return " ".join(self.componentsSeparatedByCharactersInSet(NSCharacterSet.letterCharacterSet().invertedSet)).lowercaseString
    }

    func componentsSeparatedByWhiteSpace() -> [String] {
        return self.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
    }
    
    func trimExtraWhiteSpace() -> String {
        return " ".join(self.componentsSeparatedByWhiteSpace().filter({!$0.isEmpty}))
    }
}




Refactor functions into String extension
Refactor functions into String extension

This seems to provide improved readability and usability.
