whitesmith/WSTagsField

Feature Request: Case insensitive comparison

Pe-te opened this issue · 4 comments

Pe-te commented

Hi, would be cool if WSTag could do the equals comparison case insensitive, maybe like this?

open var ignoreCaseWhenAdding: Bool = false

open func addTag(_ tag: WSTag) {
    if ignoreCaseWhenAdding {
        if self.tags.contains(where: { $0.text.uppercased() == tag.text.uppercased() } ) { return }
    } else {
        if self.tags.contains(tag) { return }
    }

    self.tags.append(tag)

@Pe-te Nice one! Thanks 👍

Pe-te commented

Hi again, had to add a validator to avoid tags with only a "#", can be used for all kinds of other checks too:

open var validator: ((WSTag, [WSTag]) -> Bool)?

open var ignoreCaseWhenAdding: Bool = false

open func addTag(_ tag: WSTag) {

    if let validator = validator, !validator(tag, self.tags) {
        return
    } else if ignoreCaseWhenAdding {
        if self.tags.contains(where: { $0.text.uppercased() == tag.text.uppercased() }) { return }
    } else {
        if self.tags.contains(tag) { return }
    }

Can be used like this:

tagsField.validator = { tag, _ in
    return tag.text != "#"
}
Pe-te commented

Added pull request for the last version: #96

Thank you, I'll look at the PR.