mirror of
https://github.com/penpot/penpot.git
synced 2025-01-08 16:00:19 -05:00
🎉 Add nil-free hashmap factory macros.
This commit is contained in:
parent
a4d6524cb6
commit
4355d5ae80
1 changed files with 29 additions and 1 deletions
|
@ -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."
|
||||
|
|
Loading…
Reference in a new issue