From 7a07e8fa16b6fee9b5486d184a7e73d1f587ab64 Mon Sep 17 00:00:00 2001 From: Rhodri Karim Date: Fri, 2 Dec 2016 23:04:43 +0000 Subject: [PATCH] Issue 1233: product filter should only match if name begins withs search term --- .../darkswarm/filters/filter_products.js.coffee | 2 +- .../javascripts/darkswarm/services/matcher.js.coffee | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/darkswarm/filters/filter_products.js.coffee b/app/assets/javascripts/darkswarm/filters/filter_products.js.coffee index 18402edc02..3e9f0b4821 100644 --- a/app/assets/javascripts/darkswarm/filters/filter_products.js.coffee +++ b/app/assets/javascripts/darkswarm/filters/filter_products.js.coffee @@ -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 diff --git a/app/assets/javascripts/darkswarm/services/matcher.js.coffee b/app/assets/javascripts/darkswarm/services/matcher.js.coffee index 9360afdd1f..73abbbc1f6 100644 --- a/app/assets/javascripts/darkswarm/services/matcher.js.coffee +++ b/app/assets/javascripts/darkswarm/services/matcher.js.coffee @@ -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