mirror of
https://github.com/penpot/penpot.git
synced 2025-01-24 07:29:08 -05:00
60 lines
1.5 KiB
Clojure
60 lines
1.5 KiB
Clojure
(ns uxbox.util.data
|
|
"A collection of data transformation utils."
|
|
(:require [cljs.reader :as r]))
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; 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."
|
|
[coll getter]
|
|
(persistent!
|
|
(reduce #(assoc! %1 (getter %2) %2) (transient {}) coll)))
|
|
|
|
(def ^:static index-by-id #(index-by % :id))
|
|
|
|
(defn remove-nil-vals
|
|
"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)))
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; Numbers Parsing
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
(defn nan?
|
|
[v]
|
|
(js/isNaN v))
|
|
|
|
(defn read-string
|
|
[v]
|
|
(r/read-string v))
|
|
|
|
(defn parse-int
|
|
([v]
|
|
(js/parseInt v 10))
|
|
([v default]
|
|
(let [v (js/parseInt v 10)]
|
|
(if (or (not v) (nan? v))
|
|
default
|
|
v))))
|
|
|
|
(defn parse-float
|
|
([v]
|
|
(js/parseFloat v))
|
|
([v default]
|
|
(let [v (js/parseFloat v)]
|
|
(if (or (not v) (nan? v))
|
|
default
|
|
v))))
|