Skip to content

Commit 89eefa0

Browse files
authored
Support cljs (#21)
1 parent 6e6711b commit 89eefa0

File tree

2 files changed

+67
-64
lines changed

2 files changed

+67
-64
lines changed

src/nextjournal/clerk_table_stats.cljc

+63-57
Original file line numberDiff line numberDiff line change
@@ -296,12 +296,12 @@
296296

297297
(defmethod col-filter-fn :regexp [[_ s]]
298298
(when (and (not (nil? s)) (not= "" s))
299-
(try
300-
(let [re (java.util.regex.Pattern/compile s)]
301-
(fn [value]
302-
(re-find re (str value))))
303-
(catch java.util.regex.PatternSyntaxException e
304-
#_(println (.getMessage e))))))
299+
(when-let [re #?(:clj (try (java.util.regex.Pattern/compile s)
300+
(catch java.util.regex.PatternSyntaxException e
301+
(println (.getMessage e))))
302+
:cljs (js/RegExp. s))]
303+
(fn [value]
304+
(re-find re (str value))))))
305305

306306
(defmethod col-filter-fn :ranges [[_ ranges]]
307307
(when-not (empty? ranges)
@@ -350,50 +350,44 @@
350350
"(?<fullvalue>" value-qq "|" value-q "|" value ")"
351351
")?"))))
352352

353-
(comment
354-
(doseq [modifier ["" "+" "-"]
355-
key ["key" "'a key'" "'a:key'" "\"a key\"" "\"a:key\""]
356-
delim [nil ":"]
357-
value (if delim
358-
[nil "value" "a:value" "'a value'" "'a:value'" "\"a value\"" "\"a:value\""]
359-
[nil])
360-
:let [s (str modifier key delim value)]]
361-
(prn s #_(match-all filter-regexp s) (parse-query s))))
362-
363353
(defn match-all [re s]
364-
(let [m ^java.util.regex.Matcher (re-matcher re s)]
365-
(loop [acc (transient [])]
366-
(if (.find m)
367-
(recur (conj! acc
368-
{:groups (mapv #(.group m %) (range (.groupCount m)))
369-
:named (persistent!
370-
(reduce-kv
371-
(fn [acc k i]
372-
(assoc! acc (keyword k) (.group m ^int i)))
373-
(transient {})
374-
(.namedGroups m)))
375-
:start (.start m)
376-
:end (.end m)}))
377-
(persistent! acc)))))
354+
#? (:clj (let [m ^java.util.regex.Matcher (re-matcher re s)]
355+
(loop [acc (transient [])]
356+
(if (.find m)
357+
(recur (conj! acc
358+
{:groups (mapv #(.group m %) (range (.groupCount m)))
359+
:named (persistent!
360+
(reduce-kv
361+
(fn [acc k i]
362+
(assoc! acc (keyword k) (.group m ^int i)))
363+
(transient {})
364+
(.namedGroups m)))
365+
:start (.start m)
366+
:end (.end m)}))
367+
(persistent! acc))))
368+
:cljs (.matchAll s re)))
378369

379370
(defn parse-query [s]
380371
(vec
381372
(for [match (match-all filter-regexp s)
382-
:let [groups (:named match)
383-
start (:start match)
384-
full (-> match :groups first)
385-
modifier (-> groups :modifier not-blank)
386-
key (or
387-
(-> groups :keyqq not-blank)
388-
(-> groups :keyq not-blank)
389-
(-> groups :key not-blank))
390-
fullkey (-> groups :fullkey not-blank)
391-
delimeter (-> groups :delimeter not-blank)
392-
value (or
393-
(-> groups :valueqq not-blank)
394-
(-> groups :valueq not-blank)
395-
(-> groups :value not-blank))
396-
fullvalue (-> groups :fullvalue not-blank)]]
373+
:let [groups #?(:clj (:named match) :cljs (.-groups match))
374+
start #?(:clj (:start match) :cljs (.-index match))
375+
full #?(:clj (-> match :groups first) :cljs (aget match 0))
376+
modifier #?(:clj (-> groups :modifier not-blank) :cljs (-> groups .-modifier not-blank))
377+
key #?(:clj (or
378+
(-> groups :keyqq not-blank)
379+
(-> groups :keyq not-blank)
380+
(-> groups :key not-blank))
381+
:cljs (-> groups .-key not-blank))
382+
fullkey #?(:clj (-> groups :fullkey not-blank) :cljs nil);;TODO
383+
delimeter #?(:clj (-> groups :delimeter not-blank) :cljs (-> groups .-fullkey not-blank))
384+
value #?(:clj (or
385+
(-> groups :valueqq not-blank)
386+
(-> groups :valueq not-blank)
387+
(-> groups :value not-blank))
388+
:cljs (-> groups .-value not-blank))
389+
fullvalue #?(:clj (-> groups :fullvalue not-blank) :cljs nil) ;;TODO
390+
]]
397391
{:start-idx start
398392
:modifier modifier
399393
:key-quote (when fullkey
@@ -409,24 +403,34 @@
409403
:value value
410404
:end-idx (+ start (count full))})))
411405

406+
(comment
407+
(doseq [modifier ["" "+" "-"]
408+
key ["key" "'a key'" "'a:key'" "\"a key\"" "\"a:key\""]
409+
delim [nil ":"]
410+
value (if delim
411+
[nil "value" "a:value" "'a value'" "'a:value'" "\"a value\"" "\"a:value\""]
412+
[nil])
413+
:let [s (str modifier key delim value)]]
414+
(prn s #_(match-all filter-regexp s) (parse-query s))))
415+
412416
(defn filter-by-query [{:as data
413417
:keys [rows visible-paths]} query]
414418
(let [names (into {}
415419
(for [[path idx] (map vector visible-paths (range))]
416-
[(str/join "/" (map name path)) idx]))
420+
[(str/join "/" (map name path)) idx]))
417421
filters (for [{:keys [modifier key value]} (parse-query query)
418422
:let [idx (names key)]
419423
:when idx]
420424
(cond
421425
(and (= "+" modifier) (nil? value))
422426
#(some? (nth % idx))
423-
427+
424428
(and (= "-" modifier) (nil? value))
425429
#(nil? (nth % idx))
426-
430+
427431
(and (= "-" modifier) value)
428432
#(not (str/index-of (str (nth % idx)) value))
429-
433+
430434
value
431435
#(str/index-of (str (nth % idx)) value)))
432436
filters (remove nil? filters)]
@@ -451,22 +455,24 @@
451455
active-filters (compute-filters-data opts)
452456
true (compute-autocomplete-data opts)
453457
true (update :rows #(filter row-filter-fn %))
454-
search-query (filter-by-query search-query)))))
458+
search-query #?(:clj (filter-by-query search-query)
459+
:cljs identity)))))
455460

456461
(def table-markup-viewer
457-
{:require-cljs true
458-
:render-fn `render/table-markup-viewer})
462+
(assoc viewer/table-markup-viewer
463+
:render-fn `render/render-table-markup))
459464

460465
(def table-head-viewer
461-
{:require-cljs true
462-
:render-fn `render/table-head-viewer})
466+
(assoc viewer/table-head-viewer
467+
:render-fn `render/render-table-head))
463468

464469
(def table-body-viewer
465-
{:render-fn '(fn [rows opts] (into [:tbody] (map-indexed (fn [idx row] (nextjournal.clerk.render/inspect-presented (update opts :path conj idx) row))) rows))})
470+
(assoc viewer/table-body-viewer
471+
:render-fn '(fn [rows opts] (into [:tbody] (map-indexed (fn [idx row] (nextjournal.clerk.render/inspect-presented (update opts :path conj idx) row))) rows))))
466472

467473
(def table-row-viewer
468-
{:require-cljs true
469-
:render-fn `render/table-row-viewer})
474+
(assoc viewer/table-row-viewer
475+
:render-fn `render/render-table-row))
470476

471477
(defn tabular? [xs]
472478
(and (seqable? xs)

src/nextjournal/clerk_table_stats/render.cljs

+4-7
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@
447447
[icon-checkmark])]
448448
name])]]])))
449449

450-
(defn table-head-viewer
450+
(defn render-table-head
451451
[header-row {:as opts :keys [table-state]}]
452452
(let [cells* (viewer/desc->values header-row)
453453
cells (mapcat #(if (vector? %)
@@ -538,7 +538,7 @@
538538
:idx idx}])])])
539539
sub-headers)))]))
540540

541-
(defn table-row-viewer
541+
(defn render-table-row
542542
[row {:as opts :keys [path number-col?]}]
543543
(into [:tr.print:border-b-gray-500.hover:bg-slate-200.print:hover:bg-transparent.group
544544
{:class (str "print:border-b-[1px] "
@@ -834,11 +834,8 @@
834834
label])]])
835835
js/document.body))])))
836836

837-
(defn table-markup-viewer [head+body {:as opts :keys [sync-var]}]
838-
(r/with-let [table-state (if sync-var
839-
(deref sync-var)
840-
#?(:clj (throw (js/Error. (str "no sync var: " sync-var)))
841-
:cljs nil))]
837+
(defn render-table-markup [head+body {:as opts :keys [sync-var]}]
838+
(r/with-let [table-state (when sync-var (deref sync-var))]
842839
[:div
843840
[table-search head+body table-state opts]
844841
[:div.bg-white.rounded.border.border-slate-300.shadow-sm.font-sans.text-sm.not-prose.overflow-x-auto

0 commit comments

Comments
 (0)