Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
3ef8de9a
Commit
3ef8de9a
authored
Apr 08, 2015
by
Randall Leeds
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2143 from hypothesis/2142-split-search-terms
Introduce _splitTerm() for searchFilter service
parents
4df6d1d7
f3112329
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
8 deletions
+90
-8
search-filter.coffee
h/static/scripts/search-filter.coffee
+21
-8
search-filter-test.coffee
h/static/scripts/test/search-filter-test.coffee
+69
-0
No files found.
h/static/scripts/search-filter.coffee
View file @
3ef8de9a
...
...
@@ -2,6 +2,22 @@
# It expects a search query string where the search term are separated by space character
# and collects them into the given term arrays
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
# Slice character: space,
...
...
@@ -34,12 +50,9 @@ module.exports = class SearchFilter
# Remove quotes for power search.
# I.e. 'tag:"foo bar"' -> 'tag:foo bar'
for
token
,
index
in
tokens
filter
=
token
.
slice
0
,
token
.
indexOf
":"
unless
filter
?
then
filter
=
""
if
filter
in
[
'quote'
,
'result'
,
'since'
,
'tag'
,
'text'
,
'uri'
,
'user'
]
tokenPart
=
token
[
filter
.
length
+
1
..]
tokens
[
index
]
=
filter
+
':'
+
(
_removeQuoteCharacter
tokenPart
)
[
filter
,
data
]
=
@
_splitTerm
(
token
)
if
filter
?
tokens
[
index
]
=
filter
+
':'
+
(
_removeQuoteCharacter
data
)
tokens
...
...
@@ -61,8 +74,8 @@ module.exports = class SearchFilter
if
searchtext
terms
=
@
_tokenize
(
searchtext
)
for
term
in
terms
[
filter
,
data
]
=
term
.
split
(
':'
)
unless
data
[
filter
,
data
]
=
@
_splitTerm
(
term
)
unless
filter
?
filter
=
'any'
data
=
term
addToObj
(
filterToBackendFilter
(
filter
),
data
)
...
...
h/static/scripts/test/search-filter-test.coffee
0 → 100644
View file @
3ef8de9a
{
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'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment