GithubHelp home page GithubHelp logo

murtaza52 / camel-snake-kebab Goto Github PK

View Code? Open in Web Editor NEW

This project forked from clj-commons/camel-snake-kebab

0.0 2.0 0.0 146 KB

A Clojure library to convert between different word case conventions.

Clojure 100.00%

camel-snake-kebab's Introduction

Camel_SNAKE-kebab

A Clojure library to convert between different word case conventions.

Examples

(use 'camel-snake-kebab)

(->CamelCase 'flux-capacitor)
; => 'FluxCapacitor

(->SNAKE_CASE "I am constant")
; => "I_AM_CONSTANT"

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

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

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

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

Installation

  1. Add the following to your project.clj :dependencies:
[camel-snake-kebab "0.1.4"]
  1. Add the following to your namespace declaration:
:use camel-snake-kebab

Available Conversion Functions

  • ->CamelCase
  • ->camelCase
  • ->SNAKE_CASE
  • ->Snake_case
  • ->snake_case
  • ->kebab-case
  • ->Camel_Snake_Case
  • ->HTTP-Header-Case

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

Yeah, and then there are the type converting functions:

  • ->CamelCase{Keyword, String, Symbol}
  • ->camelCase{Keyword, String, Symbol}
  • ->SNAKE_CASE_{KEYWORD, STRING, SYMBOL}
  • ->Snake_case_{keyword, string, symbol}
  • ->snake_case_{keyword, string, symbol}
  • ->kebab-case-{keyword, string, symbol}
  • ->Camel_Snake_Case_{Keyword, String, Symbol}
  • ->HTTP-Header-Case-{Keyword, String, Symbol}

Serving Suggestions

(defn map-keys [f m]
  (letfn [(mapper [[k v]] [(f k) (if (map? v) (map-keys f v) v)])]
    (into {} (map mapper m))))

With JSON

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

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

; And back:

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

With Plain Maps

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

; And back:

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

With JavaBeans

(->> (java.util.Date.)
     (bean)
     (map-keys ->kebab-case)
     :timezone-offset)
; => -120

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.

(use 'clojure.core.memoize)
(use 'criterium.core)

(def memoized->kebab-case
  (memo-fifo ->kebab-case 512))

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

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

Further Reading

Alternatives

License

Copyright (C) 2012-2014 Christoffer Sawicki & ToBeReplaced

Distributed under the Eclipse Public License, the same as Clojure.

camel-snake-kebab's People

Contributors

qerub avatar tobereplaced avatar murtaza52 avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.