Преглед изворни кода

Normalize search values and search queries the same way

Thibaut пре 10 година
родитељ
комит
add213e0f4
2 измењених фајлова са 23 додато и 20 уклоњено
  1. 20 4
      assets/javascripts/app/searcher.coffee
  2. 3 16
      assets/javascripts/models/entry.coffee

+ 20 - 4
assets/javascripts/app/searcher.coffee

@@ -112,6 +112,25 @@ class app.Searcher
     max_results: app.config.max_results
     fuzzy_min_length: 3
 
+  SEPARATORS_REGEXP = /\:?\ |#|::|->/g
+  PARANTHESES_REGEXP = /\(.*?\)$/
+  EVENT_REGEXP = /\ event$/
+  DOT_REGEXP = /\.+/g
+  WHITESPACE_REGEXP = /\s/g
+
+  EMPTY_STRING = ''
+  ELLIPSIS = '...'
+
+  @normalizeString: (string) ->
+    string
+      .toLowerCase()
+      .replace ELLIPSIS, EMPTY_STRING
+      .replace EVENT_REGEXP, EMPTY_STRING
+      .replace SEPARATORS_REGEXP, SEPARATOR
+      .replace DOT_REGEXP, SEPARATOR
+      .replace PARANTHESES_REGEXP, EMPTY_STRING
+      .replace WHITESPACE_REGEXP, EMPTY_STRING
+
   constructor: (options = {}) ->
     @options = $.extend {}, DEFAULTS, options
 
@@ -127,7 +146,7 @@ class app.Searcher
     return
 
   setup: ->
-    query = @query = @normalizeQuery(@query)
+    query = @query = @constructor.normalizeString(@query)
     queryLength = query.length
     @dataLength = @data.length
     @matchers = [exactMatch]
@@ -231,9 +250,6 @@ class app.Searcher
   delay: (fn) ->
     @timeout = setTimeout(fn, 1)
 
-  normalizeQuery: (string) ->
-    string.replace(/\s/g, '').toLowerCase()
-
   queryToFuzzyRegexp: (string) ->
     chars = string.split ''
     chars[i] = $.escapeRegexp(char) for char, i in chars

+ 3 - 16
assets/javascripts/models/entry.coffee

@@ -1,24 +1,11 @@
+#= require app/searcher
+
 class app.models.Entry extends app.Model
   # Attributes: name, type, path
 
-  SEPARATORS_REGEXP = /\:?\ |#|::|->/g
-  PARANTHESES_REGEXP = /\(.*?\)$/
-  EVENT_REGEXP = /\ event$/
-  DOT_REGEXP = /\.+/g
-
   constructor: ->
     super
-    @text = @searchValue()
-
-  searchValue: ->
-    @name
-      .toLowerCase()
-      .replace '...', ' '
-      .replace EVENT_REGEXP, ''
-      .replace SEPARATORS_REGEXP, '.'
-      .replace DOT_REGEXP, '.'
-      .replace PARANTHESES_REGEXP, ''
-      .trim()
+    @text = app.Searcher.normalizeString(@name)
 
   fullPath: ->
     @doc.fullPath if @isIndex() then '' else @path