0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-14 19:19:09 -05:00
penpot/frontend/uxbox/util/data.cljs

61 lines
1.5 KiB
Text
Raw Normal View History

(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]
2015-12-28 13:31:12 +02:00
(persistent!
(reduce #(assoc! %1 (getter %2) %2) (transient {}) coll)))
(def ^:static index-by-id #(index-by % :id))
2015-12-26 15:18:36 +02:00
(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)))
2015-12-29 15:51:32 +02:00
(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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2016-01-06 21:04:50 +02:00
(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))))
2016-01-07 01:24:11 +02:00
(defn parse-float
([v]
(js/parseFloat v))
([v default]
(let [v (js/parseFloat v)]
(if (or (not v) (nan? v))
default
v))))