Skip to content
This repository has been archived by the owner on May 8, 2022. It is now read-only.

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

Open
blakewilson opened this issue Jul 23, 2019 · 2 comments

Comments

@blakewilson
Copy link

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?

@blakewilson
Copy link
Author

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!

@alexandrethsilva
Copy link

@krisk Do you have any feedback on this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants