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
112112 max_results: app.config.max_results
113113 fuzzy_min_length: 3
114114
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+
115134 constructor: (options = {}) ->
116135 @options = $.extend {}, DEFAULTS, options
117136
@@ -127,7 +146,7 @@ class app.Searcher · 9 unmodified lines
127146 return
128147
129148 setup: ->
130− query = @query = @normalizeQuery(@query)
149+ query = @query = @constructor.normalizeString(@query)
131150 queryLength = query.length
132151 @dataLength = @data.length
133152 @matchers = [exactMatch]
@@ -231,9 +250,6 @@ class app.Searcher · 97 unmodified lines
231250 delay: (fn) ->
232251 @timeout = setTimeout(fn, 1)
233252
234− normalizeQuery: (string) ->
235− string.replace(/\s/g, '').toLowerCase()
236−
237253 queryToFuzzyRegexp: (string) ->
238254 chars = string.split ''
239255 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+
13 class app.models.Entry extends app.Model
24 # Attributes: name, type, path
35
4− SEPARATORS_REGEXP = /\:?\ |#|::|->/g
5− PARANTHESES_REGEXP = /\(.*?\)$/
6− EVENT_REGEXP = /\ event$/
7− DOT_REGEXP = /\.+/g
8−
96 constructor: ->
107 super
11− @text = @searchValue()
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()
8+ @text = app.Searcher.normalizeString(@name)
229
2310 fullPath: ->
2411 @doc.fullPath if @isIndex() then '' else @path