fix(utils): use `stringx.strip` to trim string
This commit is contained in:
parent
c21eda0a3f
commit
9d9f263c69
|
@ -1,3 +1,4 @@
|
||||||
|
local stringx = require "pl.stringx"
|
||||||
local web_sanitize = require "web_sanitize"
|
local web_sanitize = require "web_sanitize"
|
||||||
|
|
||||||
local utils = {}
|
local utils = {}
|
||||||
|
@ -34,25 +35,8 @@ function utils.map<T, U>(list: { T }, iteratee: function(value: T, position: int
|
||||||
return mapped
|
return mapped
|
||||||
end
|
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
|
function utils.sanitize_string(s: string): string
|
||||||
return utils.trim(
|
return (stringx.strip(web_sanitize.extract_text(s)))
|
||||||
utils.replace(web_sanitize.extract_text(s), "^%s*(.-)%s*$", "%1")
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- At some point, we should probably write a wrapper to make penlight's function work with pcalls.
|
-- At some point, we should probably write a wrapper to make penlight's function work with pcalls.
|
||||||
|
|
Loading…
Reference in New Issue