Module:CompareVersion

Jump to navigation Jump to search
This module depends on Module:Arguments, a script written in Lua.

This module implements Template:CompareVersion.

[view] [edit] [history] [refresh]The above documentation is transcluded from Module:CompareVersion/doc.
local p = {}
function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:CompareVersion/helper'
	})
	return p.second(args)
end

function split(str)
	local array = {}
	local curr = 1
	local i = 1
	local section = ""
	while string.sub(str, i, i) ~= "" do
		if string.sub(str, i, i) == "." then
			array[curr] = tonumber(section) or -1
			section = ""
			curr = curr + 1
		else
			section = section .. string.sub(str, i, i)
		end
		i = i + 1
	end
	array[curr] = tonumber(section) or -1
	return array
end

function p.second(args)
	local user_ver = split(args[1] or "")
	local curr_ver = split(args[2] or "")
	if user_ver == "" or curr_ver == "" then
		return ""
	end
	local i = 1
	local result = "1"
	while user_ver[i] ~= nil and curr_ver[i] ~= nil do
		if user_ver[i] == -1 or curr_ver[i] == -1 then
			result = "3"
			break
		end
		if curr_ver[i] > user_ver[i] then
			result = "2"
			break
		end
		if user_ver[i] > curr_ver[i] then
			result = "3"
			break
		end
		i = i + 1
	end
	if result == "1" and user_ver[i] ~= nil then
		result = "3"
	end
	if result == "3" then
		return ""
	else
		return "true"
	end
end
return p