Back
trim-newline (clj)
(source)function
(trim-newline s)
Removes all trailing newline \n or return \r characters from
string. Similar to Perl's chomp.
Examples
clojure
(ns clojure.test-clojure.string
(:require [clojure.string :as s])
(:use clojure.test))
(deftest t-trim-newline
(is (= "foo" (s/trim-newline "foo\n")))
(is (= "foo" (s/trim-newline "foo\r\n")))
(is (= "foo" (s/trim-newline "foo")))
(is (= "" (s/trim-newline ""))))
(deftest nil-handling
(are [f args] (thrown? NullPointerException (apply f args))
s/reverse [nil]
s/replace [nil #"foo" "bar"]
s/replace-first [nil #"foo" "bar"]
s/re-quote-replacement [nil]
s/capitalize [nil]
s/upper-case [nil]
s/lower-case [nil]
s/split [nil #"-"]
s/split [nil #"-" 1]
s/trim [nil]
s/triml [nil]
s/trimr [nil]
s/trim-newline [nil]))
(deftest char-sequence-handling
(are [result f args] (let [[^CharSequence s & more] args]
(= result (apply f (StringBuffer. s) more)))
"paz" s/reverse ["zap"]
"foo:bar" s/replace ["foo-bar" \- \:]
"ABC" s/replace ["abc" #"\w" s/upper-case]
"faa" s/replace ["foo" #"o" (StringBuffer. "a")]
"baz::quux" s/replace-first ["baz--quux" #"--" "::"]
"baz::quux" s/replace-first ["baz--quux" (StringBuffer. "--") (StringBuffer. "::")]
"zim-zam" s/replace-first ["zim zam" #" " (StringBuffer. "-")]
"\\\\ \\$" s/re-quote-replacement ["\\ $"]
"Pow" s/capitalize ["POW"]
"BOOM" s/upper-case ["boom"]
"whimper" s/lower-case ["whimPER"]
["foo" "bar"] s/split ["foo-bar" #"-"]
"calvino" s/trim [" calvino "]
"calvino " s/triml [" calvino "]
" calvino" s/trimr [" calvino "]
"the end" s/trim-newline ["the end\r\n\r\r\n"]
true s/blank? [" "]
["a" "b"] s/split-lines ["a\nb"]
"fa la la" s/escape ["fo lo lo" {\o \a}]))
clojure/clojure
(ns clojure.test-clojure.string
(:require [clojure.string :as s])
(:use clojure.test))
(deftest t-trim-newline
(is (= "foo" (s/trim-newline "foo\n")))
(is (= "foo" (s/trim-newline "foo\r\n")))
(is (= "foo" (s/trim-newline "foo")))
(is (= "" (s/trim-newline ""))))
(deftest nil-handling
(are [f args] (thrown? NullPointerException (apply f args))
s/reverse [nil]
s/replace [nil #"foo" "bar"]
s/replace-first [nil #"foo" "bar"]
s/re-quote-replacement [nil]
s/capitalize [nil]
s/upper-case [nil]
s/lower-case [nil]
s/split [nil #"-"]
s/split [nil #"-" 1]
s/trim [nil]
s/triml [nil]
s/trimr [nil]
s/trim-newline [nil]))
(deftest char-sequence-handling
(are [result f args] (let [[^CharSequence s & more] args]
(= result (apply f (StringBuffer. s) more)))
"paz" s/reverse ["zap"]
"foo:bar" s/replace ["foo-bar" \- \:]
"ABC" s/replace ["abc" #"\w" s/upper-case]
"faa" s/replace ["foo" #"o" (StringBuffer. "a")]
"baz::quux" s/replace-first ["baz--quux" #"--" "::"]
"baz::quux" s/replace-first ["baz--quux" (StringBuffer. "--") (StringBuffer. "::")]
"zim-zam" s/replace-first ["zim zam" #" " (StringBuffer. "-")]
"\\\\ \\$" s/re-quote-replacement ["\\ $"]
"Pow" s/capitalize ["POW"]
"BOOM" s/upper-case ["boom"]
"whimper" s/lower-case ["whimPER"]
["foo" "bar"] s/split ["foo-bar" #"-"]
"calvino" s/trim [" calvino "]
"calvino " s/triml [" calvino "]
" calvino" s/trimr [" calvino "]
"the end" s/trim-newline ["the end\r\n\r\r\n"]
true s/blank? [" "]
["a" "b"] s/split-lines ["a\nb"]
"fa la la" s/escape ["fo lo lo" {\o \a}]))
clojure/clojurescript
(ns clojure.string-test
(:require [cljs.test :as test
:refer-macros [deftest is testing]]
[clojure.test.check :as tc]
[clojure.test.check.clojure-test :refer-macros [defspec]]
[clojure.test.check.generators :as gen]
[clojure.test.check.properties :as prop :include-macros true]
[clojure.string :as s]))
(testing "Testing string trim-newline"
(is (= "foo" (s/trim-newline "foo\n")))
(is (= "foo" (s/trim-newline "foo\r\n")))
(is (= "foo" (s/trim-newline "foo")))
(is (= "foo\r " (s/trim-newline "foo\r ")))
(is (= "" (s/trim-newline ""))))
(testing "Testing string trim-newline"
(is (= "foo" (s/trim-newline "foo\n")))
(is (= "foo" (s/trim-newline "foo\r\n")))
(is (= "foo" (s/trim-newline "foo")))
(is (= "foo\r " (s/trim-newline "foo\r ")))
(is (= "" (s/trim-newline ""))))
(deftest char-sequence-handling
(are [result f args] (let [[^CharSequence s & more] args]
(= result (apply f (StringBuffer. s) more)))
"paz" s/reverse ["zap"]
"foo:bar" s/replace ["foo-bar" \- \:]
"ABC" s/replace ["abc" #"\w" s/upper-case]
"faa" s/replace ["foo" #"o" (StringBuffer. "a")]
"baz::quux" s/replace-first ["baz--quux" #"--" "::"]
"baz::quux" s/replace-first ["baz--quux" (StringBuffer. "--") (StringBuffer. "::")]
"zim-zam" s/replace-first ["zim zam" #" " (StringBuffer. "-")]
"Pow" s/capitalize ["POW"]
"BOOM" s/upper-case ["boom"]
"whimper" s/lower-case ["whimPER"]
["foo" "bar"] s/split ["foo-bar" #"-"]
"calvino" s/trim [" calvino "]
"calvino " s/triml [" calvino "]
" calvino" s/trimr [" calvino "]
"the end" s/trim-newline ["the end\r\n\r\r\n"]
true s/blank? [" "]
["a" "b"] s/split-lines ["a\nb"]
"fa la la" s/escape ["fo lo lo" {\o \a}]))
)
arcadia-unity/Arcadia
(ns clojure.test-clojure.string
(:require [clojure.string :as s])
(:use clojure.test))
(deftest t-trim-newline
(is (= "foo" (s/trim-newline "foo\n")))
(is (= "foo" (s/trim-newline "foo\r\n")))
(is (= "foo" (s/trim-newline "foo")))
(is (= "" (s/trim-newline ""))))
(deftest nil-handling
(are [f args] (thrown? Exception (apply f args)) ;;; NullPointerException
s/reverse [nil]
s/replace [nil #"foo" "bar"]
s/replace-first [nil #"foo" "bar"]
;;;s/re-quote-replacement [nil] ;;; CLR no-op
s/capitalize [nil]
s/upper-case [nil]
s/lower-case [nil]
s/split [nil #"-"]
s/split [nil #"-" 1]
s/trim [nil]
s/triml [nil]
s/trimr [nil]
s/trim-newline [nil]))
;(deftest char-sequence-handling ;;; This tests StringBuffer : CharSequence -- irrelevant for ClojureCLR
; (are [result f args] (let [[^String s & more] args] ;;; CharSequence
; (= result (apply f (StringBuffer. s) more)))
; "paz" s/reverse ["zap"]
; "foo:bar" s/replace ["foo-bar" \- \:]
; "ABC" s/replace ["abc" #"\w" s/upper-case]
; "faa" s/replace ["foo" #"o" (StringBuffer. "a")]
; "baz::quux" s/replace-first ["baz--quux" #"--" "::"]
; "baz::quux" s/replace-first ["baz--quux" (StringBuffer. "--") (StringBuffer. "::")]
; "zim-zam" s/replace-first ["zim zam" #" " (StringBuffer. "-")]
; "\\\\ \\$" s/re-quote-replacement ["\\ $"]
; "Pow" s/capitalize ["POW"]
; "BOOM" s/upper-case ["boom"]
; "whimper" s/lower-case ["whimPER"]
; ["foo" "bar"] s/split ["foo-bar" #"-"]
; "calvino" s/trim [" calvino "]
; "calvino " s/triml [" calvino "]
; " calvino" s/trimr [" calvino "]
; "the end" s/trim-newline ["the end\r\n\r\r\n"]
; true s/blank? [" "]
; ["a" "b"] s/split-lines ["a\nb"]
; "fa la la" s/escape ["fo lo lo" {\o \a}]))
yogthos/markdown-clj
(ns markdown.md-file-test
(:require [markdown.core :as markdown]
[markdown.transformers :as transformers]
[clojure.string :as string]
[clojure.test :refer :all]))
(deftest img-references
(let [wrt (java.io.StringWriter.)]
(markdown/md-to-html (str "test/files" java.io.File/separator "img_references.md") wrt :reference-links? true)
(is (= (clojure.string/trim-newline
(slurp (str "test/files" java.io.File/separator "img_references.html")))
(.toString wrt)))))
mhuebert/maria
(ns maria.repl-specials
"Special forms that exist only in the REPL."
(:require [lark.eval :as e :refer [defspecial]]
[maria.views.repl-specials :as special-views]
[maria.friendly.kinds :as kinds]
[maria.live.ns-utils :as ns-utils]
[clojure.string :as str]
[maria.editors.code :as code]
[maria.views.cards :as repl-ui]
[maria.util :as util]
[chia.view.hiccup :as hiccup]
[chia.view :as v]))
(defspecial doc
"Show documentation for given symbol"
[c-state c-env name]
(if-let [the-var (ns-utils/resolve-var-or-special c-state c-env name)]
{:value (special-views/doc (merge {:expanded? true
:standalone? true}
the-var))}
{:error (js/Error. (if (symbol? name) (str "Could not resolve the symbol `" (str/trim-newline (with-out-str (prn name))) "`. Maybe it has not been defined?")
(str (str "`doc` requires a symbol, but a " (cljs.core/name (kinds/kind name)) " was passed."))))}))
(defspecial source
"Show source code for given symbol"
[c-state c-env name]
(if-let [the-var (and (symbol? name) (ns-utils/resolve-var-or-special c-state c-env name))]
{:value (hiccup/element [:div {:class (str repl-ui/card-classes
" ph3")}
(special-views/var-source the-var)])}
{:error (js/Error. (str "Could not resolve the symbol `" (str/trim-newline (with-out-str (prn name))) "`"))}))