diff --git a/.formatter.exs b/.formatter.exs new file mode 100644 index 0000000..d2cda26 --- /dev/null +++ b/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..58881af --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +# The directory Mix will write compiled artifacts to. +/_build/ + +# If you run "mix test --cover", coverage assets end up here. +/cover/ + +# The directory Mix downloads your dependencies sources to. +/deps/ + +# Where third-party dependencies like ExDoc output generated docs. +/doc/ + +# Ignore .fetch files in case you like to edit your project deps locally. +/.fetch + +# If the VM crashes, it generates a dump, let's ignore it too. +erl_crash.dump + +# Also ignore archive artifacts (built via "mix archive.build"). +*.ez + +# Ignore package tarball (built via "mix hex.build"). +yt_ext-*.tar + +# Temporary files, for example, from tests. +/tmp/ diff --git a/lib/yt_ext.ex b/lib/yt_ext.ex new file mode 100644 index 0000000..072f923 --- /dev/null +++ b/lib/yt_ext.ex @@ -0,0 +1,18 @@ +defmodule YtExt do + @moduledoc """ + Documentation for `YtExt`. + """ + + @doc """ + Hello world. + + ## Examples + + iex> YtExt.hello() + :world + + """ + def hello do + :world + end +end diff --git a/lib/yt_ext/application.ex b/lib/yt_ext/application.ex new file mode 100644 index 0000000..f1283fc --- /dev/null +++ b/lib/yt_ext/application.ex @@ -0,0 +1,20 @@ +defmodule YtExt.Application do + # See https://hexdocs.pm/elixir/Application.html + # for more information on OTP Applications + @moduledoc false + + use Application + + @impl true + def start(_type, _args) do + children = [ + # Starts a worker by calling: YtExt.Worker.start_link(arg) + # {YtExt.Worker, arg} + ] + + # See https://hexdocs.pm/elixir/Supervisor.html + # for other strategies and supported options + opts = [strategy: :one_for_one, name: YtExt.Supervisor] + Supervisor.start_link(children, opts) + end +end diff --git a/lib/yt_ext/youtube_id.ex b/lib/yt_ext/youtube_id.ex new file mode 100644 index 0000000..3007422 --- /dev/null +++ b/lib/yt_ext/youtube_id.ex @@ -0,0 +1,20 @@ +defmodule YouTubeID do + @youtube_chars "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" + @base length(String.graphemes(@youtube_chars)) + + def to_integer(<>) do + video_id + |> String.graphemes() + |> Enum.reverse() + |> Enum.with_index() + |> Enum.reduce(0, fn {char, index}, acc -> + char_value = contains_at(@youtube_chars, char) + acc + char_value * trunc(:math.pow(@base, index)) + end) + end + + defp contains_at(string, char) do + String.split(string, "", trim: true) + |> Enum.find_index(&(&1 == char)) + end +end diff --git a/mix.exs b/mix.exs new file mode 100644 index 0000000..a28533f --- /dev/null +++ b/mix.exs @@ -0,0 +1,29 @@ +defmodule YtExt.MixProject do + use Mix.Project + + def project do + [ + app: :yt_ext, + version: "0.1.0", + elixir: "~> 1.17", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger], + mod: {YtExt.Application, []} + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/test/test_helper.exs b/test/test_helper.exs new file mode 100644 index 0000000..869559e --- /dev/null +++ b/test/test_helper.exs @@ -0,0 +1 @@ +ExUnit.start() diff --git a/test/yt_ext_test.exs b/test/yt_ext_test.exs new file mode 100644 index 0000000..56e9995 --- /dev/null +++ b/test/yt_ext_test.exs @@ -0,0 +1,8 @@ +defmodule YtExtTest do + use ExUnit.Case + doctest YtExt + + test "greets the world" do + assert YtExt.hello() == :world + end +end