Skip to content

Commit

Permalink
adding namespaced-key->snake-key
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsbarbosa committed Apr 14, 2024
1 parent e0267cc commit e6370b6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .clj-kondo/nubank/matcher-combinators/config.edn
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{:linters
{:unresolved-symbol
{:exclude [(clojure.test/is [match? thrown-match?])]}}}
{:exclude [(cljs.test/is [match? thrown-match?])
(clojure.test/is [match? thrown-match?])]}}}
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject org.clojars.majorcluster/clj-data-adapter "0.9.0"
(defproject org.clojars.majorcluster/clj-data-adapter "0.10.0"
:description "A Clojure data adapter library to convert your data between your layers"
:url "https://github.com/majorcluster/clj-data-adapter"
:license {:name "The MIT License"
Expand Down
10 changes: 10 additions & 0 deletions src/clj/clj_data_adapter/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@
name
(str/replace "-" "_")))

(defn namespaced-key->snake-key
"Converts namespaced kebab keys to snake cased key"
[k]
(-> k
name
(str/split #".*\/")
last
(str/replace "-" "_")
keyword))

(defn namespaced-key->kebab-key
"Converts namespaced keys to kebab cased key"
[k]
Expand Down
13 changes: 12 additions & 1 deletion test/clj_data_adapter/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
kebab-key->namespaced-key
kebab-key->snake-str namespaced-key->kebab-key opt snake-key->kebab-key snake-key->kebab-str
snake-str->kebab-key str->uuid transform transform-keys transform-keys-1-depth
transform-values transform-values-1-depth uuid->str]]
transform-values transform-values-1-depth uuid->str
namespaced-key->snake-key]]
[clojure.test :refer :all]))

(deftest transform-keys-test
Expand Down Expand Up @@ -50,6 +51,16 @@
namespaced-key->kebab-key
[{:test/id 1, :test/name "croissant", :test/unit-grams 200, :test/price 5.40M}
{:test/id 4, :test/price 9.40M}]))))
(testing "converts namespaced keys to snake cased keys"
(is (= {:id 1, :name "croissant", :unit_grams 200, :price 5.40M}
(transform-keys namespaced-key->snake-key {:test/id 1, :test/name "croissant", :test/unit-grams 200, :test/price 5.40M}))))
(testing "converts namespaced keys in a vector to snake cased keys"
(is (= [{:id 1, :name "croissant", :unit_grams 200, :price 5.40M}
{:id 4, :price 9.40M}]
(transform-keys
namespaced-key->snake-key
[{:test/id 1, :test/name "croissant", :test/unit-grams 200, :test/price 5.40M}
{:test/id 4, :test/price 9.40M}]))))
(testing "converts camel cased keys to kebab cased"
(is (= [{:id 1, :bread-name "croissant", :unit-grams 200, :price 5.40M, :a 1, :my-bread "croissant"}]
(transform-keys
Expand Down

0 comments on commit e6370b6

Please # to comment.