From 75065b56e394914ebc8ff063d6f08c27c277217a Mon Sep 17 00:00:00 2001 From: Tommi Reiman Date: Mon, 14 Jan 2019 16:38:45 +0200 Subject: [PATCH] Public docs for reitit.segment --- modules/reitit-core/src/reitit/segment.cljc | 22 ++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/modules/reitit-core/src/reitit/segment.cljc b/modules/reitit-core/src/reitit/segment.cljc index b8a6be5f..d51022df 100644 --- a/modules/reitit-core/src/reitit/segment.cljc +++ b/modules/reitit-core/src/reitit/segment.cljc @@ -48,16 +48,20 @@ ;; public api ;; -(defn insert [root path data] - #?(:cljs (-insert (or root (segment)) (impl/segments path) (map->Match {:data data})) - :clj (.add (or ^SegmentTrie root ^SegmentTrie (SegmentTrie.)) ^String path data))) +(defn insert + "Returns a Segment Trie with path with data inserted into it. Creates the trie if `nil`." + [trie path data] + #?(:cljs (-insert (or trie (segment)) (impl/segments path) (map->Match {:data data})) + :clj (.add (or ^SegmentTrie trie ^SegmentTrie (SegmentTrie.)) ^String path data))) -(defn compile [segment] - #?(:cljs segment - :clj (.matcher ^SegmentTrie (or segment (SegmentTrie.))))) +(defn compile [trie] + "Compiles the Trie so that [[lookup]] can be used." + #?(:cljs trie + :clj (.matcher ^SegmentTrie (or trie (SegmentTrie.))))) -(defn lookup [segment path] - #?(:cljs (if-let [match (-lookup segment (impl/segments path) {})] +(defn lookup [trie path] + "Looks the path from a Segment Trie. Returns a [[Match]] or `nil`." + #?(:cljs (if-let [match (-lookup trie (impl/segments path) {})] (assoc match :path-params (impl/url-decode-coll (:path-params match)))) - :clj (if-let [match ^SegmentTrie$Match (SegmentTrie/lookup segment path)] + :clj (if-let [match ^SegmentTrie$Match (SegmentTrie/lookup trie path)] (->Match (.data match) (clojure.lang.PersistentHashMap/create (.params match))))))