0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-08 07:50:43 -05:00

🎉 Add nil-free hashmap factory macros.

This commit is contained in:
Andrey Antukh 2020-09-29 10:00:10 +02:00 committed by Andrés Moya
parent a4d6524cb6
commit 4355d5ae80

View file

@ -6,7 +6,7 @@
(ns app.common.data
"Data manipulation and query helper functions."
(:refer-clojure :exclude [concat read-string])
(:refer-clojure :exclude [concat read-string hash-map])
(:require [clojure.set :as set]
[linked.set :as lks]
#?(:cljs [cljs.reader :as r]
@ -115,6 +115,34 @@
[data]
(into {} (remove (comp nil? second) data)))
(defmacro without-nils
"A generic helper macro that removes nils from hash-map from
collection at compile time. If it is not possible (a symbol is
received), fallback to runtume nil removing procediment thanks to
`remove-nil-vals` function."
[param]
(cond
(symbol? param)
`(remove-nil-vals ~param)
(map? param)
`~(remove-nil-vals param)
:else
(throw (ex-info "Invalid arguments"
{:type :internal
:code :invalid-arguments}))))
(defmacro hash-map
"A closure friendly macro for build nil-free hash-maps at compile
time."
[& kvpairs]
(let [data (->> (partition 2 kvpairs)
(remove (comp nil? second))
(mapcat identity))]
`(hash-map ~@data)))
(defn without-keys
"Return a map without the keys provided
in the `keys` parameter."