Toolbelt

This is a small library which I created to abstract away some common utility functionality in my projects. It provides some useful tools for working with various internal data structures. This library was heavily inspired by lodash, but it doesn’t try to mimick it. Instead, it grows on an as-needed basis. Maybe it is of use to you, too.

The docs can be found at https://hexdocs.pm/toolbelt.

Toolbelt extends native Elixir modules with extra functionality. A simple use Toolbelt will make those extra functions accessible, while you can still use everything just as usual. Some of the functions it provides: Keyword.is_keyword_element, Map.map_deep, Map.map_keys, Map.map_values, Enum.andmap, Enum.ormap, Enum.first, Enum.last, …

defmodule MyModule do

  use Toolbelt

  def do_something do
    merged = Map.merge_deep(
      %{ a: "hello", b: %{ one: 1, two: 2 }}, 
      %{ b: %{ three: 3 }}
    )
    # result: %{ a: "hello", b: %{ one: 1, two: 2, three: 3 } }
    # built-in functions still work
    merged = Map.put(merged, :c, "world")
  end

end

Installation

Just add the following to your mix.exs-file:

def deps do
  [{:toolbelt, "~> 0.2.1"}]
end

Usage

As has already been stated, just add use Toolbelt somewhere within your module and enjoy the extra functions on Map, List, Enum, and so on. Alternatively, you can access the modules directly using something like Toolbelt.Enum.andmap. Check out the documentation for more information.

All Kinds of Mappers

With Toolbelt, you will have Enum.map and Map.map. What is the difference? Enum.map is a generic function that always produces a (keyword) list. It’s great, but sometimes you want the result to be a Map, just like the input. In that case, use Map.map, as it will always produce a map.

Map.map(%{ a: 1, b: 2, c: 3}, fn({k,v}) -> {k,v+1} end)
# %{ a: 2, b: 3, c: 4 }

A note about compiler warnings

Toolbelt “injects” code into the main Elixir classes. To make sure as many versions of Elixir are supported, we had to do a bit of magic, resulting in warnings when compiling Toolbelt for the first time. This is normal, and the warnings can be safely ignored.

Contributing

Yes, we very much like your help! Be it a suggestion, feature request, bug report, or a pull request. The aim of this project is to become a useful library for many projects, so the more contribute, the more chancer there are of getting there.

License

The MIT License