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

Commit

Permalink
Fix parameters naming to reflect semantics
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenSorial committed May 8, 2020
1 parent c7adb16 commit bd9536f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 68 deletions.
54 changes: 17 additions & 37 deletions Example/Tests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,20 @@ class Tests: XCTestCase {

func testProtocolWeightedSearch1() {
struct Book: Fuseable {
let author: String
let title: String

public init (author: String, title: String) {
self.author = author
self.title = title
}
let author: String

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

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

let fuse = Fuse()
Expand All @@ -95,25 +90,20 @@ class Tests: XCTestCase {

func testProtocolWeightedSearch2() {
struct Book: Fuseable {
let author: String
let title: String

public init (author: String, title: String) {
self.author = author
self.title = title
}
let author: String

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

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

let fuse = Fuse()
Expand Down Expand Up @@ -185,25 +175,20 @@ class Tests: XCTestCase {

func testProtocolWeightedSearchTokenized() {
struct Book: Fuseable {
let author: String
let title: String

public init (author: String, title: String) {
self.author = author
self.title = title
}
let author: String

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

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

let fuse = Fuse(tokenize: true)
Expand All @@ -219,15 +204,10 @@ class Tests: XCTestCase {
let author: String
let title: String

public init (author: String, title: String) {
self.author = author
self.title = title
}

var properties: [FuseProperty] {
return [
FuseProperty(name: title, weight: 0.5),
FuseProperty(name: author, weight: 0.5),
FuseProperty(value: title, weight: 0.5),
FuseProperty(value: author, weight: 0.5)
]
}
}
Expand Down
43 changes: 20 additions & 23 deletions Fuse/Classes/Fuse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
import Foundation

public struct FuseProperty {
let name: String
let value: String
let weight: Double

public init (name: String) {
self.init(name: name, weight: 1)
}
public init (value: String, weight: Double = 1.0) {
self.value = value

public init (name: String, weight: Double) {
self.name = name
self.weight = weight
}
}
Expand All @@ -42,7 +39,7 @@ public class Fuse {
index: Int,
score: Double,
results: [(
key: String,
value: String,
score: Double,
ranges: [CountableClosedRange<Int>]
)]
Expand Down Expand Up @@ -375,7 +372,7 @@ extension Fuse {

/// Searches for a text pattern in an array of `Fuseable` objects.
///
/// Each `FuseSearchable` object contains a `properties` accessor which returns `FuseProperty` array. Each `FuseProperty` is a tuple containing a `key` (the value of the property which should be included in the search), and a `weight` (how much "weight" to assign to the score)
/// Each `FuseSearchable` object contains a `properties` accessor which returns `FuseProperty` array. Each `FuseProperty` is a tuple containing a `value` (the value of the property which should be included in the search), and a `weight` (how much "weight" to assign to the score)
///
/// ## Example
///
Expand All @@ -387,17 +384,17 @@ extension Fuse {
///
/// var properties: [FuseProperty] {
/// return [
/// FuseProperty(name: title, weight: 0.3),
/// FuseProperty(name: author, weight: 0.7),
/// FuseProperty(value: title, weight: 0.7),
/// FuseProperty(value: author, weight: 0.3)
/// ]
/// }
/// }
///
/// Searching is straightforward:
///
/// let books: [Book] = [
/// Book(author: "John X", title: "Old Man's War fiction"),
/// Book(author: "P.D. Mans", title: "Right Ho Jeeves")
/// Book(title: "Old Man's War fiction", author: "John X"),
/// Book(title: "Right Ho Jeeves", author: "P.D. Mans")
/// ]
///
/// let fuse = Fuse()
Expand All @@ -416,11 +413,11 @@ extension Fuse {
var scores = [Double]()
var totalScore = 0.0

var propertyResults = [(key: String, score: Double, ranges: [CountableClosedRange<Int>])]()
var propertyResults = [(value: String, score: Double, ranges: [CountableClosedRange<Int>])]()

item.properties.forEach { property in

let value = property.name
let value = property.value

if let result = self.search(pattern, in: value) {
let weight = property.weight == 1 ? 1 : 1 - property.weight
Expand All @@ -429,7 +426,7 @@ extension Fuse {

scores.append(score)

propertyResults.append((key: property.name, score: score, ranges: result.ranges))
propertyResults.append((value: property.value, score: score, ranges: result.ranges))
}
}

Expand All @@ -450,7 +447,7 @@ extension Fuse {

/// Asynchronously searches for a text pattern in an array of `Fuseable` objects.
///
/// Each `FuseSearchable` object contains a `properties` accessor which returns `FuseProperty` array. Each `FuseProperty` is a tuple containing a `key` (the value of the property which should be included in the search), and a `weight` (how much "weight" to assign to the score)
/// Each `FuseSearchable` object contains a `properties` accessor which returns `FuseProperty` array. Each `FuseProperty` is a tuple containing a `value` (the value of the property which should be included in the search), and a `weight` (how much "weight" to assign to the score)
///
/// ## Example
///
Expand All @@ -462,17 +459,17 @@ extension Fuse {
///
/// var properties: [FuseProperty] {
/// return [
/// FuseProperty(name: title, weight: 0.3),
/// FuseProperty(name: author, weight: 0.7),
/// FuseProperty(value: title, weight: 0.7),
/// FuseProperty(value: author, weight: 0.3)
/// ]
/// }
/// }
///
/// Searching is straightforward:
///
/// let books: [Book] = [
/// Book(author: "John X", title: "Old Man's War fiction"),
/// Book(author: "P.D. Mans", title: "Right Ho Jeeves")
/// Book(title: "Old Man's War fiction", author: "John X"),
/// Book(title: "Right Ho Jeeves", author: "P.D. Mans")
/// ]
///
/// let fuse = Fuse()
Expand Down Expand Up @@ -501,11 +498,11 @@ extension Fuse {
var scores = [Double]()
var totalScore = 0.0

var propertyResults = [(key: String, score: Double, ranges: [CountableClosedRange<Int>])]()
var propertyResults = [(value: String, score: Double, ranges: [CountableClosedRange<Int>])]()

item.properties.forEach { property in

let value = property.name
let value = property.value

if let result = self.search(pattern, in: value) {
let weight = property.weight == 1 ? 1 : 1 - property.weight
Expand All @@ -514,7 +511,7 @@ extension Fuse {

scores.append(score)

propertyResults.append((key: property.name, score: score, ranges: result.ranges))
propertyResults.append((value: property.value, score: score, ranges: result.ranges))
}
}

Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ struct Book: Fuseable {

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

let books: [Book] = [
Book(author: "John X", title: "Old Man's War fiction"),
Book(author: "P.D. Mans", title: "Right Ho Jeeves")
Book(title: "Old Man's War fiction", author: "John X"),
Book(title: "Right Ho Jeeves", author: "P.D. Mans")
]
let fuse = Fuse()
let results = fuse.search("man", in: books)
Expand All @@ -92,12 +92,12 @@ results.forEach { item in
// Output:
//
// index: 1
// score: 0.015
// results: [(key: "author", score: 0.015000000000000003, ranges: [CountableClosedRange(5...7)])]
// score: 0.015000000000000003
// results: [(value: "P.D. Mans", score: 0.015000000000000003, ranges: [CountableClosedRange(5...7)])]
// ---------------
// index: 0
// score: 0.028
// results: [(key: "title", score: 0.027999999999999997, ranges: [CountableClosedRange(4...6)])]
// score: 0.027999999999999997
// results: [(value: "Old Man\'s War fiction", score: 0.027999999999999997, ranges: [CountableClosedRange(4...6)])]
```

##### Asynchronous version
Expand Down

0 comments on commit bd9536f

Please sign in to comment.