Module:Variables

From Dota 2 Wiki
Jump to: navigation, search

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

This module provides a Lua interface for the parser functions provided by Extension:Variables.

Usage[edit]

Function names are identical to the parser function names.

Variables.vardefineecho[edit]

Returns the value as it was stored (as a string). Should only be used to verify the stored value, not for inline declarations.

Differences[edit]

  • Returns nil instead of empty strings (use p.varexists() to check for variables holding an empty string).
  • varexists() returns a boolean instead of 1 or nil.

Tests[edit]

YesY All tests passed.

Name Expected Actual
YesY test_var_defined
YesY test_var_defined_empty
YesY test_var_defined_empty_default
YesY test_var_not_defined
YesY test_var_not_defined_default
YesY test_vardefine_boolean
YesY test_vardefine_function_throws
YesY test_vardefine_nil
YesY test_vardefine_number
YesY test_vardefine_string
YesY test_vardefine_table_throws
YesY test_vardefineecho_boolean
YesY test_vardefineecho_function_throws
YesY test_vardefineecho_nil
YesY test_vardefineecho_number
YesY test_vardefineecho_returns
YesY test_vardefineecho_string
YesY test_vardefineecho_table_throws
YesY test_varexists_false
YesY test_varexists_true


--------------------------------------------------------------------------------
-- Imports
--------------------------------------------------------------------------------

local util = require( 'libraryUtil' )
local checkType = util.checkType
local checkTypeMulti = util.checkTypeMulti

--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------

local frame

local function callParserFunction( func, args )
  if not frame then
    frame = mw.getCurrentFrame()
  end
  return frame:callParserFunction( func, args )
end

--------------------------------------------------------------------------------
-- Functions
--------------------------------------------------------------------------------

local p = {}

function p.vardefine( name, value )
  checkType( 'vardefine', 1, name, 'string' )
  checkTypeMulti( 'vardefine', 2, value, { 'boolean', 'string', 'number', 'nil' } )
  local value = tostring( value or '' )

  callParserFunction( '#vardefine', { name, value } )
end

function p.vardefineecho( name, value )
  checkType( 'vardefineecho', 1, name, 'string' )
  checkTypeMulti( 'vardefineecho', 2, value, { 'boolean', 'string', 'number', 'nil' } )
  local value = tostring( value or '' )

  return callParserFunction( '#vardefineecho', { name, value } )
end

function p.varexists( name )
  checkType( 'varexists', 1, name, 'string' )

  return ( callParserFunction( '#varexists', name ) == '1' )
end

function p.var( name, default )
  checkType( 'var', 1, name, 'string' )

  local value = callParserFunction( '#var', name )
  if value ~= '' then
    return value
  elseif default then
    return default
  else
    return nil
  end
end

--------------------------------------------------------------------------------
-- Return
--------------------------------------------------------------------------------

return p