Bước tới nội dung

Mô đun:Unicode data/matching

Từ điển mở Wiktionary
local export = {}

local module_name_aliases = {
	sc = "scripts",
	script = "scripts",
	gc = "category",
	generalcategory = "category",
}

local function get_pattern(prop)
	if type(prop) ~= "string" then
		error("prop is not a string")
	end
	local module_name, value = prop:match "^(%l+):(%a+)$"
	if not module_name then
		error("prop '" .. prop .. "' is invalid")
	end
	module_name = module_name:lower()
	module_name = module_name_aliases[module_name] or module_name
	
	return require "Module:Unicode data/patterns".make_pattern {
		args = { module = module_name, value = value }
	}
end

local function make_funcs(name, func)
	export[name] = function (str, properties)
		local pattern = properties:map(get_pattern):concat()
		return func(str, pattern)
	end
	
	export[name .. "_t"] = function (frame)
		local args = require "Module:array".shallowcopy(frame.args)
		local str = args:remove(1)
		return export[name](str, args)
	end
end

make_funcs("remove_all", function (str, pattern)
		return (mw.ustring.gsub(str, "[" .. pattern .. "]", ""))
	end)

make_funcs("contains_any", function (str, pattern)
		return mw.ustring.find(str, "[" .. pattern .. "]") ~= nil
	end)

make_funcs("filter", function (str, pattern)
		return (mw.ustring.gsub(str, "[^" .. pattern .. "]", ""))
	end)

return export