From a1ffc869f32894b404ba3f147e5449f3fbfd735c Mon Sep 17 00:00:00 2001 From: Julius Pabrinkis Date: Fri, 21 Apr 2017 12:10:40 +0100 Subject: [PATCH] Refactor code by suggestion to use RegExp and add tests coverage --- .../darkswarm/services/matcher.js.coffee | 6 +-- .../darkswarm/services/matcher_spec.js.coffee | 54 +++++++++++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 spec/javascripts/unit/darkswarm/services/matcher_spec.js.coffee diff --git a/app/assets/javascripts/darkswarm/services/matcher.js.coffee b/app/assets/javascripts/darkswarm/services/matcher.js.coffee index 73abbbc1f6..db871f6a0d 100644 --- a/app/assets/javascripts/darkswarm/services/matcher.js.coffee +++ b/app/assets/javascripts/darkswarm/services/matcher.js.coffee @@ -10,7 +10,5 @@ Darkswarm.factory "Matcher", -> # 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 + regexp = new RegExp("(?:^|[\\s-])#{text}", "i") + properties.some (prop) -> (prop || "").search(regexp) >= 0 diff --git a/spec/javascripts/unit/darkswarm/services/matcher_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/matcher_spec.js.coffee new file mode 100644 index 0000000000..4da9cb506e --- /dev/null +++ b/spec/javascripts/unit/darkswarm/services/matcher_spec.js.coffee @@ -0,0 +1,54 @@ +describe 'Matcher service', -> + Matcher = null + + beforeEach -> + module 'Darkswarm' + inject ($injector)-> + Matcher = $injector.get("Matcher") + + describe '#match', -> + it "matches full word", -> + expect(Matcher.match ["product_name"], "product_name").toEqual true + + it "matches second word", -> + expect(Matcher.match ["product_name", 'variant'], "variant").toEqual true + + it "matches word with underscore", -> + expect(Matcher.match ["product_name"], "ct_na").toEqual true + + it "matches word if property has two words", -> + expect(Matcher.match ["product name"], "nam").toEqual true + + it "matches word with dash", -> + expect(Matcher.match ["product-name"], "ct-na").toEqual true + + it "finds in any part of properties", -> + expect(Matcher.match ["keyword"], "word").toEqual true + + it "finds beginning of property", -> + expect(Matcher.match ["keyword"], "key").toEqual true + + it "doesn't find non-sense or mistypes", -> + expect(Matcher.match ["keyword"], "keywrd").toEqual false + + describe '#matchBeginning', -> + it "matches full word", -> + expect(Matcher.matchBeginning ["product_name"], "product_name").toEqual true + + it "matches second word", -> + expect(Matcher.matchBeginning ["product_name", 'variant'], "variant").toEqual true + + it "matches word if property has two words", -> + expect(Matcher.matchBeginning ["product name"], "nam").toEqual true + + it "matches second part of word separated by dash", -> + expect(Matcher.matchBeginning ["product-name"], "name").toEqual true + + it "matches beginning of property", -> + expect(Matcher.matchBeginning ["keyword"], "key").toEqual true + + it "doesn't match in any part of property", -> + expect(Matcher.matchBeginning ["keyword"], "word").toEqual false + + it "doesn't match non-sense or mistypes", -> + expect(Matcher.matchBeginning ["keyword"], "keywrd").toEqual false