An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# fields [![Build Status](https://travis-ci.com/stefanhengl/pdfsplit.svg?branch=master)](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

[![Clojars Project](https://img.shields.io/clojars/v/fields.svg)](https://clojars.org/fields)

## License
MIT