|
296 | 296 |
|
297 | 297 | (defmethod col-filter-fn :regexp [[_ s]]
|
298 | 298 | (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)))))) |
305 | 305 |
|
306 | 306 | (defmethod col-filter-fn :ranges [[_ ranges]]
|
307 | 307 | (when-not (empty? ranges)
|
|
350 | 350 | "(?<fullvalue>" value-qq "|" value-q "|" value ")"
|
351 | 351 | ")?"))))
|
352 | 352 |
|
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 |
| - |
363 | 353 | (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))) |
378 | 369 |
|
379 | 370 | (defn parse-query [s]
|
380 | 371 | (vec
|
381 | 372 | (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 | + ]] |
397 | 391 | {:start-idx start
|
398 | 392 | :modifier modifier
|
399 | 393 | :key-quote (when fullkey
|
|
409 | 403 | :value value
|
410 | 404 | :end-idx (+ start (count full))})))
|
411 | 405 |
|
| 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 | + |
412 | 416 | (defn filter-by-query [{:as data
|
413 | 417 | :keys [rows visible-paths]} query]
|
414 | 418 | (let [names (into {}
|
415 | 419 | (for [[path idx] (map vector visible-paths (range))]
|
416 |
| - [(str/join "/" (map name path)) idx])) |
| 420 | + [(str/join "/" (map name path)) idx])) |
417 | 421 | filters (for [{:keys [modifier key value]} (parse-query query)
|
418 | 422 | :let [idx (names key)]
|
419 | 423 | :when idx]
|
420 | 424 | (cond
|
421 | 425 | (and (= "+" modifier) (nil? value))
|
422 | 426 | #(some? (nth % idx))
|
423 |
| - |
| 427 | + |
424 | 428 | (and (= "-" modifier) (nil? value))
|
425 | 429 | #(nil? (nth % idx))
|
426 |
| - |
| 430 | + |
427 | 431 | (and (= "-" modifier) value)
|
428 | 432 | #(not (str/index-of (str (nth % idx)) value))
|
429 |
| - |
| 433 | + |
430 | 434 | value
|
431 | 435 | #(str/index-of (str (nth % idx)) value)))
|
432 | 436 | filters (remove nil? filters)]
|
|
451 | 455 | active-filters (compute-filters-data opts)
|
452 | 456 | true (compute-autocomplete-data opts)
|
453 | 457 | 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))))) |
455 | 460 |
|
456 | 461 | (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)) |
459 | 464 |
|
460 | 465 | (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)) |
463 | 468 |
|
464 | 469 | (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)))) |
466 | 472 |
|
467 | 473 | (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)) |
470 | 476 |
|
471 | 477 | (defn tabular? [xs]
|
472 | 478 | (and (seqable? xs)
|
|
0 commit comments