hara.string methods for string manipulation

Author: Chris Zheng  (z@caudate.me)
Date: 29 June 2017
Repository: https://github.com/zcaudate/hara
Version: 2.5.10

hara.string contain functions and utilities for strings.

1    string.case

Add to project.clj dependencies:

[im.chit/hara.string.case "2.5.10"]

hara.string.case provides methods for changing case representation



camel-case ^

converts a string-like object to camel case representation

v 2.1
(defn camel-case
  [value]
  (let [meta (common/to-meta value)
        value (common/to-string value)]
    (->> #(string/upper-case (apply str (rest %)))
         (re-sub value non-camel-separator-pattern)
         (common/from-string meta ))))
link
(camel-case :hello-world) => :helloWorld (camel-case 'hello_world) => 'helloWorld

lower-case ^

converts a string-like object to a lower case string

v 2.1
(defn lower-case
  [value]
  (->> (-> (common/to-string value)
           (separate-camel-humps)
           (string/split #"[ |-|_]"))
       (map string/lower-case)
       (string/join " ")))
link
(lower-case "helloWorld") => "hello world" (lower-case 'hello-world) => "hello world"

re-sub ^

NONE
(defn re-sub
  [^String value pattern sub-func]
  (loop [matcher (re-matcher pattern value)
         result []
         last-end 0]
    (if (.find matcher)
      (recur matcher
             (conj result
                   (.substring value last-end (.start matcher))
                   (sub-func (re-groups matcher)))
             (.end matcher))
      (apply str (conj result (.substring value last-end))))))
example not found

separate-camel-humps ^

NONE
(defn separate-camel-humps
  [value]
  (re-sub value hump-pattern #(string/join " " (seq %))))
example not found

snake-case ^

converts a string-like object to snake case representation

v 2.1
(defn snake-case
  [value]
  (let [meta (common/to-meta value)
        value (common/to-string value)]
    (-> (separate-camel-humps value)
        (string/lower-case)
        (string/replace non-snake-separator-pattern "_")
        (->> (common/from-string meta)))))
link
(snake-case :hello-world) => :hello_world (snake-case 'helloWorld) => 'hello_world

spear-case ^

converts a string-like object to spear case representation

v 2.1
(defn spear-case
  [value]
  (let [meta (common/to-meta value)
        value (common/to-string value)]
    (-> (separate-camel-humps value)
        (string/lower-case)
        (string/replace non-spear-separator-pattern "-")
        (->> (common/from-string meta)))))
link
(spear-case :hello_world) => :hello-world (spear-case 'helloWorld) => 'hello-world

title-case ^

converts a string-like object to a title case string

v 2.1
(defn title-case
  [value]
  (->> (-> (common/to-string value)
           (separate-camel-humps)
           (string/split #"[ |-|_]"))
       (map string/capitalize)
       (string/join " ")))
link
(title-case "helloWorld") => "Hello World" (title-case :hello-world) => "Hello World"

2    string.path

Add to project.clj dependencies:

[im.chit/hara.string.path "2.5.10"]

hara.string.path provides methods for manipulating paths in string like objects



contains ^

check that a path contains the subkey

v 2.1
(defn contains
  [k subk]
  (or (= k subk)
      (.startsWith (common/to-string k)
                   (str (common/to-string subk) *default-seperator*))))
link
(path/contains :hello/world :hello) => true (path/contains "a/b/c/d" "a/b/c") => true

join ^

joins a sequence of elements into a path seperated value

v 2.1
(defn join
  ([ks] (join ks *default-seperator*))
  ([ks sep]
     (if (empty? ks) nil
         (let [meta (common/to-meta (first ks))]
           (->> (filter identity ks)
                (map common/to-string)
                (string/join sep)
                (common/from-string meta))))))
link
(path/join ["a" "b" "c"]) => "a/b/c" (path/join '[a b c] '-) => 'a-b-c

make-pattern ^

NONE
(defn make-pattern [s]
  (-> s
      (.replaceAll "\." "\\\.")
      (.replaceAll "\*" "\\\*")
      (re-pattern)))
example not found

path-ns ^

returns the path namespace of the string/keyword/symbol

v 2.1
(defn path-ns
  [k]
  (join (path-vec k)))
link
(path/path-ns "a/b/c/d") => "a/b/c"

path-ns? ^

check for the path namespace of the string/keyword/symbol

v 2.1
(defn path-ns?
  ([k] (< 0 (.indexOf (str k) *default-seperator*)))
  ([k ns] (if-let [tkns (path-ns k)]
            (= 0 (.indexOf (str k)
                 (str ns *default-seperator*)))
            (nil? ns))))
link
(path/path-ns? "a/b/c/d" "a/b/c") => true

path-root ^

returns the path root of the string/keyword/symbol

v 2.1
(defn path-root
  [k]
  (first (path-vec k)))
link
(path/path-root "a/b/c/d") => "a"

path-root? ^

check for the path root of the string/keyword/symbol

v 2.1
(defn path-root?
  [k pk]
  (= pk (path-root k)))
link
(path/path-root? "a/b/c/d" "a") => true

path-stem ^

returns the path stem of the string/keyword/symbol

v 2.1
(defn path-stem
  [k]
  (join (path-stem-vec k)))
link
(path/path-stem "a/b/c/d") => "b/c/d"

path-stem-vec ^

returns the path stem vector of the string/keyword/symbol

v 2.1
(defn path-stem-vec
  [k]
  (rest (split k)))
link
(path/path-stem-vec "a/b/c/d") => ["b" "c" "d"]

path-stem-vec? ^

check for the path stem vector of the string/keyword/symbol

v 2.1
(defn path-stem-vec?
  [k kv]
  (= kv (path-stem-vec k)))
link
(path/path-stem-vec? "a/b/c/d" ["b" "c" "d"]) => true

path-stem? ^

check for the path stem of the string/keyword/symbol

v 2.1
(defn path-stem?
  [k ks]
  (= ks (path-stem k)))
link
(path/path-stem? "a/b/c/d" "b/c/d") => true

path-vec ^

returns the path vector of the string/keyword/symbol

v 2.1
(defn path-vec
  [k]
  (or (butlast (split k)) []))
link
(path/path-vec "a/b/c/d") => ["a" "b" "c"]

path-vec? ^

check for the path vector of the string/keyword/symbol

v 2.1
(defn path-vec?
  [k pv]
  (= pv (path-vec k)))
link
(path/path-vec? "a/b/c/d" ["a" "b" "c"]) => true

split ^

splits a sequence of elements into a path seperated value

v 2.1
(defn split
  ([k] (split k (make-pattern *default-seperator*)))
  ([k re]
     (cond (nil? k) []

           :else
           (let [meta (common/to-meta k)]
             (mapv #(common/from-string meta %)
                   (string/split (common/to-string k) re))))))
link
(path/split :hello/world) => [:hello :world] (path/split "a/b/c/d") => '["a" "b" "c" "d"]

val ^

returns the val of the string/keyword/symbol

v 2.1
(defn val
  [k]
  (last (split k)))
link
(path/val "a/b/c/d") => "d"

val? ^

check for the val of the string/keyword/symbol

v 2.1
(defn val?
  [k z]
  (= z (val k)))
link
(path/val? "a/b/c/d" "d") => true

3    string.prose

Add to project.clj dependencies:

[im.chit/hara.string.path "2.5.10"]

hara.string.prose provides methods for general manipulation of text



escape-dollars ^

for regex purposes, escape dollar signs in strings

v 2.4
(defn escape-dollars
  [^String s]
  (.replaceAll s "\$" "\\\$"))
link
(escape-dollars "$") => string?

escape-escapes ^

makes sure that newlines are printable

v 2.4
(defn escape-escapes
  [^String s]
  (.replaceAll s "(\\)([A-Za-z])" "$1$1$2"))
link
(escape-escapes "\n") => "\n"

escape-newlines ^

makes sure that newlines are printable

v 2.4
(defn escape-newlines
  [^String s]
  (.replaceAll s "\n" "\\n"))
link
(escape-newlines "\n") => "n"

escape-quotes ^

makes sure that quotes are printable in string form

v 2.4
(defn escape-quotes
  [^String s]
  (.replaceAll s "(\\)?"" "$1$1\\\""))
link
(escape-quotes ""hello"") => ""hello""

has-quotes? ^

checks if a string has quotes

v 2.4
(defn has-quotes?
  [^String s]
  (and (.startsWith s """)
       (.endsWith s """)))
link
(has-quotes? ""hello"") => true

strip-quotes ^

gets rid of quotes in a string

v 2.4
(defn strip-quotes
  [s]
  (if (has-quotes? s) 
    (subs s 1 (dec (count s)))
    s))
link
(strip-quotes ""hello"") => "hello"

whitespace? ^

checks if the string is all whitespace

v 2.4
(defn whitespace?
  [s]
  (boolean (or (= "" s) (re-find #"^[st]+$" s))))
link
(whitespace? " ") => true