Module:Languages/javascript-interface/sandbox

From The Languages of David J. Peterson
Jump to navigation Jump to search

Documentation for this module may be created at Module:Languages/javascript-interface/sandbox/documentation

local export = {}

function export.get_language_by_prefix(frame)
	local prefix = frame.args[1]
	if not prefix or prefix == "" then
		return
	end
	
	local Map = require "Module:User:Erutuon/lang_stuff/map"
	local name_to_code = require "Module:languages/canonical names"
	
	local lower, find
	if prefix:find("[\128-\255]") then
		lower, find = mw.ustring.lower, mw.ustring.find
	else
		lower, find = string.lower, string.find
	end
	
	return require "Module:JSON".toJSON(Map:new(name_to_code)
		:filter(function (code, name)
			return find(lower(name), "^" .. prefix) ~= nil
		end)
		:map(function (code, name)
			return { name, code }
		end)
		:values())
end

return export