list.cljs 1.1 KB

1234567891011121314151617181920212223242526272829
  1. (ns frontend.util.list
  2. "High level list operations for use in editor"
  3. (:require [clojure.string :as string]))
  4. (defn- newline?
  5. [line]
  6. (or (= line "\n") (= line "\r\n")))
  7. (defn re-order-items
  8. [lines start-idx]
  9. (loop [lines lines
  10. idx start-idx
  11. result []
  12. double-newlines? false]
  13. (let [[line & others] lines]
  14. (if (empty? lines)
  15. (->> result
  16. (map (fn [line] (if (newline? line) "" line)))
  17. (string/join "\n"))
  18. (let [[_ num-str] (re-find #"^(\d+){1}\." line)
  19. num (if num-str (parse-long num-str) nil)
  20. double-newlines?' (or double-newlines?
  21. (and (newline? line) (seq others) (newline? (first others))))
  22. [idx' result'] (if (and (not double-newlines?') num)
  23. (let [idx' (inc idx)
  24. line' (string/replace-first line (str num ".") (str idx' "."))]
  25. [idx' (conj result line')])
  26. [idx (conj result line)])]
  27. (recur others idx' result' double-newlines?'))))))