Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sentence case and Title Case for string output #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/camel_snake_kebab/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
->snake_case
->kebab-case
->HTTP-Header-Case
->Sentence
->Title

->PascalCaseKeyword
->camelCaseKeyword
Expand All @@ -21,6 +23,8 @@
->kebab-case-keyword
->Camel_Snake_Case_Keyword
->HTTP-Header-Case-Keyword
->Sentencekeyword
->TitleKeyword

->PascalCaseString
->camelCaseString
Expand All @@ -29,14 +33,18 @@
->kebab-case-string
->Camel_Snake_Case_String
->HTTP-Header-Case-String
->Sentencestring
->TitleString

->PascalCaseSymbol
->camelCaseSymbol
->SCREAMING_SNAKE_CASE_SYMBOL
->snake_case_symbol
->kebab-case-symbol
->Camel_Snake_Case_Symbol
->HTTP-Header-Case-Symbol)
->HTTP-Header-Case-Symbol
->Sentencesymbol
->Titlesymbol)

(defn convert-case
"Converts the case of a string according to the rule for the first
Expand All @@ -53,3 +61,5 @@
(defconversion "snake_case" clojure.string/lower-case clojure.string/lower-case "_")
(defconversion "kebab-case" clojure.string/lower-case clojure.string/lower-case "-")
(defconversion "HTTP-Header-Case" camel-snake-kebab.internals.misc/capitalize-http-header camel-snake-kebab.internals.misc/capitalize-http-header "-")
(defconversion "Sentencecase" clojure.string/capitalize clojure.string/lower-case " ")
(defconversion "TitleCase" clojure.string/capitalize clojure.string/capitalize " ")
7 changes: 6 additions & 1 deletion src/camel_snake_kebab/internals/macros.cljc
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
(ns ^:no-doc camel-snake-kebab.internals.macros
#?(:cljs (:refer-clojure :exclude [resolve]))
(:require [camel-snake-kebab.internals.alter-name :refer [alter-name]]
[camel-snake-kebab.internals.misc :refer [convert-case]]))
[camel-snake-kebab.internals.misc :refer [convert-case]]
[clojure.string]))

#?(:cljs
(defn resolve [sym]
;; On self-hosted ClojureScript, macros are evaluated under the `:cljs` conditional branch
;; In that case, we need to use `eval` in order to resolve variables instead of `resolve`
(eval `(~'var ~sym))))

(defn strip-whitespace [s]
(clojure.string/replace s #" " ""))

(defn type-preserving-function [case-label first-fn rest-fn sep]
`(defn ~(symbol (str "->" case-label)) [s# & rest#]
(let [convert-case# #(apply convert-case ~first-fn ~rest-fn ~sep % rest#)]
Expand All @@ -19,6 +23,7 @@
(->> (str case-label " " type-label)
(convert-case (resolve first-fn) (resolve rest-fn) sep)
(str "->")
(strip-whitespace)
(symbol)))]
(for [[type-label type-converter] {"string" `identity "symbol" `symbol "keyword" `keyword}]
`(defn ~(make-name type-label) [s# & rest#]
Expand Down