Skip to the content.

Examples

(require '[camel-snake-kebab.core :as csk])

(csk/->camelCase 'flux-capacitor)
; => 'fluxCapacitor

(csk/->SCREAMING_SNAKE_CASE "I am constant")
; => "I_AM_CONSTANT"

(csk/->kebab-case :object_id)
; => :object-id

(csk/->HTTP-Header-Case "x-ssl-cipher")
; => "X-SSL-Cipher"

There are also functions that convert the value type for you:

(csk/->kebab-case-keyword "object_id")
; => :object-id

If the default way of separating words doesn’t work in your use case, you can override it:

(csk/->snake_case :s3-key :separator \-)
; => :s3_key

The :separator argument can either be a regex, string or character.

Installation

camel-snake-kebab is readily available from Clojars.

If you’re using Leiningen, then add [camel-snake-kebab "0.4.3"] to your project.clj :dependencies.

For deps, use {camel-snake-kebab {:mvn/version "0.4.3"}}.

Then add the following to your namespace declaration:

(:require [camel-snake-kebab.core :as csk])

Available Conversion Functions

You should be able to figure out all what all of them do.

Yeah, and then there are the type-converting functions:

Notes

Serving Suggestions

With JSON

(require '[clojure.data.json :as json])

(json/read-str "{\"firstName\":\"John\",\"lastName\":\"Smith\"}" :key-fn csk/->kebab-case-keyword)
; => {:first-name "John", :last-name "Smith"}

; And back:

(json/write-str {:first-name "John", :last-name "Smith"} :key-fn csk/->camelCaseString)
; => "{\"firstName\":\"John\",\"lastName\":\"Smith\"}"

With Plain Maps

(require '[camel-snake-kebab.extras :as cske])

(cske/transform-keys csk/->kebab-case-keyword {"firstName" "John", "lastName" "Smith"})
; => {:first-name "John", :last-name "Smith"}

; And back:

(cske/transform-keys csk/->camelCaseString {:first-name "John", :last-name "Smith"})
; => {"firstName" "John", "lastName" "Smith"}

With Memoization

If you’re going to do case conversion in a hot spot, use core.memoize to avoid doing the same conversions over and over again.

(require '[clojure.core.memoize :as m])
(require '[criterium.core :as cc])

(def memoized->kebab-case
  (m/fifo csk/->kebab-case {} :fifo/threshold 512))

(cc/quick-bench (csk/->kebab-case "firstName"))
; ... Execution time mean : 6,384971 µs ...

(cc/quick-bench (memoized->kebab-case "firstName"))
; ... Execution time mean : 700,146806 ns ...

Further Reading

Alternatives

License

Copyright (C) 2012-2020 the AUTHORS.

Distributed under the Eclipse Public License 1.0 (the same as Clojure).