From 316d2cac3b7cbb0d05c83b76ebbf2a7ad332d902 Mon Sep 17 00:00:00 2001 From: varttist Date: Thu, 5 Nov 2026 20:55:05 +0300 Subject: [PATCH] `HelperLib`: add `matchItemDescription()` function --- mods/SYSTEM/HelperLib/mod.conf | 2 +- mods/SYSTEM/HelperLib/src/Lib.lua | 6 +- .../src/Lib/matchItemDescription.lua | 70 +++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 mods/SYSTEM/HelperLib/src/Lib/matchItemDescription.lua diff --git a/mods/SYSTEM/HelperLib/mod.conf b/mods/SYSTEM/HelperLib/mod.conf index fe2f43e..ee61d95 100644 --- a/mods/SYSTEM/HelperLib/mod.conf +++ b/mods/SYSTEM/HelperLib/mod.conf @@ -1,2 +1,2 @@ name = smc__system__helper_lib -depends = smc__system__ensure, smc__system__mod +depends = smc__system__ensure, smc__system__mod, smc__system__utf8 diff --git a/mods/SYSTEM/HelperLib/src/Lib.lua b/mods/SYSTEM/HelperLib/src/Lib.lua index dbcaaa9..5a9a877 100644 --- a/mods/SYSTEM/HelperLib/src/Lib.lua +++ b/mods/SYSTEM/HelperLib/src/Lib.lua @@ -6,6 +6,7 @@ Smc.isAsync = false local modInfo = Mod.getInfo('smc__system__helper_lib') local require = modInfo.require +-- Independent of SMC methods require('Lib.activeFormspecs') require('Lib.globalStep') require('Lib.color') @@ -14,4 +15,7 @@ require('Lib.nodeSuperClasses') require('Lib.getObjectByGUID') require('Lib.receiveFields') require('Lib.registeredItems') -require('Lib.requires') \ No newline at end of file +require('Lib.requires') + +--- Dependent of SMC methods +require('Lib.matchItemDescription') \ No newline at end of file diff --git a/mods/SYSTEM/HelperLib/src/Lib/matchItemDescription.lua b/mods/SYSTEM/HelperLib/src/Lib/matchItemDescription.lua new file mode 100644 index 0000000..7dd9b38 --- /dev/null +++ b/mods/SYSTEM/HelperLib/src/Lib/matchItemDescription.lua @@ -0,0 +1,70 @@ +local ipairs = ipairs +local tableInsert = table.insert +local stringMatch, stringLower = string.match, string_utf8.lower +local coreGetTranslatedString = core.get_translated_string + +---Checks whether an item's name and description match a given pattern. +--- +---Use it to create unconventional lists based on text searches. +--- +--- +---For example: +---```lua +------A function for determining a list of crafts that match a text-based search query +---local function getMathedCrafts(crafts, pattern) +--- local pattern = stringLower(pattern) +--- local matchedCrafts = {} +--- +--- for _, craft in ipairs(crafts) do +--- if Smc.matchItemDescription(ItemStack(craft.output):get_name(), pattern) then +--- tableInsert(matchedCrafts, craft) +--- end +--- end +--- +--- return matchedCrafts +---end +---``` +--- +---**Note**: to obtain standard lists of items, use: `Smc.getMathedItems()` +--- +---@param itemName ItemStackString +---@return boolean +function Smc.matchItemDescription(itemName, pattern) + local ItemStack = ItemStack(itemName) + + -- Only items registered via the `Item` wrapper class have an `_title` field. + -- Therefore, `short_description` is used as a fallback title. + local itemTitle = ItemStack:get_definition()._title or ItemStack:get_short_description() or '' + + local ruName = stringLower(coreGetTranslatedString('ru', itemTitle)) + local enName = stringLower(coreGetTranslatedString('en', itemTitle)) + + if stringMatch(itemName, pattern) or + stringMatch(ruName, pattern) or + stringMatch(enName, pattern) then + return true + else + return false + end +end + +local matchItemDescription = Smc.matchItemDescription + +---Returns a list of items whose name or description matches a pattern. +--- +---Use this method to retrieve lists of items based on a text search. +---@param items ItemStackString[] +---@param pattern string +---@return ItemStackString[] +function Smc.getMathedItems(items, pattern) + local pattern = stringLower(pattern) + local macthedItems = {} + + for _, itemName in ipairs(items) do + if matchItemDescription(itemName, pattern) then + tableInsert(macthedItems, itemName) + end + end + + return macthedItems +end