Giter Club home page Giter Club logo

proper_case's Introduction

ProperCase

An Elixir library that converts keys in maps between snake_case and camel_case.

Useful as a plug in Phoenix for converting incoming params from JavaScript's camelCase to Elixir's snake_case

Converts all keys in maps to snake case ProperCase.to_snake_case/1

Converts all keys in maps to camel case ProperCase.to_camel_case/1

Converts a string to snake case ProperCase.snake_case/1

Converts a string to camel case ProperCase.camel_case/1

Usage

Example: ProperCase.to_snake_case

# Before:
%{"user" => %{
    "firstName" => "Han",
    "lastName" => "Solo",
    "alliesInCombat" => [
      %{"name" => "Luke", "weaponOfChoice" => "lightsaber"},
      %{"name" => "Chewie", "weaponOfChoice" => "bowcaster"},
      %{"name" => "Leia", "weaponOfChoice" => "blaster"}
    ] 
  }
} 

# After:
%{"user" => %{
    "first_name" => "Han",
    "last_name" => "Solo",
    "allies_in_combat" => [
      %{"name" => "Luke", "weapon_of_choice" => "lightsaber"},
      %{"name" => "Chewie", "weapon_of_choice" => "bowcaster"},
      %{"name" => "Leia", "weapon_of_choice" => "blaster"}
    ]
  }
}

Using as a plug in Phoenix

ProperCase is extremely useful as a part of your connection pipeline, converting incoming params from JavaScript's camelCase to Elixir's snake_case

Plug it into your router.ex connection pipeline like so:

  pipeline :api do
    plug :accepts, ["json"]
    plug ProperCase.Plug.SnakeCaseParams
  end

camelCase before encoding json in Phoenix

Set phoenix's json encoder in config/config.exs, this way ProperCase will camelCase your data before encoding to JSON:

  :phoenix, :format_encoders, json: ProperCase.JSONEncoder.CamelCase

Custom data transform before encoding with Phoenix

To ensure that outgoing params are converted to camelCase, define a custom JSON encoder that runs a transform before encoding to json.

def MyApp.CustomJSONEncoder do
  use ProperCase.JSONEncoder, transform: &ProperCase.to_camel_case/1
end

config.exs

  :phoenix, :format_encoders, json: MyApp.CustomJSONEncoder

Installation

If available in Hex, the package can be installed as:

  1. Add proper_case to your list of dependencies in mix.exs:

    def deps do [{:proper_case, "~> 1.0.2"}] end

  2. Ensure proper_case is started before your application:

    def application do [applications: [:proper_case]] end

Contributors

proper_case's People

Contributors

johnnyji avatar smdern avatar alxgnon avatar expede avatar iancanderson avatar mikaak avatar

Watchers

James Cloos avatar  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.