krisk / fuse-swift

A lightweight fuzzy-search library, with zero dependencies

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Search through an array of Fuseable objects with a value that is an array

blakewilson opened this issue · comments

Hi there! This is an awesome project, thank you for your hard work. I've used Fuse in the past in the JavaScript world and have found a need for it in Swift, so I'm excited to use this.

I'm trying to find a way to include a FuseProperty that is an array of keywords. However, Fuse doesn't seem to acknowledge the key if it holds an array opposed to a string. Here is the code that I have:

import Cocoa

class Book: Fuseable {
    @objc dynamic var name: String
    @objc dynamic var author: String
    @objc dynamic var keywords: [String] // <- This value doesn't seem to make a different
    
    init(author: String, name: String, keywords: [String]) {
        self.author = author
        self.name = name
        self.keywords = keywords
    }
    
    var properties: [FuseProperty] {
        return [
            FuseProperty(name: "name", weight: 0.3),
            FuseProperty(name: "author", weight: 0.2),
            FuseProperty(name: "keywords", weight: 0.5) // <- Not included in the results
        ]
    }
}

var books = [Book]()

let fuse = Fuse()

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        books.append(Book(author: "John X", name: "Old Man's War fiction", keywords: ["Testing 1", "Testing 2"]))
        books.append(Book(author: "P.D. Mans", name: "Right Ho Jeeves", keywords: ["Testing 3", "Testing 4"]))
        
        let results = fuse.search("man", in: books)
        
        for result in results {
            print(result)
        }

        // Do any additional setup after loading the view.
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

Is this supported in fuse-swift?

I see on line 423 of Fuse.swift that the value is cast to a String, which is causing this.

Would you be open to a PR to change this behavior if the property is an array of strings? How would you like this done if so? Could it emulate the "Searching in an array of strings" method you've already created?

Thanks for your time!

@krisk Do you have any feedback on this?