Back

trim (clj)

(source)

function

(trim s)
Removes whitespace from both ends of string.

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 t-triml
  (is (= "foo " (s/triml " foo ")))
  (is (= "" (s/triml "   ")))
  (is (= "bar" (s/triml "\u2002 \tbar"))))

(deftest t-trimr
  (is (= " foo" (s/trimr " foo ")))
  (is (= "" (s/trimr "   ")))
  (is (= "bar" (s/trimr "bar\t \u2002"))))

(deftest t-trim
  (is (= "foo" (s/trim "  foo  \r\n")))
  (is (= "bar" (s/trim "\u2000bar\t \u2002"))))

(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}]))
logseq/logseq
(ns frontend.components.diff
  (:require [clojure.string :as string]
            [frontend.diff :as diff]
            [frontend.handler.file :as file]
            [frontend.state :as state]
            [frontend.ui :as ui]
            [frontend.util :as util]
            [medley.core :as medley]
            [rum.core :as rum]))

(rum/defcs local-file < rum/reactive
  {:will-unmount (fn [state]
                   (reset! disk-value nil)
                   (reset! db-value nil)
                   state)}
  [state repo path disk-content db-content]
  (when (nil? @disk-value)
    (reset! disk-value disk-content)
    (reset! db-value db-content))
  [:div.cp__diff-file
   [:div.cp__diff-file-header
    [:span.cp__diff-file-header-content.pl-1.font-medium
     (str "File " path " has been modified on the disk.")]]
   [:div.p-4
    (when (not= (string/trim disk-content) (string/trim db-content))
      (ui/foldable
       [:span.text-sm.font-medium.ml-1 "Check diff"]
       (fn []
         (let [local-content (or db-content "")
               content (or disk-content "")
               diff (medley/indexed (diff/diff local-content content))
               diff? (some (fn [[_idx {:keys [added removed]}]]
                             (or added removed))
                           diff)]
           (when diff?
             [:div.overflow-y-scroll.flex.flex-col
              [:div {:style {:max-height "65vh"}}
               (diff-cp diff)]])))
       {:default-collapsed? true
        :title-trigger? true}))
status-im/status-mobile
(ns quo.components.inputs.recovery-phrase.component-spec
  (:require
    [clojure.string :as string]
    [oops.core :as oops]
    [quo.components.inputs.recovery-phrase.view :as recovery-phrase]
    [test-helpers.component :as h]))

    (h/test "Marked when words exceed the limit given"
      (h/render [recovery-phrase/recovery-phrase-input
                 {:mark-errors? true
                  :word-limit   4}
                 "these are ok words, these words exceed the limit"])
      (let [children-text-nodes            (-> (h/get-by-label-text :recovery-phrase-input)
                                               (oops/oget "props" "children" "props" "children")
                                               (js->clj :keywordize-keys true))
            {:keys [ok-words error-words]} (group-by #(if (string? %) :ok-words :error-words)
                                                     children-text-nodes)]
        (h/is-equal (string/trim (apply str ok-words))
                    "these are ok words,")
        (h/is-equal (->> error-words
                         (map #(-> % :props :argv second))
                         (interpose " ")
                         (apply str))
                    "these words exceed the limit")))))
functional-koans/clojure-koans
(ns koans.02-strings
  (:require [koan-engine.core :refer :all]
            [clojure.string :as string]))

  "Sometimes you don't want whitespace cluttering the front and back"
  (= __ (string/trim "  \nhello world \t \n"))
clj-kondo/clj-kondo
(require '[selmer.parser :as p])
(require '[clojure.java.io :as io])
(require '[clojure.string :as str])
(require '[clojure.edn :as edn])

(def version (str/trim (slurp (io/file "resources/CLJ_KONDO_VERSION"))))
(def stable-version (str/trim (slurp (io/file "resources/CLJ_KONDO_RELEASED_VERSION"))))
HumbleUI/HumbleUI
(ns examples.7guis-converter
  (:require
    [clojure.string :as str]
    [io.github.humbleui.core :as core]
    [io.github.humbleui.ui :as ui]))

(add-watch *celsius ::update
  (fn [_ _ old new]
    (when-not *editing*
      (when (not= (:text old) (:text new))
        (binding [*editing* true]
          (if-some [c (parse-long (str/trim (:text new)))]
            (let [f (-> c (* 9) (quot 5) (+ 32) str)]
              (swap! *fahrenheit assoc
                :text f
                :from (count f)
                :to   (count f)))
            (swap! *fahrenheit assoc
              :text ""
              :from 0
              :to   0)))))))

(add-watch *fahrenheit ::update
  (fn [_ _ old new]
    (when-not *editing*
      (when (not= (:text old) (:text new))
        (binding [*editing* true]
          (if-some [f (parse-long (str/trim (:text new)))]
            (let [c (-> f (- 32) (* 5) (quot 9) str)]
              (swap! *celsius assoc
                :text c
                :from (count c)
                :to   (count c)))
            (swap! *celsius assoc
              :text ""
              :from 0
              :to   0)))))))