Giter Club home page Giter Club logo

recase's Introduction

Recase

Recase

Build Status Coverage Status Hex Version Hex Docs Total Download License Last Updated

Recase helps you to convert a string from any case to any case.

Why?

One can ask: "Why should I use Recase when I can use built-in Macro module?". But, you have to keep in mind that Macro's functions are not suitable for general case usage:

Do not use it as a general mechanism for underscoring or camelizing strings as it does not support Unicode or characters that are not valid in Elixir identifiers.

Installation

def deps do
  [
    {:recase, "~> 0.5"}
  ]
end

Usage

Pascal

Pascal (or Upper) case uses mixed case with lower and upper cased characters. Separates words from each other with the upper case letters. Starts with upper case letter.

Recase.to_pascal("some-value") # => "SomeValue"
Recase.to_pascal("Some value") # => "SomeValue"

Camel

Camel case uses mixed case with lower and upper cased characters. Separates words from each other with the upper cased letters. Starts with lower case letter.

Recase.to_camel("some-value") # => "someValue"
Recase.to_camel("Some Value") # => "someValue"

Snake

Snake cases uses all lower case. Uses _ as a separator.

Recase.to_snake("someValue") # => "some_value"
Recase.to_snake("Some Value") # => "some_value"

Kebab

Kebab cases uses all lower case. Uses - as a separator.

Recase.to_kebab("someValue") # => "some-value"
Recase.to_kebab("Some Value") # => "some-value"

Constant

Constant case uses all upper case. Uses _ as a separator.

Recase.to_constant("SomeValue") # => "SOME_VALUE"
Recase.to_constant("some value") # => "SOME_VALUE"

Dot

Dot case uses all lower case similar to snake case. Uses . as a separator.

Recase.to_dot("SomeValue") # => "some.value"
Recase.to_dot("some value") # => "some.value"

Path

Path case preserves case, you can also provide a separator as the second argument.

Recase.to_path("SomeValue") # => "Some/Value"
Recase.to_path("some value", "\\") # => "some\\value"

Sentence

Sentence case uses the same rules as regular sentence. First letter is uppercase all others letters are lowercase separated by spaces.

Recase.to_sentence("SomeValue") # => "Some value"
Recase.to_sentence("some value") # => "Some value"

Title

Title case applies a "Title Style" to all words in a sentence.

Recase.to_title("some-value") # => "Some Value"
Recase.to_title("some value") # => "Some Value"

Header

Header case uses the same case as PascalCase, while using - as a separator.

Recase.to_header("SomeValue") # => "Some-Value"
Recase.to_header("some value") # => "Some-Value"

Enumerable

You can convert all keys in an enumerable with:

Recase.Enumerable.convert_keys(
  %{"yourKey" => "value"},
  &Recase.to_snake/1
) # => %{"your_key" => "value"}

Recase.Enumerable.convert_keys(
  %{"your_key" => "value"},
  &Recase.to_camel/1
) # => %{"yourKey" => "value"}

Recase.Enumerable.atomize_keys(
  %{"yourKey" => "value"},
  &Recase.to_snake/1
) # => %{your_key: "value"}

Recase.Enumerable.atomize_keys(
  %{"your_key" => "value"},
  &Recase.to_camel/1
) # => %{yourKey: "value"}

Changelog

Full changelog is available here.

Copyright and License

Copyright (c) 2017 Nikita Sobolev

This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See the LICENSE.md file for more details.

Thanks

Thanks to Gyan Lakhwani for the logo.

recase's People

Contributors

am-kantox avatar bluecollarchris avatar dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar elielhaouzi avatar iporsut avatar jhchen avatar kianmeng avatar mralexlau avatar ndreynolds avatar neel-tanda avatar rbino avatar sobolevn avatar teedang19 avatar tildewill avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

recase's Issues

`to_camel` only capitalizes last word if given camel cased input

to_camel produces unexpected output when given an existing camel case string. This seems to happen when the given string has more than 2 words in it:

Recase.to_camel("fooBar")
> "fooBar" # Correct

Recase.to_camel("fooBarBaz")
> "foobarBaz" # Incorrect, this should be "fooBarBaz"

Recase.to_camel("fooBarBazBiz")
> "foobarbazBiz" # Incorrect, this should be "fooBarBazBiz"

Recase logo offer

Hello! I am a graphic designer. I contribute to open source software. I saw the logo in the Readme File. I wanted to customize the logo. You can be sure I can do better. Do you want to see my new logo idea?

I will wait for feedback. Have a nice day!

You cannot circularly convert camel to snake

I came here because Macro has the same problem, and it's frustrating with how it's used inside Absinthe but isn't circular. I was hoping to find another lib that did it right, but...alas, this lib has the same broken functionality when you have acronyms with mulitiple cases in a row:

iex> "power1VA" |> Recase.to_snake |> Recase.to_camel
"power1Va"

What I'd expect is Recase.to_snake converts it to "power1_v_a"

Is this something this library would like to fix? I can look into the code if so.

Handle atom inputs

What do you think about also handling atom inputs? I'm happy to make a PR to implement this enhancement if you're on board.

Maps that are not Enumerable

Hello!

When using Recase.Enumerable.convert_keys, it tries to recursively convert keys in maps. The problem is that not every map has Enumerable protocol implementation, so it raises if it finds for example DateTime struct.

Guess we could check if Enumerable is implemented?

Recase.to_snake/1 is not consistent with previous versions (<0.4.0)

We have seen a compatibility break from versions 0.5.0+ in Recase.to_snake/1. This happens when there are multiple uppercase letters followed by lowercase ones as you can see in the example with "CurrencyISOCode":

Versions 0.5+

iex(2)> Recase.to_snake("CurrencyISOCode")
"currency_isocode"

Previos versions <= 0.4

iex(2)> Recase.to_snake("CurrencyISOCode")
"currency_iso_code"

Is that intended? For the moment we are pinning the dependency to v0.4.0

`Recase.to_name/1` incorrectly lowercases certain last names

NameCase lowercases certain patterns of strings like El -> el and Von -> von to handle names that use these patterns as connectors. Something like Erik von Willebrand. However, this is not desired behaviour when something like Von is the last name.

Consider Theo Von. NameCase changes this to Theo von, but it should not do that. Similarly other last names like Le/Lo are also incorrectly lowercased.

This logic should only kick in when the connector is followed by another name after it.

Consider using digits as separator in snake and related cases

Currently digits do not break up tokens in snake case and other (kebab case, dot case, constant case) so SnakeCase.convert("g2g") == "g2g". This is different from other implementations such as Lodash where _.lodash("g2g") === "g_2_g". I could not find an authoritative description of snake case as to which is the expected behavior but given the prevalence of Lodash, following its behavior might end up causing less surprise for developers/users.

Missing to_title function and TitleCase module

This is really baffling. Obviously I see both here on Github, but if add the {:recase, "~> 0.4"} dependency, fetch it with mix deps.get, and then go spelunking into the deps/recase/... directory both the function and module are missing. Is this some kind of hex bug?

Add sentence case

Sentence case works like so:

  • Some Random Value -> Some random value
  • some_random_value -> Some random value

0.7.0

Hi @sobolevn,

Can you tag the current master version ? is there a specific reason not publish a new tag ?

Thanks

Removing punctuation

From the Wikipedia page linked from kebab_case.ex it says "As per snake_case above" and in the snake_case section above it says "Punctuation is removed and spaces are replaced..." However it looks like in this project's tests for kebab_case it seems to intentionally test for preservation of punctuation https://github.com/sobolevn/recase/blob/master/test/recase_test/kebab_case_test.exs#L18

My understanding of kebab case (and other cases) is that punctation should be removed and consecutive punctation is collapsed into one hyphen. This is for example what lodash does for their string conversion suite and this definition of kebab case seems supported by this project's own comment link to Wikipedia.

So for one I wanted to ask to clarify on what is the correct behavior. If punctuation should be preserved then the link to Wikipedia should be removed as it does not describe the functionality of the functions implemented by this project. If they should be removed then I'm happy to submit a PR changing this and updating the tests.

In case it is helpful the use case I have is generating urls for titles (user generated) and preserving punctation is of course problematic for urls.

`Recase.to_name/1` does not handle characters with accents correctly

When using NameCase we're seeing that characters following characters with accents (like í or á) are capitalized, when they should not be.

iex(1)> Recase.to_name("Colín Díaz")
"ColíN DíAz"
iex(2)> Recase.to_name("Colin Diaz")
"Colin Diaz"

I would expect Recase.to_name("Colín Díaz") == "Colín Díaz".

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.