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 }})
    # built-in functions still work
    Map.put(merged, :c, "world")
  end

end

Installation

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

def deps do
  [{:toolbelt, "~> 0.1.0"}]
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 }

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 project, so the more contribute, the more chancer there are of getting there.

License

The MIT License