Difference between revisions of "Module:StringHelpers"

From Data JW United
Jump to navigation Jump to search
Line 5: Line 5:
 
if (dashIndex ~= nil)  
 
if (dashIndex ~= nil)  
 
then
 
then
if (tonumber(string.sub(param.args[1], dashIndex - 3, dashIndex -3)))
+
if (dashIndex - 3 > 0 and tonumber(string.sub(param.args[1], dashIndex - 3, dashIndex -3)))
 
then
 
then
 
return "-3 " .. string.sub(param.args[1], dashIndex - 3, dashIndex - 1);
 
return "-3 " .. string.sub(param.args[1], dashIndex - 3, dashIndex - 1);

Revision as of 18:15, 12 April 2020

Module supports numbers with max 3 digits.

Tests

Simple question

Expected Actual
150 150
15 15
7 7
150 150
15 15
7 7

Complex question

Expected Actual
6 -1 6
7 -1 7
10 -2 10
99 -2 99
101 -3 101
6 -1 6
7 -1 7
10 -2 10
99 -2 99
101 -3 101

local p = {}

function p.getParagraphFromQuestion(param)
	dashIndex = string.find(param.args[1], '-', 1, true);
	if (dashIndex ~= nil) 
		then
			if (dashIndex - 3 > 0 and tonumber(string.sub(param.args[1], dashIndex - 3, dashIndex -3)))
				then
					return "-3 " .. string.sub(param.args[1], dashIndex - 3, dashIndex - 1);
			end
			if (tonumber(string.sub(param.args[1], dashIndex - 2, dashIndex -2)))
				then
					return "-2 " .. string.sub(param.args[1], dashIndex - 2, dashIndex - 1);
			end
			if (tonumber(string.sub(param.args[1], dashIndex - 1, dashIndex -1)))
				then
					return "-1 " .. string.sub(param.args[1], dashIndex - 1, dashIndex - 1);
			end
		else
			if (tonumber(string.sub(param.args[1], string.len(param.args[1]) - 2)))
				then 
					i = string.len(param.args[1]) - 2;
					return string.sub(param.args[1], i)
				end
			if (tonumber(string.sub(param.args[1], string.len(param.args[1]) - 1)))
				then 
					i = string.len(param.args[1]) - 1;
					return string.sub(param.args[1], i)
			else 
				return string.sub(param.args[1], string.len(param.args[1]))
			end
	end
end

return p