Module:User:Erutuon/split language data modules

Hello, you have come here looking for the meaning of the word Module:User:Erutuon/split language data modules. In DICTIOUS you will not only get to know all the dictionary meanings for the word Module:User:Erutuon/split language data modules, but we will also tell you about its etymology, its characteristics and you will know how to say Module:User:Erutuon/split language data modules in singular and plural. Everything you need to know about the word Module:User:Erutuon/split language data modules you have here. The definition of the word Module:User:Erutuon/split language data modules will help you to be more precise and correct when speaking or writing your texts. Knowing the definition ofModule:User:Erutuon/split language data modules, as well as those of other words, enriches your vocabulary and provides you with more and better linguistic resources.

Template:documentation

local export = {}

-- Version of deepcopy from ] that doesn't preserve
-- references to the same table. If tables are recursive, this will cause
-- a stack overflow!
local function deepcopy(orig, level)
	if type(orig) == 'table' then
		local copy = {}
		for orig_key, orig_value in pairs(orig) do
			copy = deepcopy(orig_value)
		end
		return copy
	else -- number, string, boolean, etc
		return orig
	end
end

-- Load data module containing multiple language data tables.
-- mw.text.jsonEncode converts integer keys to strings if the data table
-- contains any string keys. Have to convert them back.
function export.load_json_language_data(json)
	local data = mw.text.jsonDecode(json)
	local fixed_data = {}
	for code, old_table in pairs(data) do
		-- Key 1 (canonical name) is always present in the original
		-- language data, so key "1" is always present in old_table
		-- if it was decoded from a JSON object.
		if old_table then
			local new_table = {}
			for k, v in pairs(old_table) do
				k = tonumber(k) or k
				new_table = v
			end
			fixed_data = new_table
		else
			fixed_data = old_table
		end
	end
	return fixed_data
end

function export.split(key_maker)
	local tables_by_key = {}
	for code, data in pairs(require "Module:languages/data/all") do
		local key = key_maker(code, data)
		local subtable = tables_by_key
		if not subtable then
			subtable = {}
			tables_by_key = subtable
		end
		-- mw.text.jsonEncode refuses to directly encode the language data
		-- because of "circular references", which probably means
		-- the scripts fields that refer to the same tables containing
		-- {"Latn"}, {"Cyrl"}, {"Arab"}.
		-- Sequence tables (only canonical name, Wikidata item, and family)
		-- are encoded as JSON arrays, others as objects.
		-- Thus the number-indexed values will be under either string or
		-- number fields. Thus keys should be processed using
		-- tonumber(key) or key
		-- or the equivalent on the other side.
		subtable = deepcopy(data)
	end
	return mw.text.jsonEncode(tables_by_key)
end

function export.split_by_two_letter_prefix(frame)
	return export.split(
		function(code)
			return code:sub(1, 2)
		end)
end

function export.show(frame)
	local all_languages = export.split_by_two_letter_prefix()
	return "length: " .. #all_languages
		.. "\n\n"
		.. all_languages
end

return export