From e6370b6231c04cac23ce07aee9126a568d27f517 Mon Sep 17 00:00:00 2001 From: Mateus Carvalho <1434224+mtsbarbosa@users.noreply.github.com> Date: Sun, 14 Apr 2024 15:05:42 -0300 Subject: [PATCH] adding namespaced-key->snake-key --- .clj-kondo/nubank/matcher-combinators/config.edn | 3 ++- project.clj | 2 +- src/clj/clj_data_adapter/core.clj | 10 ++++++++++ test/clj_data_adapter/core_test.clj | 13 ++++++++++++- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/.clj-kondo/nubank/matcher-combinators/config.edn b/.clj-kondo/nubank/matcher-combinators/config.edn index 2060e09..ea4ded7 100644 --- a/.clj-kondo/nubank/matcher-combinators/config.edn +++ b/.clj-kondo/nubank/matcher-combinators/config.edn @@ -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?])]}}} diff --git a/project.clj b/project.clj index 91feda9..7b8aeb7 100644 --- a/project.clj +++ b/project.clj @@ -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" diff --git a/src/clj/clj_data_adapter/core.clj b/src/clj/clj_data_adapter/core.clj index d6cf41d..4944012 100644 --- a/src/clj/clj_data_adapter/core.clj +++ b/src/clj/clj_data_adapter/core.clj @@ -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] diff --git a/test/clj_data_adapter/core_test.clj b/test/clj_data_adapter/core_test.clj index 7c24cd1..3212ce2 100644 --- a/test/clj_data_adapter/core_test.clj +++ b/test/clj_data_adapter/core_test.clj @@ -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 @@ -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