Module:utilities/scribunto parameter key

Wiktionary වෙතින්

Takes a parameter name as an input, and returns the Scribunto-normalized form (i.e. the key that that parameter would have in a frame.args table). For example, "1" is normalized to 1 (a number), and " foo " is normalized to "foo". If the input is not a string, it is returned unchanged.

Strings are trimmed with Module:string/php trim, and then converted to numbers if:

  1. They are integers, with no decimals (2.0) or leading zeroes (02).
  2. They are ≤ 253 and ≥ -253.
  3. For positive values, they do not have a leading + sign.

Note: Lua integers are only accurate to 253 - 1, so 253 and -253 have to be specifically checked for, since 2^53 == 2^53 + 1 evaluates to true.


local match = string.match
local tonumber = tonumber
local trim = require("Module:string/php trim")
local type = type

return function(key)
	if type(key) ~= "string" then
		return key
	end
	key = trim(key)
	if match(key, "^-?[1-9]%d*$") then
		local num = tonumber(key)
		return (
			num <= 9007199254740991 and num >= -9007199254740991 or
			key == "9007199254740992" or
			key == "-9007199254740992"
		) and num or key
	elseif key == "0" then
		return 0
	end
	return key
end
"https://si.wiktionary.org/w/index.php?title=Module:utilities/scribunto_parameter_key&oldid=164355" වෙතින් සම්ප්‍රවේශනය කෙරිණි