fix(utils): use `stringx.strip` to trim string
ci/woodpecker/pr/lint Pipeline was successful Details
ci/woodpecker/pr/build Pipeline was successful Details

This commit is contained in:
Aire-One 2022-10-31 15:04:44 +01:00
parent c21eda0a3f
commit 9d9f263c69
1 changed files with 2 additions and 18 deletions

View File

@ -1,3 +1,4 @@
local stringx = require "pl.stringx"
local web_sanitize = require "web_sanitize"
local utils = {}
@ -34,25 +35,8 @@ function utils.map<T, U>(list: { T }, iteratee: function(value: T, position: int
return mapped
end
-- Extracted from teh Penlight Lua library.
-- Sometime Lua string.gsub can't match unescaped strings.
-- https://stackoverflow.com/a/72666170
function utils.escape(s: string): string
return (s:gsub("[%-%.%+%[%]%(%)%$%^%%%?%*]", "%%%1"))
end
function utils.replace(s: string, old: string, new: string, n: number): string
return (s:gsub(utils.escape(old), new:gsub("%%", "%%%%"), n))
end
function utils.trim(s: string): string
return s:match "^%s*(.-)%s*$"
end
function utils.sanitize_string(s: string): string
return utils.trim(
utils.replace(web_sanitize.extract_text(s), "^%s*(.-)%s*$", "%1")
)
return (stringx.strip(web_sanitize.extract_text(s)))
end
-- At some point, we should probably write a wrapper to make penlight's function work with pcalls.