Deflate a nested map into one level deep or inflate a one level into a nested map using delimited keys.
Inspired by these guys.
For installation, add the following dependency to your project.clj
file:
[org.clojars.roboli/balloon "x.y.z"]
Or your deps.edn
:
org.clojars.roboli/balloon {:mvn/version "x.y.z"}
Import namespace, example:
(ns balloon.core
(:require [balloon.core :as b]))
Use deflate to flat a nested map:
(b/deflate {:my-map {:one "one"
:two "two"
:any {:other "other"}}
:my-array [{:a "a"} "b" "c"]})
;;=>
;; {:my-map.one "one",
;; :my-map.two "two",
;; :my-map.any.other "other",
;; :my-array.0.a "a",
;; :my-array.1 "b",
;; :my-array.2 "c"}
Use inflate to convert a deflated (flatten) map into a nested one:
(b/inflate {:my-map.one "one",
:my-map.two "two",
:my-map.any.other "other",
:my-array.0.a "a",
:my-array.1 "b",
:my-array.2 "c"})
;;=>
;; {:my-map {:one "one",
;; :two "two",
;; :any {:other "other"}},
;; :my-array [{:a "a"} "b" "c"]}
There are only two functions: deflate and inflate, each one with its own options.
(deflate nested-hash-map :option-1-key option-1-val :option-2-key option-2-val ...)
Where options:
:delimiter
: Use different delimiter to build delimited keys, defaults to .
.:keep-coll
: Do not flat collections (lists or vectors), defaults to false
.Examples:
;; Using :delimiter "*"
(b/deflate {:id 3
:profile {:first-name "Lucas"
:last-last "Arts"}
:location {:country "USA"
:city "LA"}}
:delimiter "*")
;;=>
;; {:id 3,
;; :profile*first-name "Lucas",
;; :profile*last-last "Arts",
;; :location*country "USA",
;; :location*city "LA"}
;; Or using :delimiter "/"
(b/deflate {:id 3
:profile {:first-name "Lucas"
:last-last "Arts"}
:location {:country "USA"
:city "LA"}}
:delimiter "/")
;;=>
;; {:id 3,
;; :profile/first-name "Lucas",
;; :profile/last-last "Arts",
;; :location/country "USA",
;; :location/city "LA"}
;; Using :keep-coll
(b/deflate {:destinations ["Paris" "London" "Madrid"]
:person {:first-name "John"
:last-last "Walker"}}
:keep-coll true)
;;=>
;; {:destinations ["Paris" "London" "Madrid"],
;; :person.first-name "John",
;; :person.last-last "Walker"}
(inflate flat-hash-map :option-1-key option-1-val :option-2-key option-2-val ...)
:delimiter
: Use different delimiter to unflat the hash-map delimited keys, defaults to .
.:hash-map
: Unflat indexes in delimited keys as hash-map, not as a collection, defaults to false
.Examples:
;; Using :delimiter "/"
(b/inflate {:id 3,
:profile/first-name "Lucas",
:profile/last-last "Arts",
:location/country "USA",
:location/city "LA"}
:delimiter "/")
;;=>
;; {:id 3,
;; :profile {:first-name "Lucas",
;; :last-last "Arts"},
;; :location {:country "USA",
;; :city "LA"}}
;; Using :hash-map
(b/inflate {:values.0.one "one"
:values.1.two "two"
:values.2.three "three"}
:hash-map true)
;;=>
;; {:values {0 {:one "one"},
;; 1 {:two "two"},
;; 2 {:three "three"}}}
Looking for a CLI?
$ lein balloon deflate '{:a {:b "c"}}' :delimiter '*'
;;=> {:a*b "c"}
Please check out the lein-balloon plugin for more.
Copyright © 2024 Roberto Oliveros
This program and the accompanying materials are made available under the terms of the Eclipse Public License 2.0 which is available at http://www.eclipse.org/legal/epl-2.0.
Can you improve this documentation? These fine people already did:
roboli & Roberto OliverosEdit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close