Modul:StringFunc

Dokumentasi untuk modul ini dapat dibuat di Modul:StringFunc/doc

local p = {}

--[[ 
Strip

This function Strips charecters from string

Usage:
{{#invoke:StringFunc|strip|source_string|charecters_to_strip|plain_flag}}

Parameters
	source: The string to strip
	chars:  The pattern or list of charecters to strip from string, replaced with ''
	plain:  A flag indicating that the chars should be understood as plain text. defaults to true.

Leading and trailing whitespace is also automatically stripped from the string. 
]]
function p.strip( frame )
	local new_args = p._getParameters( frame.args,  {'source', 'chars', 'plain'} )
	local source_str = new_args['source'] or '';
	local chars = new_args['chars'] or '';
	source_str = mw.text.trim(source_str);
	if source_str == '' or chars == '' then 
		return source_str;
	end
	local l_plain = p._getBoolean( new_args['plain'] or true );
	if l_plain then
		chars = p._escapePattern( chars );
	end
	local result;
	result = mw.ustring.gsub(source_str, "["..chars.."]", '')
	return result;
end


--[[
Split

This function Splits a string based on a separator, returns nth substring based on count.

Usage:
{{#invoke:StringFunc|split|source_string|separator|count}}

Parameters:
	source:    The string to return a subset of
	separator: The string to split on 
	count:     The nth substring based on the separator to return
]]
function p.split( frame )
	local new_args = p._getParameters( frame.args, {'source', 'separator', 'count'} )
	local source_str = new_args['source'] or '';
	local separator = new_args['separator'] or '';
	local separator_len = mw.ustring.len(separator);
	if source_str == '' or separator == '' then
		return source_str;
	end
	local ret_count = tonumber( new_args['count'] ) or 1;
	if ret_count < 1 then
		return "";
	end
	local start = 1;
	local iter = mw.ustring.find(source_str, separator, start, true);
	if iter == nil then
		if ret_count == 1 then
			return source_str;
		else
			return "";
		end
	else
		iter = iter - 1;
	end
	if ret_count == 1 then
		return mw.ustring.sub( source_str, start, iter);
	end
    for i=2, ret_count do
    	start = iter+separator_len + 1;
    	iter = mw.ustring.find(source_str, separator, start, true);
    	if iter == nil then
    		if ret_count == i then
    			return mw.ustring.sub(source_str, start, mw.ustring.len(source_str));
    		else
    			return "";
    		end
    	else
    		iter = iter - 1;
    	end
    end
    return mw.ustring.sub( source_str,start,iter); 
end

function p.isNumber( frame )
	local new_args = p._getParameters( frame.args, {'source'} )
	local source_str = new_args['source'] or ''
	if source_str == '' then
	   return "false"
	end
	if tonumber(source_str) == nil and tonumber(string.gsub(source_str, ",", ".", 1) .. '') == nil then
		return "false"
	end
	return "true"
end

-- Argument list helper function, as per Module:String
function p._getParameters( frame_args, arg_list)
	local new_args = {};
	local index = 1;
	local value;
	for i, arg in ipairs( arg_list ) do
		value = frame_args[arg]
		if value == nil then
			value = frame_args[index];
			index = index + 1;
		end
		new_args[arg] = value;
	end
	return new_args;
end

-- Escape Pattern helper function so that all charecters are treated as plain text, as per Module:String
function p._escapePattern( pattern_str)
	return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" );
end

-- Helper Function to interpret boolean strings, as per Module:String
function p._getBoolean( boolean_str ) 
	local boolean_value;
	
	if type( boolean_str ) == 'string' then
		boolean_str = boolean_str:lower();
		if boolean_str == 'false' or boolean_str == 'no' or boolean_str == 'O' or boolean_str == '' then
			boolean_value = false;
		else
			boolean_value = true;
		end
	elseif type(boolean_str) == 'boolean' then
			boolean_value = boolean_str;
	else
		error( 'No boolean value found' );
	end
	return boolean_value
end

return p