History

Normalize search values and search queries the same way

Thibaut committed 11 years ago

Commit add213e1 parent ec9cb102 files · +23 −20Browse files

Unified · Split

2 files
  1. assets/javascripts/app/searcher.coffee +20 −4
  2. assets/javascripts/models/entry.coffee +3 −16
assets/javascripts/app/searcher.coffee+20−4
@@ -112,6 +112,25 @@ class app.Searcher · 111 unmodified lines
112 max_results: app.config.max_results112 max_results: app.config.max_results
113 fuzzy_min_length: 3113 fuzzy_min_length: 3
114 114
115 SEPARATORS_REGEXP = /\:?\ |#|::|->/g
116 PARANTHESES_REGEXP = /\(.*?\)$/
117 EVENT_REGEXP = /\ event$/
118 DOT_REGEXP = /\.+/g
119 WHITESPACE_REGEXP = /\s/g
120
121 EMPTY_STRING = ''
122 ELLIPSIS = '...'
123
124 @normalizeString: (string) ->
125 string
126 .toLowerCase()
127 .replace ELLIPSIS, EMPTY_STRING
128 .replace EVENT_REGEXP, EMPTY_STRING
129 .replace SEPARATORS_REGEXP, SEPARATOR
130 .replace DOT_REGEXP, SEPARATOR
131 .replace PARANTHESES_REGEXP, EMPTY_STRING
132 .replace WHITESPACE_REGEXP, EMPTY_STRING
133
115 constructor: (options = {}) ->134 constructor: (options = {}) ->
116 @options = $.extend {}, DEFAULTS, options135 @options = $.extend {}, DEFAULTS, options
117 136
@@ -127,7 +146,7 @@ class app.Searcher · 9 unmodified lines
127 return146 return
128 147
129 setup: ->148 setup: ->
130 query = @query = @normalizeQuery(@query)149 query = @query = @constructor.normalizeString(@query)
131 queryLength = query.length150 queryLength = query.length
132 @dataLength = @data.length151 @dataLength = @data.length
133 @matchers = [exactMatch]152 @matchers = [exactMatch]
@@ -231,9 +250,6 @@ class app.Searcher · 97 unmodified lines
231 delay: (fn) ->250 delay: (fn) ->
232 @timeout = setTimeout(fn, 1)251 @timeout = setTimeout(fn, 1)
233 252
234 normalizeQuery: (string) ->
235 string.replace(/\s/g, '').toLowerCase()
236
237 queryToFuzzyRegexp: (string) ->253 queryToFuzzyRegexp: (string) ->
238 chars = string.split ''254 chars = string.split ''
239 chars[i] = $.escapeRegexp(char) for char, i in chars255 chars[i] = $.escapeRegexp(char) for char, i in chars
assets/javascripts/models/entry.coffee+3−16
@@ -1,24 +1,11 @@
1#= require app/searcher
2
1class app.models.Entry extends app.Model3class app.models.Entry extends app.Model
2 # Attributes: name, type, path4 # Attributes: name, type, path
3 5
4 SEPARATORS_REGEXP = /\:?\ |#|::|->/g
5 PARANTHESES_REGEXP = /\(.*?\)$/
6 EVENT_REGEXP = /\ event$/
7 DOT_REGEXP = /\.+/g
8
9 constructor: ->6 constructor: ->
10 super7 super
11 @text = @searchValue()8 @text = app.Searcher.normalizeString(@name)
12
13 searchValue: ->
14 @name
15 .toLowerCase()
16 .replace '...', ' '
17 .replace EVENT_REGEXP, ''
18 .replace SEPARATORS_REGEXP, '.'
19 .replace DOT_REGEXP, '.'
20 .replace PARANTHESES_REGEXP, ''
21 .trim()
22 9
23 fullPath: ->10 fullPath: ->
24 @doc.fullPath if @isIndex() then '' else @path11 @doc.fullPath if @isIndex() then '' else @path