util.cljs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. (ns frontend.worker.util
  2. "Worker utils"
  3. (:require [clojure.string :as string]
  4. ["remove-accents" :as removeAccents]
  5. [medley.core :as medley]
  6. [logseq.graph-parser.util :as gp-util]
  7. [goog.string :as gstring]))
  8. (defonce db-version-prefix "logseq_db_")
  9. (defonce local-db-prefix "logseq_local_")
  10. (defn db-based-graph?
  11. [s]
  12. (boolean
  13. (and (string? s)
  14. (string/starts-with? s db-version-prefix))))
  15. (defn local-file-based-graph?
  16. [s]
  17. (and (string? s)
  18. (string/starts-with? s local-db-prefix)))
  19. (defn search-normalize
  20. "Normalize string for searching (loose)"
  21. [s remove-accents?]
  22. (when s
  23. (let [normalize-str (.normalize (string/lower-case s) "NFKC")]
  24. (if remove-accents?
  25. (removeAccents normalize-str)
  26. normalize-str))))
  27. (defn safe-re-find
  28. {:malli/schema [:=> [:cat :any :string] [:or :nil :string [:vector [:maybe :string]]]]}
  29. [pattern s]
  30. (when-not (string? s)
  31. ;; TODO: sentry
  32. (js/console.trace))
  33. (when (string? s)
  34. (re-find pattern s)))
  35. (def uuid-pattern "[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}")
  36. (defonce exactly-uuid-pattern (re-pattern (str "(?i)^" uuid-pattern "$")))
  37. (defn uuid-string?
  38. {:malli/schema [:=> [:cat :string] :boolean]}
  39. [s]
  40. (boolean (safe-re-find exactly-uuid-pattern s)))
  41. (def page-name-sanity-lc
  42. "Delegate to gp-util to loosely couple app usages to graph-parser"
  43. gp-util/page-name-sanity-lc)
  44. (defn safe-page-name-sanity-lc
  45. [s]
  46. (if (string? s)
  47. (page-name-sanity-lc s) s))
  48. (defn distinct-by
  49. [f col]
  50. (medley/distinct-by f (seq col)))
  51. (defn format
  52. [fmt & args]
  53. (apply gstring/format fmt args))
  54. (defn remove-first [pred coll]
  55. ((fn inner [coll]
  56. (lazy-seq
  57. (when-let [[x & xs] (seq coll)]
  58. (if (pred x)
  59. xs
  60. (cons x (inner xs))))))
  61. coll))