packages feed

animalcase (empty) → 0.1.0.0

raw patch · 5 files changed

+127/−0 lines, 5 filesdep +basedep +bytestringdep +textsetup-changed

Dependencies added: base, bytestring, text

Files

+ Changelog.md view
@@ -0,0 +1,5 @@+# Changelog++## Version 0.1++Initial Version.
+ LICENSE view
@@ -0,0 +1,21 @@+The MIT License (MIT)++Copyright (c) 2014 Tobias Florek (me@ibotty.net)++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in+all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ animalcase.cabal view
@@ -0,0 +1,31 @@+-- Initial animalcase.cabal generated by cabal init.  For further +-- documentation, see http://haskell.org/cabal/users-guide/++name:                animalcase+version:             0.1.0.0+synopsis:            Convert camelCase to snake_case and vice versa+description:         Feedback very welcome.+homepage:            https://github.com/ibotty/animalcase+license:             MIT+license-file:        LICENSE+author:              Tobias Florek+maintainer:          tob@butter.sh+copyright:           2014           +category:            Text+build-type:          Simple+extra-source-files:  Changelog.md+cabal-version:       >=1.10++library+  exposed-modules:     Text.AnimalCase+  -- other-modules:       +  -- other-extensions:    +  build-depends:       base ==4.*+                     , bytestring >=0.9 && <0.11+                     , text >= 1.0 && <1.2+  hs-source-dirs:      src+  default-language:    Haskell2010++source-repository head+  type:     darcs+  location: git@github.com:ibotty/animalcase.git
+ src/Text/AnimalCase.hs view
@@ -0,0 +1,68 @@+-- This Module converts between camelCase and snake_case.+--+-- The suffixes specify the base type the functions are working on.+-- L uses lazy 'TL.Text', S uses 'String' and B8 uses 'B8.ByteString'.+--+-- Beware, that you should not use 'B8.ByteString's for serious work, as+-- they only work with 8 bit characters. Do only use when you are+-- absolutely sure, there is no wide character in the string!+{-# LANGUAGE OverloadedStrings #-}+module Text.AnimalCase+  ( toCamelCase+  , toSnakeCase+  , toCamelCaseS+  , toSnakeCaseS+  , toCamelCaseL+  , toSnakeCaseL+  , toCamelCaseB8+  , toSnakeCaseB8+  ) where++import Control.Arrow (first)+import Data.Char (isUpper)+import Data.Monoid ((<>))+import Data.Text.Lazy.Encoding (decodeLatin1, encodeUtf8)+import qualified Data.ByteString.Char8 as B8+import qualified Data.ByteString.Lazy as BL+import qualified Data.Text as T+import qualified Data.Text.Lazy as TL++useLazy :: (TL.Text -> TL.Text) -> T.Text -> T.Text+useLazy f = TL.toStrict .  f . TL.fromStrict++useLazyS :: (TL.Text -> TL.Text) -> String -> String+useLazyS f = TL.unpack . f . TL.pack++useLazyB8 :: (TL.Text -> TL.Text) -> B8.ByteString -> B8.ByteString+useLazyB8 f = BL.toStrict . encodeUtf8 . f . decodeLatin1 . BL.fromStrict++onFirstLetter :: (Char -> TL.Text) -> TL.Text -> TL.Text+onFirstLetter f = maybe "" (uncurry (<>) . first f) . TL.uncons++toCamelCase :: T.Text -> T.Text+toCamelCase = useLazy toCamelCaseL++toCamelCaseL :: TL.Text -> TL.Text+toCamelCaseL = TL.concat . map capitalizeWord . TL.splitOn "_"+  where capitalizeWord = onFirstLetter (TL.toUpper . TL.singleton)++toCamelCaseS :: String -> String+toCamelCaseS = useLazyS toCamelCaseL++toCamelCaseB8 :: B8.ByteString -> B8.ByteString+toCamelCaseB8 = useLazyB8 toCamelCaseL++toSnakeCase :: T.Text -> T.Text+toSnakeCase = useLazy toSnakeCaseL++toSnakeCaseL :: TL.Text -> TL.Text+toSnakeCaseL = TL.concatMap f+  where f c+          | isUpper c = "_" <> TL.toLower (TL.singleton c)+          | otherwise = TL.singleton c++toSnakeCaseS :: String -> String+toSnakeCaseS = useLazyS toSnakeCaseL++toSnakeCaseB8 :: B8.ByteString -> B8.ByteString+toSnakeCaseB8 = useLazyB8 toSnakeCaseL