Commit f3112329 authored by Gergely Ujvari's avatar Gergely Ujvari

Introduce _splitTerm() for searchFilter service

Moved out from _tokenize() as a separate function to split
a search term properly into filter, term because toObject()
must call this too.

Fix #2142
parent 4df6d1d7
...@@ -2,6 +2,22 @@ ...@@ -2,6 +2,22 @@
# It expects a search query string where the search term are separated by space character # It expects a search query string where the search term are separated by space character
# and collects them into the given term arrays # and collects them into the given term arrays
module.exports = class SearchFilter module.exports = class SearchFilter
# Splits a search term into filter and data
# i.e.
# 'user:johndoe' -> ['user', 'johndoe']
# 'example:text' -> [null, 'example:text']
_splitTerm: (term) ->
filter = term.slice 0, term.indexOf ":"
unless filter?
# The whole term is data
return [null, term]
if filter in ['quote', 'result', 'since', 'tag', 'text', 'uri', 'user']
data = term[filter.length+1..]
return [filter, data]
else
# The filter is not a power search filter, so the whole term is data
return [null, term]
# This function will slice the search-text input # This function will slice the search-text input
# Slice character: space, # Slice character: space,
...@@ -34,12 +50,9 @@ module.exports = class SearchFilter ...@@ -34,12 +50,9 @@ module.exports = class SearchFilter
# Remove quotes for power search. # Remove quotes for power search.
# I.e. 'tag:"foo bar"' -> 'tag:foo bar' # I.e. 'tag:"foo bar"' -> 'tag:foo bar'
for token, index in tokens for token, index in tokens
filter = token.slice 0, token.indexOf ":" [filter, data] = @_splitTerm(token)
unless filter? then filter = "" if filter?
tokens[index] = filter + ':' + (_removeQuoteCharacter data)
if filter in ['quote', 'result', 'since', 'tag', 'text', 'uri', 'user']
tokenPart = token[filter.length+1..]
tokens[index] = filter + ':' + (_removeQuoteCharacter tokenPart)
tokens tokens
...@@ -61,8 +74,8 @@ module.exports = class SearchFilter ...@@ -61,8 +74,8 @@ module.exports = class SearchFilter
if searchtext if searchtext
terms = @_tokenize(searchtext) terms = @_tokenize(searchtext)
for term in terms for term in terms
[filter, data] = term.split(':') [filter, data] = @_splitTerm(term)
unless data unless filter?
filter = 'any' filter = 'any'
data = term data = term
addToObj(filterToBackendFilter(filter), data) addToObj(filterToBackendFilter(filter), data)
......
{module, inject} = require('angular-mock')
assert = chai.assert
sinon.assert.expose assert, prefix: null
describe 'searchFilter', ->
sandbox = null
searchFilter = null
before ->
angular.module('h', [])
.service('searchFilter', require('../search-filter'))
beforeEach module('h')
beforeEach ->
sandbox = sinon.sandbox.create()
beforeEach inject (_searchFilter_) ->
searchFilter = _searchFilter_
afterEach ->
sandbox.restore()
describe 'toObject', ->
it 'puts a simple search string under the any filter', ->
query = 'foo'
result = searchFilter.toObject(query)
assert.equal(result.any[0], query)
it 'uses the filters as keys in the result object', ->
query = 'user:john text:foo quote:bar other'
result = searchFilter.toObject(query)
assert.equal(result.any[0], 'other')
assert.equal(result.user[0], 'john')
assert.equal(result.text[0], 'foo')
assert.equal(result.quote[0], 'bar')
it 'collects the same filters into a list', ->
query = 'user:john text:foo quote:bar other user:doe text:fuu text:fii'
result = searchFilter.toObject(query)
assert.equal(result.any[0], 'other')
assert.equal(result.user[0], 'john')
assert.equal(result.user[1], 'doe')
assert.equal(result.text[0], 'foo')
assert.equal(result.text[1], 'fuu')
assert.equal(result.text[2], 'fii')
assert.equal(result.quote[0], 'bar')
it 'preserves data with semicolon characters', ->
query = 'uri:http://test.uri'
result = searchFilter.toObject(query)
assert.equal(result.uri[0], 'http://test.uri')
it 'collects valid filters and puts invalid into the any category', ->
query = 'uri:test foo:bar text:hey john:doe quote:according hi-fi a:bc'
result = searchFilter.toObject(query)
assert.isFalse(result.foo?)
assert.isFalse(result.john?)
assert.isFalse(result.a?)
assert.equal(result.uri[0], 'test')
assert.equal(result.text[0], 'hey')
assert.equal(result.quote[0], 'according')
assert.equal(result.any[0], 'foo:bar')
assert.equal(result.any[1], 'john:doe')
assert.equal(result.any[2], 'hi-fi')
assert.equal(result.any[3], 'a:bc')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment