From 1b54edf7b3fc3eaae4cf344d8f838697d5a2483b Mon Sep 17 00:00:00 2001 From: SphericalKat Date: Fri, 13 Aug 2021 07:12:13 +0530 Subject: [PATCH] feat(utils): add a phonetic key generator Signed-off-by: SphericalKat --- .gitignore | 1 + lib/ketbin/pastes/utils.ex | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 lib/ketbin/pastes/utils.ex diff --git a/.gitignore b/.gitignore index aea7aa2..c55d324 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ npm-debug.log # we ignore priv/static. You may want to comment # this depending on your deployment strategy. /priv/static/ +.idea \ No newline at end of file diff --git a/lib/ketbin/pastes/utils.ex b/lib/ketbin/pastes/utils.ex new file mode 100644 index 0000000..da8115e --- /dev/null +++ b/lib/ketbin/pastes/utils.ex @@ -0,0 +1,19 @@ +defmodule Ketbin.Pastes.Utils do + defp rand_vowel do + String.graphemes("aeiou") + |> Enum.take_random(1) + |> Enum.at(0) + end + + defp rand_consonant do + String.graphemes("bcdfghjklmnpqrstvwxyz") + |> Enum.take_random(1) + |> Enum.at(0) + end + + def generate_key(length \\ 10) do + random = Enum.random([0, 1]) + Enum.map(0..length, fn i -> if Integer.mod(i, 2) == random, do: rand_consonant(), else: rand_vowel() end) + |> List.to_string + end +end