├── .gitignore ├── LICENSE ├── README.md ├── config └── config.exs ├── lib └── elmxir.ex ├── mix.exs └── test ├── elmxir_test.exs └── test_helper.exs /.gitignore: -------------------------------------------------------------------------------- 1 | /_build 2 | /cover 3 | /deps 4 | erl_crash.dump 5 | *.ez 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, NoRedInk 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of elmxir nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # elmxir 2 | Interop tools for working with Elm in Elixir 3 | -------------------------------------------------------------------------------- /config/config.exs: -------------------------------------------------------------------------------- 1 | # This file is responsible for configuring your application 2 | # and its dependencies with the aid of the Mix.Config module. 3 | use Mix.Config 4 | 5 | # This configuration is loaded before any dependency and is restricted 6 | # to this project. If another project depends on this project, this 7 | # file won't be loaded nor affect the parent project. For this reason, 8 | # if you want to provide default values for your application for 9 | # 3rd-party users, it should be done in your "mix.exs" file. 10 | 11 | # You can configure for your application as: 12 | # 13 | # config :elmxir, key: :value 14 | # 15 | # And access this configuration in your application as: 16 | # 17 | # Application.get_env(:elmxir, :key) 18 | # 19 | # Or configure a 3rd-party app: 20 | # 21 | # config :logger, level: :info 22 | # 23 | 24 | # It is also possible to import configuration files, relative to this 25 | # directory. For example, you can emulate configuration per environment 26 | # by uncommenting the line below and defining dev.exs, test.exs and such. 27 | # Configuration from the imported file will override the ones defined 28 | # here (which is why it is important to import them last). 29 | # 30 | # import_config "#{Mix.env}.exs" 31 | -------------------------------------------------------------------------------- /lib/elmxir.ex: -------------------------------------------------------------------------------- 1 | defmodule Elmxir do 2 | @moduledoc """ 3 | Helper methods for a better life with Elm + Elixir 4 | """ 5 | 6 | @doc """ 7 | Get the arguments from an Elm object 8 | 9 | Returned in a mixed-type list 10 | 11 | ## Examples 12 | 13 | iex> Elmxir.gather_args(nil) 14 | [] 15 | 16 | iex> Elmxir.gather_args(%{"_0" => 1}) 17 | [1] 18 | """ 19 | @spec gather_args(nil) :: list 20 | def gather_args(nil) do [] end 21 | 22 | @spec gather_args(map) :: list 23 | def gather_args(action) do gather_args(action, 0) end 24 | 25 | @spec gather_args(map, integer) :: list 26 | def gather_args(action, index) do 27 | current_index = "_#{index}" 28 | value = Map.get(action, current_index) 29 | 30 | if value == nil do 31 | [ ] 32 | else 33 | [ value | gather_args(action, index + 1) ] 34 | end 35 | end 36 | 37 | @doc """ 38 | Get the action name of an Elm object 39 | 40 | Defaults to nil 41 | 42 | ## Examples 43 | 44 | iex> Elmxir.get_action_name(%{}) 45 | nil 46 | 47 | iex> Elmxir.get_action_name(%{ "ctor" => "Name" }) 48 | "Name" 49 | """ 50 | @spec get_action_name(map) :: string 51 | def get_action_name(action) do 52 | Map.get(action, "ctor") 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule Elmxir.Mixfile do 2 | use Mix.Project 3 | 4 | def project do 5 | [app: :elmxir, 6 | version: "0.0.1", 7 | elixir: "~> 1.1", 8 | build_embedded: Mix.env == :prod, 9 | start_permanent: Mix.env == :prod, 10 | description: "Helper functions for working with Elm + Elixir", 11 | package: package, 12 | deps: deps] 13 | end 14 | 15 | def package do 16 | [ 17 | maintainers: ["Noah Hall", "NoRedInk"], 18 | licenses: ["BSD3"], 19 | links: %{"GitHub" => "https://github.com/NoRedInk/elmxir"} 20 | ] 21 | end 22 | 23 | # Configuration for the OTP application 24 | # 25 | # Type "mix help compile.app" for more information 26 | def application do 27 | [applications: [:logger]] 28 | end 29 | 30 | # Dependencies can be Hex packages: 31 | # 32 | # {:mydep, "~> 0.3.0"} 33 | # 34 | # Or git/path repositories: 35 | # 36 | # {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"} 37 | # 38 | # Type "mix help deps" for more examples and options 39 | defp deps do 40 | [{:earmark, ">= 0.0.0", only: :dev}, 41 | {:ex_doc, "~> 0.10", only: :dev}] 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /test/elmxir_test.exs: -------------------------------------------------------------------------------- 1 | defmodule ElmxirTest do 2 | use ExUnit.Case 3 | doctest Elmxir 4 | 5 | test "when action is nil" do 6 | assert Elmxir.gather_args(nil) == [] 7 | end 8 | 9 | test "when action is a map but no index" do 10 | assert Elmxir.gather_args(%{"_0" => 1}) == [ 1 ] 11 | end 12 | 13 | test "when action is an empty map but no index" do 14 | assert Elmxir.gather_args(%{}) == [] 15 | end 16 | 17 | test "when action is a map with index" do 18 | assert Elmxir.gather_args(%{"_0" => 1, "_1" => 2, "_2" => 3}, 1) == [ 2, 3 ] 19 | end 20 | 21 | test "when action is a NoOp" do 22 | assert Elmxir.get_action_name(%{ "ctor" => "NoOp", "_0" => 0}) == "NoOp" 23 | end 24 | 25 | test "when action is a nothing" do 26 | assert Elmxir.get_action_name(%{"_0" => 0}) == nil 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | --------------------------------------------------------------------------------