https://github.com/stefanhengl/fields
Like select-keys but for nested maps
https://github.com/stefanhengl/fields
clojure fields filter nested select-keys
Last synced: 6 months ago
JSON representation
Like select-keys but for nested maps
- Host: GitHub
- URL: https://github.com/stefanhengl/fields
- Owner: stefanhengl
- License: mit
- Created: 2019-08-03T16:41:15.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-08-18T04:57:46.000Z (over 6 years ago)
- Last Synced: 2025-06-09T02:12:11.755Z (7 months ago)
- Topics: clojure, fields, filter, nested, select-keys
- Language: Clojure
- Homepage:
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fields [](https://travis-ci.com/stefanhengl/pdfsplit)
`fields` exposes the function `select-keys-by-fields` which offers the
same functionality as
[select-keys](https://clojuredocs.org/clojure.core/select-keys) but
for complex nested maps. Instead of specifying paths in a vector, we write a query
describing the structure of the desired output map.
`fields`is inspired by the same-named HTTP request parameter as
described by Google
[here](https://developers.google.com/drive/api/v3/performance). In the
background it parses the query into a
[zipper](https://clojuredocs.org/clojure.zip) and traverses the zipper
filtering the map along the way.
## Example
```clojure
(def data {:a 1
:b [{:aa 2 :bb 3} {:aa 4 :bb 5}]
:c {:cc 6 :dd {:fff 7 :ggg 8}}})
(select-keys-by-fields data "(a)")
=> {:a 1}
(select-keys-by-fields data "(a,b(aa))")
=> {:a 1, :b ({:aa 2} {:aa 4})}
(select-keys-by-fields data "(b,c(cc,dd(ggg)))")
=> {:b ({:aa 2, ::bb 3} {:aa 4, :bb 5}), :c {:cc 6, :dd {:ggg 8}}}
```
The parser which parses a query string and outputs a zipper is a pure function that is worth caching whenever you have many differnt maps but only a small set of queries. For such use cases you can just memozie the parser and use `select-keys-by-zipper` instead of `select-keys-by-fields`.
``` clojure
(def parse-memo (memoize parse))
(select-keys-by-zipper data (parse-memo "(a,b(aa))"))
=> {:a 1, :b ({:aa 2} {:aa 4})}
```
## Latest Version
[](https://clojars.org/fields)
## License
MIT