From 1e9a65153719c632d96392cbbb72592516401e94 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Fri, 31 Dec 2021 00:55:46 +0000 Subject: [PATCH] stings/polyfill: add str_ends ....because seriously, Lua *really* should have this. --- worldeditadditions/utils/strings/polyfill.lua | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/worldeditadditions/utils/strings/polyfill.lua b/worldeditadditions/utils/strings/polyfill.lua index d1599ed..8c52b38 100644 --- a/worldeditadditions/utils/strings/polyfill.lua +++ b/worldeditadditions/utils/strings/polyfill.lua @@ -26,6 +26,14 @@ local function str_starts(str, start) return string.sub(str, 1, string.len(start)) == start end +--- Equivalent to string.endsWith in JS +-- @param str string The string to operate on +-- @param substr_end number The ending string to look for +-- @returns bool Whether substr_end is present at the end of str +local function str_ends(str, substr_end) + return string.sub(str, -string.len(substr_end)) == substr_end +end + --- Trims whitespace from a string from the beginning and the end. -- From http://lua-users.org/wiki/StringTrim -- @param str string The string to trim the whitespace from. @@ -39,12 +47,14 @@ if worldeditadditions then worldeditadditions.str_padend = str_padend worldeditadditions.str_padstart = str_padstart worldeditadditions.str_starts = str_starts + worldeditadditions.str_ends = str_starts worldeditadditions.trim = trim else return { str_padend = str_padend, str_padstart = str_padstart, str_starts = str_starts, + str_ends = str_starts, trim = trim } end