Created
March 21, 2018 11:17
-
-
Save simon-brooke/1898318f74a2bd912c1446dacd6055fa to your computer and use it in GitHub Desktop.
Compare strings ignoring differences in whitespace - useful in unit tests!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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