Skip to content

Commit

Permalink
refactor: improve readability and optimize toPosition function (opent…
Browse files Browse the repository at this point in the history
…ibiabr#2928)

Refactors the string.toPosition function to improve
readability and optimize the code. The main changes include:

• Renaming variables to more descriptive names.
• Optimized pattern matching by reusing patterns.
• Returning nil when no pattern is matched, improving error handling.

These changes make the code clearer and more efficient without altering
its functionality.
  • Loading branch information
omarcopires authored and vllworldbuilding committed Oct 24, 2024
1 parent d4094fc commit e9efe69
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions data/libs/functions/string.lua
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,19 @@ end
string.capitalize = function(str)
return str:gsub("%f[%a].", string.upper)
end

function string.toPosition(inputString)
local positionPatterns = {
"{%s*x%s*=%s*(%d+)%s*,%s*y%s*=%s*(%d+)%s*,%s*z%s*=%s*(%d+)%s*}",
"Position%s*%((%d+)%s*,%s*(%d+)%s*,%s*(%d+)%s*%)",
"(%d+)%s*,%s*(%d+)%s*,%s*(%d+)",
}

for _, pattern in ipairs(positionPatterns) do
local posX, posY, posZ = string.match(inputString, pattern)
if posX and posY and posZ then
return Position(tonumber(posX), tonumber(posY), tonumber(posZ))
end
end
return nil
end

0 comments on commit e9efe69

Please sign in to comment.