Issue 1233: product filter should only match if name begins withs search term

This commit is contained in:
Rhodri Karim
2016-12-02 23:04:43 +00:00
committed by Rob Harrington
parent 699da16049
commit 7a07e8fa16
2 changed files with 11 additions and 2 deletions

View File

@@ -5,4 +5,4 @@ Darkswarm.filter 'products', (Matcher) ->
return products if text == ""
products.filter (product) =>
propertiesToMatch = [product.name, product.variant_names, product.supplier.name, product.primary_taxon.name]
Matcher.match propertiesToMatch, text
Matcher.matchBeginning propertiesToMatch, text

View File

@@ -1,7 +1,16 @@
Darkswarm.factory "Matcher", ->
# Match text fragment in an array of strings.
new class Matcher
# Match text fragment in an array of strings.
match: (properties, text)->
properties.some (prop)->
prop ||= ""
prop.toLowerCase().indexOf(text.toLowerCase()) != -1
# Return true if text occurs at the beginning of any word present in an array of strings
matchBeginning: (properties, text) ->
text = text.trim()
properties.some (prop) ->
prop ||= ""
prop.split(' ').some (word) ->
word.toLowerCase().indexOf(text.toLowerCase()) == 0