Skip to content
This repository was 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 all commits
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.

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

struct Publisher {
let name: String
let year: String
}

struct Book: Fuseable {

let author: String
let title: String?
let publisher: Publisher?

var properties: [FuseProperty] {
return [
FuseProperty(name: "title", weight: 0.3),
FuseProperty(name: "author", weight: 0.6),
FuseProperty(name: "publisher.name", weight: 0.1),
]
}
}

func testListOfFuseableStruct() {

let books: [Book] = [
Book(author: "John X", title: "Old Man's War fiction", publisher: Publisher(name: "Tor Books", year: "2005")),
Book(author: "P.D. Mans", title: "Right Ho Jeeves", publisher: Publisher(name: "Herbert Jenkins", year: "1934")),
Book(author: "Robert M. Pirsig", title: "Lila", publisher: Publisher(name: "Bantom Books", year: "1991")),
Book(author: "Yuval Noah Harari", title: "Sapiens", publisher: Publisher(name: "Harper", year: "2015")),
Book(author: "Homer", title: "The Odyssey", publisher: nil)
]

let fuse = Fuse()

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


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


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

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

}

func testListOfFuseableStructASync() {

let books: [Book] = [
Book(author: "John X", title: "Old Man's War fiction", publisher: Publisher(name: "Tor Books", year: "2005")),
Book(author: "P.D. Mans", title: "Right Ho Jeeves", publisher: Publisher(name: "Herbert Jenkins", year: "1934")),
Book(author: "Robert M. Pirsig", title: "Lila", publisher: Publisher(name: "Bantom Books", year: "1991")),
Book(author: "Yuval Noah Harari", title: "Sapiens", publisher: Publisher(name: "Harper", year: "2015")),
Book(author: "Homer", title: "The Odyssey", publisher: nil)
]

let fuse = Fuse()

// XCTest async
let expectation = self.expectation(description: #function)
var asyncResult: [Fuse.FusableSearchResult] = []

fuse.search("man", in: books){ results in
asyncResult = results
expectation.fulfill()
}

// Then
waitForExpectations(timeout: 10)


// two matches
XCTAssertEqual(asyncResult.count, 3)

// the key should be the name of the property
XCTAssertEqual(asyncResult[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
6 changes: 3 additions & 3 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 Expand Up @@ -505,7 +505,7 @@ extension Fuse {

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
32 changes: 32 additions & 0 deletions Fuse/Classes/FuseUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,36 @@ class FuseUtilities {
}
return ranges
}

static func propertyStringValueUsingKey(_ key: String, instance: Any) -> String {

// values containing periods Also have spaces. Here to support using value rather than key in FuseProperty
if(key.contains(" ")){ return key }

var mirror = Mirror(reflecting: instance)
var propertyValue: Any = mirror.descendant(key) ?? key
// walk key path if dot notation is present
let keyFragments = key.components(separatedBy: ".")
// only do the work if there were key path fragments
if(keyFragments.count > 1){
// iterate fragments
keyFragments.forEach{ keyFragment in
// retrieve property value
propertyValue = mirror.descendant(keyFragment) ?? ""
// reflect on property value
mirror = Mirror(reflecting: propertyValue)
// if optional, descendents aren't there ;-\
if(mirror.displayStyle == .optional) {
// unwrap optional
if let some = mirror.children.first?.value {
// use the wrapped value
propertyValue = some
// reflect on the unwrapped value
mirror = Mirror(reflecting: propertyValue)
}
}
}
}
return propertyValue 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