Files
openfoodnetwork/app/assets/javascripts/darkswarm/services/matcher.js.coffee
2021-08-02 18:59:42 +01:00

15 lines
545 B
CoffeeScript

angular.module('Darkswarm').factory "Matcher", ->
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()
regexp = new RegExp("(?:^|[\\s-])#{text}", "i")
properties.some (prop) -> (prop || "").search(regexp) >= 0