Util for building strings | (ns metabase.util.string (:require [clojure.string :as str] [metabase.util.i18n :refer [deferred-tru]])) |
(set! *warn-on-reflection* true) | |
Join parts of a sentence together to build a compound one. Options: - stop? (default true): whether to add a period at the end of the sentence Examples: (build-sentence ["foo" "bar" "baz"]) => "foo, bar and baz." (build-sentence ["foo" "bar" "baz"] :stop? false) => "foo, bar and baz" Note: this assumes we're building a sentence with parts from left to right, It might not works correctly with right-to-left language. Also not all language uses command and "and" to represting 'listing'. | (defn build-sentence
([parts]
(build-sentence parts :stop? true))
([parts & {:keys [stop?]
:or {stop? true}
:as options}]
(when (seq parts)
(cond
(= (count parts) 1) (str (first parts) (when stop? \.))
(= (count parts) 2) (str (first parts) " " (deferred-tru "and") " " (second parts) (when stop? \.))
:else (str (first parts) ", " (build-sentence (rest parts) options)))))) |
Mask string value behind 'start...end' representation. First four and last four symbols are shown. Even less if string is shorter than 8 chars. | (defn mask
([s]
(mask s 4))
([s start-limit]
(mask s start-limit 4))
([s start-limit end-limit]
(if (str/blank? s)
s
(let [cnt (count s)]
(str
(subs s 0 (max 1 (min start-limit (- cnt 2))))
"..."
(when (< (+ end-limit start-limit) cnt)
(subs s (- cnt end-limit) cnt))))))) |
True if the given string is formatted like a UUID | (defn valid-uuid?
[s] (try (java.util.UUID/fromString s) true
(catch Exception _e false))) |
Elides the string to the specified length, adding '...' if it exceeds that length. | (defn elide
[s max-length]
(if (> (count s) max-length)
(str (subs s 0 (- max-length 3)) "...")
s)) |