Skip to content

Instantly share code, notes, and snippets.

@simon-brooke
Created March 21, 2018 11:17
Show Gist options
  • Select an option

  • Save simon-brooke/1898318f74a2bd912c1446dacd6055fa to your computer and use it in GitHub Desktop.

Select an option

Save simon-brooke/1898318f74a2bd912c1446dacd6055fa to your computer and use it in GitHub Desktop.
Compare strings ignoring differences in whitespace - useful in unit tests!
(require '[clojure.string :as s])
(defn string-equal-ignore-whitespace?
"if `a` and `b` are both strings, ignore whitespace changes when comparing them;
othewise just check for ordinary equality. Return true if `a` and `b` are identical
modulo whitespace changes. Written because I didn't want unit tests to fail just
because of emitted whitespace changes"
[a b]
(if
(and
(string? a)
(string? b))
(let
[pattern #"[\s]+"
aa (s/replace a pattern " ")
bb (s/replace b pattern " ")]
(= aa bb))
(= a b)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment