From 4fd598786cc96f4d23fa8336969b4918f76e374c Mon Sep 17 00:00:00 2001 From: Rahul De Date: Mon, 30 Sep 2024 07:54:55 +0100 Subject: [PATCH] [pattern] optimise matching --- README.md | 2 +- src/navi/core.clj | 9 +-------- test/navi/core_test.clj | 16 ++++++++-------- 3 files changed, 10 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index e2bda20..f2ea99b 100644 --- a/README.md +++ b/README.md @@ -210,7 +210,7 @@ deps.edn used for this example: - Clojure [tools.deps](https://clojure.org/guides/getting_started) ### Running tests locally -- `clojure -M:test` to run all tests +- `clojure -X:test` to run all tests ## License diff --git a/src/navi/core.clj b/src/navi/core.clj index 17f9261..2f8d1f3 100644 --- a/src/navi/core.clj +++ b/src/navi/core.clj @@ -85,13 +85,6 @@ .getSchema schema->spec)))) -(defn matches-regex? - [exp s] - (-> exp - (re-pattern) - (re-matches s) - (some?))) - (defmulti spec (fn [^Schema schema] (or (first (.getTypes schema)) "null"))) @@ -104,7 +97,7 @@ string?) pattern (.getPattern schema)] (if pattern - [:and content-fn [:fn #(matches-regex? pattern %)]] + [:and content-fn (re-pattern pattern)] content-fn))) (defmethod spec diff --git a/test/navi/core_test.clj b/test/navi/core_test.clj index b1469da..7e1b83a 100644 --- a/test/navi/core_test.clj +++ b/test/navi/core_test.clj @@ -127,14 +127,14 @@ (is (#{[:or string? int?] [:or int? string?]} (core/schema->spec strint))))) (testing "regex string" - (let [[kw1 fn1 [kw2 fn2]] (core/schema->spec (doto (Schema.) - (.addType "string") - (.setPattern "^(\\d+)([KMGTPE]i{0,1})$")))] - (is (= :and kw1)) - (is (= string? fn1)) - (is (= :fn kw2)) - (is (fn2 "1024Ki")) - (is (not (fn2 "1024Kib")))))) + (let [[kw f regex] (core/schema->spec (doto (Schema.) + (.addType "string") + (.setPattern "^(\\d+)([KMGTPE]i{0,1})$")))] + (is (= :and kw)) + (is (= string? f)) + (is (instance? java.util.regex.Pattern regex)) + (is (some? (re-matches regex "1024Ki"))) + (is (nil? (re-matches regex "1024Kib")))))) (deftest responses-to-malli-spec (testing "empty response"