Skip to content

Instantly share code, notes, and snippets.

@ajchemist
Created September 14, 2016 14:53
Show Gist options
  • Save ajchemist/3a03e084c70bcf0ad455f299f45ae161 to your computer and use it in GitHub Desktop.
Save ajchemist/3a03e084c70bcf0ad455f299f45ae161 to your computer and use it in GitHub Desktop.
namespace string shortener
(defn ns-str-shortener
([ns-str] ns-str)
([ns-str number]
(let [length (.length ns-str)]
(if (< number length)
(let [splitted (.split #"\." ns-str)
endi (dec (alength splitted))
short (volatile! false)]
(areduce splitted i ret ""
(let [j (- endi i)
comp (aget splitted j)
length (+ (.length ret) (.length comp) 1)
comp (cond
@short (subs comp 0 1)
(== i 0) comp
(< number length) (do (vreset! short true)
(subs comp 0 1))
:else comp)]
(str (when-not (== j 0) ".") comp ret))))
ns-str))))
(ns-str-shortener "namespace.somewhat.long.example" 16)
;; => "n.s.long.example"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment