Skip to content

Commit

Permalink
Introduce optional-args
Browse files Browse the repository at this point in the history
  • Loading branch information
verberktstan authored Jun 4, 2024
1 parent 19c2e1c commit 3986354
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/swark/core.cljc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns swark.core
(:require [clojure.string :as str]))
(:require [clojure.string :as str]
[clojure.set :as set]))

;; SWiss ARmy Knife - Your everyday clojure toolbelt!
;; Copyright 2024 - Stan Verberkt ([email protected])
Expand Down Expand Up @@ -71,6 +72,20 @@
predicate (if ns #{ns} nil?)]
(filter-keys map (comp predicate namespace)))))

;; TODO: Add tests
(defn optional-args
"Returns a function that selects and renames keys, and adds defaults for sequentialargs (as destructured by &). Supply a props map containing permitted input keys as keys, and a map with :default & :rename as val.
`((optional-args {:name {:rename :person/name :default "Unnamed person"} :from {:rename :person/from :default "Nowhere"}}) {:name "Me"}) => #:person{:name "Me" :from "Nowhere"}`"
[props]
(let [renames (map-vals :rename props)
defaults (map-vals :default props)]
(fn optional-args* [args]
(let [m (apply hash-map args)]
(as-> m m'
(select-keys m' (keys props))
(merge defaults m')
(set/rename-keys m' renames))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Try and catch

Expand Down

0 comments on commit 3986354

Please sign in to comment.