From 959786182296943278095fdf6d90060b90308885 Mon Sep 17 00:00:00 2001 From: Max Cahill <1bardesign@gmail.com> Date: Wed, 24 Apr 2024 13:04:30 +1000 Subject: [PATCH] added tablex.previous_element --- tablex.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tablex.lua b/tablex.lua index ebb010e..1932204 100644 --- a/tablex.lua +++ b/tablex.lua @@ -182,6 +182,20 @@ function tablex.next_element(t, v) return t[i] end +function tablex.previous_element(t, v) + local i = tablex.index_of(t, v) + --not present? just get the front of the table + if not i then + return tablex.front(t) + end + --(not using mathx to avoid inter-dependency) + i = i - 1 + if i == 0 then + i = #t + end + return t[i] +end + --note: keyed versions of the above aren't required; you can't double --up values under keys