Fix oban tags
[akkoma] / lib / pleroma / emoji / combinations.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Emoji.Combinations do
6 # FE0F is the emoji variation sequence. It is used for fully-qualifying
7 # emoji, and that includes emoji combinations.
8 # This code generates combinations per emoji: for each FE0F, all possible
9 # combinations of the character being removed or staying will be generated.
10 # This is made as an attempt to find all partially-qualified and unqualified
11 # versions of a fully-qualified emoji.
12 # I have found *no cases* for which this would be a problem, after browsing
13 # the entire emoji list in emoji-test.txt. This is safe, and, sadly, most
14 # likely sane too.
15
16 defp qualification_combinations(codepoints) do
17 qualification_combinations([[]], codepoints)
18 end
19
20 defp qualification_combinations(acc, []), do: acc
21
22 defp qualification_combinations(acc, ["\uFE0F" | tail]) do
23 acc
24 |> Enum.flat_map(fn x -> [x, x ++ ["\uFE0F"]] end)
25 |> qualification_combinations(tail)
26 end
27
28 defp qualification_combinations(acc, [codepoint | tail]) do
29 acc
30 |> Enum.map(&Kernel.++(&1, [codepoint]))
31 |> qualification_combinations(tail)
32 end
33
34 def variate_emoji_qualification(emoji) when is_binary(emoji) do
35 emoji
36 |> String.codepoints()
37 |> qualification_combinations()
38 |> Enum.map(&List.to_string/1)
39 end
40
41 def variate_emoji_qualification(emoji) when is_list(emoji) do
42 emoji
43 |> Enum.map(fn emoji -> {emoji, variate_emoji_qualification(emoji)} end)
44 end
45 end