mirror of
https://github.com/penpot/penpot.git
synced 2025-03-14 16:51:18 -05:00
254 lines
6.6 KiB
Clojure
254 lines
6.6 KiB
Clojure
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
;;
|
|
;; Copyright (c) 2015-2019 Andrey Antukh <niwi@niwi.nz>
|
|
|
|
(ns app.util.data
|
|
"A collection of data transformation utils."
|
|
(:require [cljs.reader :as r]
|
|
[cuerdas.core :as str]))
|
|
|
|
;; TODO: partially move to app.common.data
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; Data structure manipulation
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
(defn index-by
|
|
"Return a indexed map of the collection
|
|
keyed by the result of executing the getter
|
|
over each element of the collection."
|
|
[getter coll]
|
|
(persistent!
|
|
(reduce #(assoc! %1 (getter %2) %2) (transient {}) coll)))
|
|
|
|
(def index-by-id #(index-by :id %))
|
|
|
|
(defn without-nils
|
|
"Given a map, return a map removing key-value
|
|
pairs when value is `nil`."
|
|
[data]
|
|
(into {} (remove (comp nil? second) data)))
|
|
|
|
(defn without-keys
|
|
"Return a map without the keys provided
|
|
in the `keys` parameter."
|
|
[data keys]
|
|
(persistent!
|
|
(reduce #(dissoc! %1 %2) (transient data) keys)))
|
|
|
|
(defn dissoc-in
|
|
[m [k & ks :as keys]]
|
|
(if ks
|
|
(if-let [nextmap (get m k)]
|
|
(let [newmap (dissoc-in nextmap ks)]
|
|
(if (seq newmap)
|
|
(assoc m k newmap)
|
|
(dissoc m k)))
|
|
m)
|
|
(dissoc m k)))
|
|
|
|
(defn index-of
|
|
"Return the first index when appears the `v` value
|
|
in the `coll` collection."
|
|
[coll v]
|
|
(first (keep-indexed (fn [idx x]
|
|
(when (= v x) idx))
|
|
coll)))
|
|
|
|
(defn replace-by-id
|
|
([value]
|
|
(map (fn [item]
|
|
(if (= (:id item) (:id value))
|
|
value
|
|
item))))
|
|
([coll value]
|
|
(sequence (replace-by-id value) coll)))
|
|
|
|
(defn deep-merge
|
|
"Like merge, but merges maps recursively."
|
|
[& maps]
|
|
(if (every? map? maps)
|
|
(apply merge-with deep-merge maps)
|
|
(last maps)))
|
|
|
|
(defn conj-or-disj
|
|
"Given a set, and an element remove that element from set
|
|
if it exists or add it if it does not exists."
|
|
[s v]
|
|
(if (contains? s v)
|
|
(disj s v)
|
|
(conj s v)))
|
|
|
|
(defn enumerate
|
|
([items] (enumerate items 0))
|
|
([items start]
|
|
(loop [idx start
|
|
items items
|
|
res []]
|
|
(if (empty? items)
|
|
res
|
|
(recur (inc idx)
|
|
(rest items)
|
|
(conj res [idx (first items)]))))))
|
|
|
|
(defn concatv
|
|
[& colls]
|
|
(loop [colls colls
|
|
result []]
|
|
(if (seq colls)
|
|
(recur (rest colls) (reduce conj result (first colls)))
|
|
result)))
|
|
|
|
(defn seek
|
|
([pred coll]
|
|
(seek pred coll nil))
|
|
([pred coll not-found]
|
|
(reduce (fn [_ x]
|
|
(if (pred x)
|
|
(reduced x)
|
|
not-found))
|
|
not-found coll)))
|
|
|
|
(defn remove-equal-values [m1 m2]
|
|
(if (and (map? m1) (map? m2) (not (nil? m1)) (not (nil? m2)))
|
|
(->> m1
|
|
(remove (fn [[k v]] (= (k m2) v)))
|
|
(into {}))
|
|
m1))
|
|
|
|
(defn with-next
|
|
"Given a collectin will return a new collection where each element
|
|
is paried with the next item in the collection
|
|
(with-next (range 5)) => [[0 1] [1 2] [2 3] [3 4] [4 nil]"
|
|
[coll]
|
|
(map vector
|
|
coll
|
|
(concat [] (rest coll) [nil])))
|
|
|
|
(defn with-prev
|
|
"Given a collectin will return a new collection where each element
|
|
is paried with the previous item in the collection
|
|
(with-prev (range 5)) => [[0 nil] [1 0] [2 1] [3 2] [4 3]"
|
|
[coll]
|
|
(map vector
|
|
coll
|
|
(concat [nil] coll)))
|
|
|
|
(defn with-prev-next
|
|
"Given a collection will return a new collection where every item is paired
|
|
with the previous and the next item of a collection
|
|
(with-prev-next (range 5)) => [[0 nil 1] [1 0 2] [2 1 3] [3 2 4] [4 3 nil]"
|
|
[coll]
|
|
(map vector
|
|
coll
|
|
(concat [nil] coll)
|
|
(concat [] (rest coll) [nil])))
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; Numbers Parsing
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
(defn nan?
|
|
[v]
|
|
(js/isNaN v))
|
|
|
|
(defn read-string
|
|
[v]
|
|
(r/read-string v))
|
|
|
|
(defn parse-int
|
|
([v]
|
|
(parse-int v nil))
|
|
([v default]
|
|
(let [v (js/parseInt v 10)]
|
|
(if (or (not v) (nan? v))
|
|
default
|
|
v))))
|
|
|
|
(defn parse-float
|
|
([v]
|
|
(parse-float v nil))
|
|
([v default]
|
|
(let [v (js/parseFloat v)]
|
|
(if (or (not v) (nan? v))
|
|
default
|
|
v))))
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; Other
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
(defn classnames
|
|
[& params]
|
|
{:pre [(even? (count params))]}
|
|
(str/join " " (reduce (fn [acc [k v]]
|
|
(if (and k (true? v))
|
|
(conj acc (name k))
|
|
acc))
|
|
[]
|
|
(partition 2 params))))
|
|
|
|
;; (defn normalize-attrs
|
|
;; [m]
|
|
;; (letfn [(transform [[k v]]
|
|
;; (cond
|
|
;; (or (= k :class) (= k :class-name))
|
|
;; ["className" v]
|
|
|
|
;; (or (keyword? k) (string? k))
|
|
;; [(str/camel (name k)) v]
|
|
|
|
;; :else
|
|
;; [k v]))
|
|
;; (walker [x]
|
|
;; (if (map? x)
|
|
;; (into {} (map tf) x)
|
|
;; x))]
|
|
;; (walk/postwalk walker m)))
|
|
|
|
(defn normalize-props
|
|
[props]
|
|
(clj->js props :keyword-fn (fn [key]
|
|
(if (or (= key :class) (= key :class-name))
|
|
"className"
|
|
(str/camel (name key))))))
|
|
|
|
(defn matches-search
|
|
[name search-term]
|
|
(if (str/empty? search-term)
|
|
true
|
|
(let [st (str/trim (str/lower search-term))
|
|
nm (str/trim (str/lower name))]
|
|
(str/includes? nm st))))
|
|
|
|
;; (defn coalesce
|
|
;; [^number v ^number n]
|
|
;; (if (.-toFixed v)
|
|
;; (js/parseFloat (.toFixed v n))
|
|
;; 0))
|
|
|
|
|
|
|
|
;; (defmacro mirror-map [& fields]
|
|
;; (let [keys# (map #(keyword (name %)) fields)
|
|
;; vals# fields]
|
|
;; (apply hash-map (interleave keys# vals#))))
|
|
|
|
;; (defmacro some->'
|
|
;; [x & forms]
|
|
;; `(let [x# (p/then' ~x (fn [v#]
|
|
;; (when (nil? v#)
|
|
;; (throw (ex-info "internal" {::some-interrupt true})))
|
|
;; v#))]
|
|
;; (-> (-> x# ~@forms)
|
|
;; (p/catch' (fn [e#]
|
|
;; (if (::some-interrupt (ex-data e#))
|
|
;; nil
|
|
;; (throw e#)))))))
|
|
|
|
(defn prefix-keyword [prefix kw]
|
|
(let [prefix (if (keyword? prefix) (name prefix) prefix)
|
|
kw (if (keyword? kw) (name kw) kw)]
|
|
(keyword (str prefix kw))))
|