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

Fixes #33 FuseProperty.name reports the value #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions Example/Tests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,66 @@ class Tests: XCTestCase {
XCTAssert(results[1].index == 1, "The second result is the second book")
}

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),
]
}
}

func testListOfFuseableStruct() {

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

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("--------------------")
}
*/
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this could have been uncommented


// 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)])]
// ...

// two matches
XCTAssertEqual(results.count, 2)

// the key should be the name of the property
XCTAssertEqual(results[0].results[0].key, "author")



}

//MARK: - Performance Tests
func testPerformanceSync() {
if let path = Bundle(for: type(of: self)).path(forResource: "books", ofType: "txt") {
Expand Down
4 changes: 2 additions & 2 deletions Fuse/Classes/Fuse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,8 @@ extension Fuse {
var propertyResults = [(key: String, score: Double, ranges: [CountableClosedRange<Int>])]()

item.properties.forEach { property in

let value = property.name
let value = FuseUtilities.propertyStringValueUsingKey(property.name, instance: item)

if let result = self.search(pattern, in: value) {
let weight = property.weight == 1 ? 1 : 1 - property.weight
Expand Down
5 changes: 5 additions & 0 deletions Fuse/Classes/FuseUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ class FuseUtilities {
}
return ranges
}

static func propertyStringValueUsingKey(_ key: String, instance: Any) -> String {
let mirror = Mirror(reflecting: instance)
return mirror.descendant(key) as? String ?? key
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ struct Book: Fuseable {

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