Add tests for query syntax
This commit is contained in:
parent
934c499e93
commit
5452579f0d
4 changed files with 85 additions and 15 deletions
|
|
@ -1,16 +1,23 @@
|
|||
(ns computersandblues.lodestone.match
|
||||
(:require [applied-science.js-interop :as j]))
|
||||
|
||||
(defn ->regex [s]
|
||||
(defn- ->regex
|
||||
"Does its best to compila a stirng into a regular expression; will fall back
|
||||
to return a RegExp that matches verbatim."
|
||||
[s]
|
||||
(try
|
||||
(js/RegExp. s "i")
|
||||
(catch js/Error _
|
||||
(js/RegExp. (js/RegExp.escape s) "i"))))
|
||||
|
||||
(defn query->tokens [query]
|
||||
(->> (re-seq #"\"([^\"]+)\"|([^\s]+)" query)
|
||||
(mapv (fn [[_ a b]] (or a b)))))
|
||||
|
||||
(defn query->matching-fn [query]
|
||||
(let [match? (if query
|
||||
(partial re-find (->regex query))
|
||||
(constantly true))]
|
||||
(partial re-find (->regex query))
|
||||
(constantly true))]
|
||||
(fn [post]
|
||||
(or (match? (j/get post :content))
|
||||
(match? (j/get-in post [:account :acct])) ; search for url + username of poster
|
||||
|
|
|
|||
13
src/computersandblues/lodestone/match_test.cljs
Normal file
13
src/computersandblues/lodestone/match_test.cljs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(ns computersandblues.lodestone.match-test
|
||||
(:require [cljs.test :refer-macros [deftest testing is]]
|
||||
[computersandblues.lodestone.match :as match]))
|
||||
|
||||
(def fixtures
|
||||
(clj->js []))
|
||||
|
||||
(deftest query->tokens
|
||||
(testing "split words on whitespace"
|
||||
(is (= ["foo" "bar"] (match/query->tokens "foo bar")))
|
||||
(is (= ["xxxx" "yyyyy"] (match/query->tokens "xxxx yyyyy"))))
|
||||
(testing "quotes can be used to group tokens"
|
||||
(is (= ["foo" "bar baz"] (match/query->tokens "foo \"bar baz\"")))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue