Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/malomohq/cocoon
Pure Elixir library for transforming data 🐛✨🦋
https://github.com/malomohq/cocoon
elixir-lang elixir-library made-by-malomo transform transformations transformer
Last synced: 3 days ago
JSON representation
Pure Elixir library for transforming data 🐛✨🦋
- Host: GitHub
- URL: https://github.com/malomohq/cocoon
- Owner: malomohq
- License: mit
- Created: 2020-03-09T15:40:10.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-03-10T19:14:02.000Z (almost 5 years ago)
- Last Synced: 2024-04-14T16:58:52.919Z (9 months ago)
- Topics: elixir-lang, elixir-library, made-by-malomo, transform, transformations, transformer
- Language: Elixir
- Homepage:
- Size: 13.7 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cocoon
**Cocoon** is a pure [Elixir](http://elixir-lang.org/) library for transforming data 🐛✨🦋
## Installation
Cocoon is published on [Hex](https://hex.pm/packages/cocoon).
Add it to your list of dependencies in `mix.exs`;```elixir
defp deps do
{ :cocoon, "~> 1.0" }
end
```## Usage
Pass a map-based dataset into `Cocoon.transform/2`, along with a list of a
mappings, and it will emerge in a new form:```elixir
data =
%{
"currency" => "USD",
"customer" => %{
"email" => "[email protected]",
},
"notifications" => false,
"number_of_items" => 3,
"order_number" => 12
}mappings =
[
{ :currency, ["currency"] },
{ :email, ["customer", "email"] },
{ :item_count, ["number_of_items"] },
{ :number, ["order_number"], &to_string/1 }
]Cocoon.transform(data, mappings)
#=> %{
#=> currency: "USD",
#=> email: "[email protected]",
#=> item_count: 3,
#=> number: "12"
#=> }
```One potential use case for Cocoon is to serve as a transformation layer between
the outside world and your application.For example, you might transform the responses from an external API into a
format that is more predictable and consistent with the vocabulary in your app.
By doing this, you also impose a boundary that limits coupling to the external
data source.### Mappings
Cocoon transforms data by using a set of user-specified mappings, i.e. a list
of instructions for how individual elements in the dataset should be changed.A mapping can be either a 2 or 3-element tuple, corresponding to a specific
entry in the dataset:* First element - destination key, i.e. "to key"
* Second element - path to existing key, i.e. "from key(s)"
* Third element (optional) - function to apply to value at existing key_NOTE: A mapping must be provided for every key / value pair that you want to
be returned. In other words, the transformed dataset will only contain key /
value pairs that are explicitly defined by a mapping._### Data Types
Cocoon currently provides support for transforming maps, or a list of maps, e.g.
a list of orders.If a list of maps is provided, a list of maps will also be returned, with the
mappings applied to each element of the set.