krisk / fuse-swift

A lightweight fuzzy-search library, with zero dependencies
MIT License
935 stars 112 forks source link

Fuseable FuseProperty.name reports the value rather than the key #33

Open levous opened 4 years ago

levous commented 4 years ago

When using Fuseable protocol, the key to be searched is replaced with the value at runtime. The README indicates the expected report will contain the name of the property, not the value. The example code is passing in the value of each property at runtime. Using a string "key" instead will pass the string characters of the key (name) as the searchable text.

expected results: matchedItem.results: [(key: "title", score: 0.027999999999999997, ranges: [CountableClosedRange(4...6)])]

actual results: matchedItem.results: [(key: "Old Man\'s War fiction", score: 0.027999999999999997, ranges: [ClosedRange(4...6)])]

` import XCTest import Fuse

class FuseTests: XCTestCase {

struct Book: Fuseable {

    let author: String
    let title: String

    var properties: [FuseProperty] {
        return [
            FuseProperty(name: title, weight: 0.3),
            FuseProperty(name: author, weight: 0.7),
        ]
    }
}

override func setUp() {
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

override func tearDown() {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
}

func testFuseable() {

    let books: [Book] = [
        Book(author: "John X", title: "Old Man's War fiction"),
        Book(author: "P.D. Mans", title: "Right Ho Jeeves")
    ]

    let fuse = Fuse()

    let results = fuse.search("man", in: books)

    results.forEach { item in
        print("-- Fuse Result -----")
        print("index: \(item.index)")
        print("score: \(item.score)")
        print("results: \(item.results)")
        print("--------------------")
    }

    // Expected Output:
    // ...
    // index: 0
    // score: 0.028
    // results: [(key: "title", score: 0.027999999999999997, ranges: [CountableClosedRange(4...6)])]
    // ...

    // Actual Output

    // ...
    // index: 0
    // score: 0.027999999999999997
    // results: [(key: "Old Man\'s War fiction", score: 0.027999999999999997, ranges: [ClosedRange(4...6)])]
    // ...

    XCTAssertEqual(results[0].results[0].key, "author")

}

}

`

StevenSorial commented 4 years ago

This is a result of #20. I think its a semantics problem and the only change needed is to change FuseProperty's name to be value and FusableSearchResult's key to value.