From 69b5b5b082696f37957263e86ec6028525efcf56 Mon Sep 17 00:00:00 2001 From: Guilherme Andrade Date: Sat, 28 Oct 2023 18:00:02 +0100 Subject: [PATCH] How far back we can take Elixir compatibility by removing `then/1`? --- .github/workflows/ci.yml | 12 +++++++ .../elixir_version_to_otp_version.json | 12 +++++++ lib/sqids/alphabet.ex | 15 ++++---- lib/sqids/blocklist.ex | 26 +++++++------- mix.exs | 1 + scripts/update_blocklist.exs | 36 +++++++++---------- 6 files changed, 63 insertions(+), 39 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8f0ac5a..d4bb6f7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,6 +23,18 @@ jobs: strategy: matrix: elixir_vsn: [ + '1.0', + '1.1', + '1.2', + '1.3', + '1.4', + '1.5', + '1.6', + '1.7', + '1.8', + '1.9', + '1.10', + '1.11', '1.12', '1.13', '1.14', diff --git a/.github/workflows/elixir_version_to_otp_version.json b/.github/workflows/elixir_version_to_otp_version.json index 718cad7..359fe42 100644 --- a/.github/workflows/elixir_version_to_otp_version.json +++ b/.github/workflows/elixir_version_to_otp_version.json @@ -1,4 +1,16 @@ { + "1.0": "18.3", + "1.1": "18.3", + "1.2": "19.3", + "1.3": "19.3", + "1.4": "19.3", + "1.5": "20.3", + "1.6": "21.3", + "1.7": "22.3", + "1.8": "22.3", + "1.9": "22.3", + "1.10": "23.3", + "1.11": "24.3", "1.12": "24.3", "1.13": "25.3", "1.14": "26.1", diff --git a/lib/sqids/alphabet.ex b/lib/sqids/alphabet.ex index ac6517b..7dcdfb1 100644 --- a/lib/sqids/alphabet.ex +++ b/lib/sqids/alphabet.ex @@ -1,6 +1,6 @@ defmodule Sqids.Alphabet do @moduledoc false - # import ExUnit.Assertions + import ExUnit.Assertions @min_alphabet_length 3 @@ -106,13 +106,12 @@ defmodule Sqids.Alphabet do @spec index_of!(t(), byte) :: index def index_of!(%{} = alphabet, char) do # It would be nice to optimize this. - alphabet - |> Enum.find_value(fn {index, byte} -> - byte == char and index - end) - |> then(fn index when index != nil -> - index - end) + + index = + Enum.find_value(alphabet, fn {index, byte} -> byte == char and index end) + + assert index !== nil + index end ## Internal diff --git a/lib/sqids/blocklist.ex b/lib/sqids/blocklist.ex index d811e5f..3f3f769 100644 --- a/lib/sqids/blocklist.ex +++ b/lib/sqids/blocklist.ex @@ -72,19 +72,19 @@ defmodule Sqids.Blocklist do alphabet_graphemes_downcased = alphabet_str |> String.downcase() |> String.graphemes() |> MapSet.new() sort_fun = fn word -> {String.length(word), word} end - words - |> Enum.uniq() - |> Enum.reduce( - _acc0 = %__MODULE__{min_word_length: min_word_length}, - &maybe_add_blocklist_entry(&1, &2, alphabet_graphemes_downcased) - ) - |> then(fn blocklist -> - %{ - blocklist - | prefixes_and_suffixes: Enum.sort_by(blocklist.prefixes_and_suffixes, sort_fun), - matches_anywhere: Enum.sort_by(blocklist.matches_anywhere, sort_fun) - } - end) + blocklist = + words + |> Enum.uniq() + |> Enum.reduce( + _acc0 = %__MODULE__{min_word_length: min_word_length}, + &maybe_add_blocklist_entry(&1, &2, alphabet_graphemes_downcased) + ) + + %{ + blocklist + | prefixes_and_suffixes: Enum.sort_by(blocklist.prefixes_and_suffixes, sort_fun), + matches_anywhere: Enum.sort_by(blocklist.matches_anywhere, sort_fun) + } end @spec maybe_add_blocklist_entry(String.t(), t(), MapSet.t(String.grapheme())) :: t() diff --git a/mix.exs b/mix.exs index 2582f4f..b25d8bc 100644 --- a/mix.exs +++ b/mix.exs @@ -27,6 +27,7 @@ defmodule Sqids.MixProject do threshold: 94 ] ], + dialyzer: [plt_add_apps: [:ex_unit]], package: package() ] end diff --git a/scripts/update_blocklist.exs b/scripts/update_blocklist.exs index 9c7265a..135270e 100755 --- a/scripts/update_blocklist.exs +++ b/scripts/update_blocklist.exs @@ -35,24 +35,24 @@ defmodule Sqids.BlocklistUpdater do defp convert_from_canonical_list do log_step("Converting canonical blocklist...") - @path_of_canonical_json - |> File.read!() - |> Jason.decode!() - |> :lists.usort() - |> Enum.reduce( - _acc0 = "", - fn word, acc -> - refute match?( - {true, _}, - {String.contains?(word, ["\n", "\r"]), word} - ) - - [acc, word, "\n"] - end - ) - |> then(fn blocklist -> - File.write!(@path_of_txt_copy, blocklist) - end) + blocklist = + @path_of_canonical_json + |> File.read!() + |> Jason.decode!() + |> :lists.usort() + |> Enum.reduce( + _acc0 = "", + fn word, acc -> + refute match?( + {true, _}, + {String.contains?(word, ["\n", "\r"]), word} + ) + + [acc, word, "\n"] + end + ) + + File.write!(@path_of_txt_copy, blocklist) end defp maybe_update_changelog do