Skip to content

Commit

Permalink
Merge pull request bayleeadamoss#176 from tinytacoteam/feature/block-…
Browse files Browse the repository at this point in the history
…type-ranking

Create a better ranking order based on block type.
  • Loading branch information
amjith authored Jan 27, 2017
2 parents c0429a3 + ae48b2f commit d79e553
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 35 deletions.
1 change: 1 addition & 0 deletions app/blocks/input/keyword.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Keyword extends InputBlock {
return new Promise((resolve) => {
resolve([
{
blockRank: 2,
title: this.title,
subtitle: this.subtitle,
value: this.keyword,
Expand Down
6 changes: 5 additions & 1 deletion app/blocks/input/prefixScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ class PrefixScript extends InputBlock {
this.logger.log('verbose', 'Executing Script', { query })
return this._ensurePromise(this.script(query, env)).then((results) => {
this.logger.log('info', 'Script Results', { results })
return this._validateResults(results)
return this._validateResults(results.map((result) => {
return Object.assign({}, result, {
blockRank: 3,
})
}))
}).catch((error) => {
this.logger.log('error', 'Script failed', { query, error })
})
Expand Down
6 changes: 5 additions & 1 deletion app/blocks/input/rootScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ class RootScript extends InputBlock {
this.logger.log('verbose', 'Executing Script', { query })
return this._ensurePromise(this.script.search(query, env)).then((results) => {
this.logger.log('info', 'Script Results', { results })
return this._validateResults(results)
return this._validateResults(results.map((result) => {
return Object.assign({}, result, {
blockRank: 1,
})
}))
}).catch((error) => {
this.logger.log('error', 'Script failed', { query, error })
})
Expand Down
9 changes: 5 additions & 4 deletions app/transforms/resultSorter.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const resultSorter = {
sort (results, clickedResults) {
return results.slice(0).map((result) => {
let score = -1
if (result.id) {
score = clickedResults.reduce((memo, clickedResult) => {
const score = clickedResults.reduce((memo, clickedResult) => {
if (clickedResult.id === result.id) memo++
return memo
}, 0)
}, result.blockRank)
return { score, result }
}
return { score, result }
const noIdScore = result.blockRank - 3
return { score: noIdScore, result }
}).sort((a, b) => {
if (a.score !== b.score) return b.score - a.score
const aIndex = results.indexOf(a.result)
Expand Down
89 changes: 60 additions & 29 deletions test/app/transforms/resultSorter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,43 @@ describe('ResultSorter', () => {
describe('given the second item is clicked', () => {
it('ranks the second item first', () => {
const results = [
{ id: 'burrito' },
{ id: 'taco' },
{ id: 'quesadilla' },
{ id: 'burrito', blockRank: 1 },
{ id: 'taco', blockRank: 1 },
{ id: 'quesadilla', blockRank: 1 },
]
const clicked = [ { id: 'taco' } ]
const sortedNames = resultSorter.sort(results, clicked)
expect(sortedNames).to.deep.equal([
{ id: 'taco' },
{ id: 'burrito' },
{ id: 'quesadilla' },
{ id: 'taco', blockRank: 1 },
{ id: 'burrito', blockRank: 1 },
{ id: 'quesadilla', blockRank: 1 },
])
})
})

describe('given some results without ids', () => {
it('ranks the second item first', () => {
const results = [
{ id: 'burrito' },
{ id: 'taco' },
{ id: 'quesadilla' },
{ id: 'burrito', blockRank: 1 },
{ id: 'taco', blockRank: 1 },
{ id: 'quesadilla', blockRank: 1 },
]
const sortedNames = resultSorter.sort(results, [])
expect(sortedNames).to.deep.equal([
{ id: 'burrito' },
{ id: 'taco' },
{ id: 'quesadilla' },
{ id: 'burrito', blockRank: 1 },
{ id: 'taco', blockRank: 1 },
{ id: 'quesadilla', blockRank: 1 },
])
})
})

describe('given mixed results', () => {
it('ranks the second item first', () => {
const results = [
{ name: 'cheeseburger' },
{ id: 1, name: 'burrito' },
{ id: 2, name: 'taco' },
{ id: 3, name: 'quesadilla' },
{ name: 'cheeseburger', blockRank: 1 },
{ id: 1, name: 'burrito', blockRank: 1 },
{ id: 2, name: 'taco', blockRank: 1 },
{ id: 3, name: 'quesadilla', blockRank: 1 },
]
const clicked = [ { id: 2 } ]
const sortedNames = resultSorter.sort(results, clicked).map((item) => {
Expand All @@ -59,19 +59,19 @@ describe('ResultSorter', () => {
describe('given non-deterministic sorts', () => {
it('maintains the current index', () => {
const results = [
{ title: 'One' },
{ title: 'Two' },
{ title: 'Three' },
{ title: 'Four' },
{ title: 'Five' },
{ title: 'Six' },
{ title: 'Seven' },
{ title: 'Eight' },
{ title: 'Nine' },
{ title: 'Ten' },
{ title: 'Eleven' },
{ title: 'Twelve' },
{ title: 'Thirteen' },
{ title: 'One', blockRank: 1 },
{ title: 'Two', blockRank: 1 },
{ title: 'Three', blockRank: 1 },
{ title: 'Four', blockRank: 1 },
{ title: 'Five', blockRank: 1 },
{ title: 'Six', blockRank: 1 },
{ title: 'Seven', blockRank: 1 },
{ title: 'Eight', blockRank: 1 },
{ title: 'Nine', blockRank: 1 },
{ title: 'Ten', blockRank: 1 },
{ title: 'Eleven', blockRank: 1 },
{ title: 'Twelve', blockRank: 1 },
{ title: 'Thirteen', blockRank: 1 },
]
const sortedItems = resultSorter.sort(results, []).map((item) => {
return item.title
Expand All @@ -93,4 +93,35 @@ describe('ResultSorter', () => {
])
})
})

describe('given various block return types', () => {
it('ranks higher block types first', () => {
const results = [
{ id: 'burrito', blockRank: 0 },
{ id: 'taco', blockRank: 1 },
{ id: 'quesadilla', blockRank: 0 },
]
const sortedNames = resultSorter.sort(results, [])
expect(sortedNames).to.deep.equal([
{ id: 'taco', blockRank: 1 },
{ id: 'burrito', blockRank: 0 },
{ id: 'quesadilla', blockRank: 0 },
])
})

it('sorts clicked items higher', () => {
const results = [
{ id: 'burrito', blockRank: 0 },
{ id: 'taco', blockRank: 1 },
{ id: 'quesadilla', blockRank: 0 },
]
const clicked = [ { id: 'quesadilla' }, { id: 'quesadilla' } ]
const sortedNames = resultSorter.sort(results, clicked)
expect(sortedNames).to.deep.equal([
{ id: 'quesadilla', blockRank: 0 },
{ id: 'taco', blockRank: 1 },
{ id: 'burrito', blockRank: 0 },
])
})
})
})

0 comments on commit d79e553

Please sign in to comment.