Dota 2 Wiki
m
m
Line 8: Line 8:
 
--
 
--
 
-- @param tbl
 
-- @param tbl
local function clearEmpty( tbl )
+
local function clearEmptyValues( tbl )
 
local newTbl = {}
 
local newTbl = {}
 
for k,v in pairs( tbl ) do
 
for k,v in pairs( tbl ) do
 
if type( v ) == 'table' then
 
if type( v ) == 'table' then
newTbl[k] = clearEmpty( v )
+
newTbl[k] = clearEmptyValues( v )
 
elseif v ~= '' then
 
elseif v ~= '' then
 
newTbl[k] = v
 
newTbl[k] = v
Line 36: Line 36:
 
return
 
return
 
end
 
end
results = clearEmpty( results )
+
results = clearEmptyValues( results )
   
 
return results
 
return results

Revision as of 15:31, 10 August 2018

Documentation for Module:Cargo Jump to code ↴ [ edit | purge ]

This module processes the output of Cargo queries. Empty values are removed and in case of no results, nil is returned instead of an empty table.

Performance

The module introduces some additional overhead leading to slightly worse performance. Raw mw.ext.cargo.query queries should be used where the automatic stripping of empty strings is not needed.

Module:Cargo mw.ext.cargo
Each call was running for about 3.6e-04 seconds.
    Mean runtime for each set was 3.6e-02 seconds,
    with standard deviation of 1.8e-03 seconds,
    minimum 6.0e-06, maximum 7.0e-06.
    Total time spent was about 3.6e-01 seconds.
Relative load is estimated to 5,579.6.
Each call was running for about 2.3e-04 seconds.
    Mean runtime for each set was 2.3e-02 seconds,
    with standard deviation of 1.6e-03 seconds,
    minimum 8.0e-06, maximum 1.0e-05.
    Total time spent was about 2.3e-01 seconds.
Relative load is estimated to 2,754.4.

Syntax

Main Article: Cargo documentation

The syntax is identical to the one provided by mw.ext.cargo.


local Cargo = {}

local util = require( 'libraryUtil' )
local checkType = util.checkType
local cargoQuery = mw.ext.cargo.query

-- Clear empty string values from a given table and all subtables.
--
-- @param tbl
local function clearEmptyValues( tbl )
  local newTbl = {}
  for k,v in pairs( tbl ) do
    if type( v ) == 'table' then
      newTbl[k] = clearEmptyValues( v )
    elseif v ~= '' then
      newTbl[k] = v
    end
  end

  return newTbl
end

-- Run a Cargo query.
--
-- @param tables
-- @param fields
-- @param args
function Cargo.query( tables, fields, args )
  checkType( 'query', 1, tables, 'string' )
  checkType( 'query', 2, fields, 'string' )
  checkType( 'query', 3, args, 'table', true )

  local results = cargoQuery( tables, fields, args )
  -- Return nil if no results were found.
  if #results == 0 then
    return
  end
  results = clearEmptyValues( results )

  return results
end

return Cargo