Simplify convert-parse-tree and sort-postings
This commit is contained in:
parent
69f13c9a52
commit
fb0204d76c
1 changed files with 17 additions and 18 deletions
|
@ -63,21 +63,18 @@
|
|||
"Reformat the parse tree into a similar data structure as used during import."
|
||||
[tree]
|
||||
(walk/postwalk #(cond
|
||||
;; vector of digit chars to bigdec
|
||||
(and (vector? %) (char? (first %))) (apply str %)
|
||||
;; vector of chars to string
|
||||
(and (vector? %)
|
||||
(= (type (first %)) java.lang.Character)) (apply str %)
|
||||
(and (vector? %) (char? (first %))) (apply str %)
|
||||
;; posting amount to bigdec
|
||||
(and (= (type %) clojure.lang.PersistentArrayMap)
|
||||
(contains? % :amount)) (dissoc (update % :amount bigdec) :_)
|
||||
(and (map? %) (contains? % :amount)) (dissoc (update % :amount bigdec) :_)
|
||||
;; flatten quoted-tokens
|
||||
(and (= (type %) clojure.lang.PersistentArrayMap)
|
||||
(contains? % :token)) (:token %)
|
||||
(and (map? %) (contains? % :token)) (:token %)
|
||||
;; drop :_ keys
|
||||
(= (type %) clojure.lang.PersistentArrayMap) (dissoc % :_)
|
||||
(map? %) (dissoc % :_)
|
||||
;; convert vector of :key/:value maps to a map
|
||||
(and (vector? %)
|
||||
(= (type (first %)) clojure.lang.PersistentArrayMap)
|
||||
(contains? (first %) :key)) (kv->map %)
|
||||
(and (vector? %) (map? (first %)) (contains? (first %) :key)) (kv->map %)
|
||||
:else %)
|
||||
tree))
|
||||
|
||||
|
@ -92,8 +89,10 @@
|
|||
(convert-parse-tree tree)))
|
||||
|
||||
(defn sort-postings
|
||||
"Sort transaction postings into a predictable order."
|
||||
"Sort transaction postings into a predictable order and drop zero postings."
|
||||
[transactions]
|
||||
(for [t transactions]
|
||||
(update t :postings
|
||||
(fn [ps] (sort-by (juxt #(get-in % [:meta :entity]) :account :amount) (filter #(not (zero? (:amount %))) ps))))))
|
||||
(fn [ps] (->> ps
|
||||
(filter #(not (zero? (:amount %))))
|
||||
(sort-by (juxt #(get-in % [:meta :entity]) :account :amount)))))))
|
||||
|
|
Loading…
Reference in a new issue