@DeletedUser41170185 (talk | contribs) (p.var now returns nil instead of empty strings; Added a default option to p.var; Renamed h.parser_function to h.callParserFunction) |
@DeletedUser41170185 (talk | contribs) mNo edit summary |
||
Line 3: | Line 3: | ||
-------------------------------------------------------------------------------- |
-------------------------------------------------------------------------------- |
||
− | local |
+ | local util = require( 'libraryUtil' ) |
+ | local checkType = util.checkType |
||
+ | local checkTypeMulti = util.checkTypeMulti |
||
-------------------------------------------------------------------------------- |
-------------------------------------------------------------------------------- |
||
Line 12: | Line 14: | ||
function h.callParserFunction( func, args ) |
function h.callParserFunction( func, args ) |
||
⚫ | |||
− | if not frame then |
||
− | frame = mw.getCurrentFrame() |
||
− | end |
||
− | |||
⚫ | |||
end |
end |
||
Line 26: | Line 24: | ||
function p.vardefine( name, value ) |
function p.vardefine( name, value ) |
||
− | + | checkType( 'vardefine', 1, name, 'string' ) |
|
− | + | checkTypeMulti( 'vardefine', 2, value, { 'boolean', 'string', 'number', 'nil' } ) |
|
+ | local value = tostring( value or '' ) |
||
− | h.callParserFunction( '#vardefine', { name, |
+ | h.callParserFunction( '#vardefine', { name, value } ) |
end |
end |
||
function p.vardefineecho( name, value ) |
function p.vardefineecho( name, value ) |
||
− | + | checkType( 'vardefineecho', 1, name, 'string' ) |
|
− | + | checkTypeMulti( 'vardefineecho', 2, value, { 'boolean', 'string', 'number', 'nil' } ) |
|
+ | local value = tostring( value or '' ) |
||
− | return h.callParserFunction( '#vardefineecho', { name, |
+ | return h.callParserFunction( '#vardefineecho', { name, value } ) |
end |
end |
||
function p.varexists( name ) |
function p.varexists( name ) |
||
− | + | checkType( 'varexists', 1, name, 'string' ) |
|
return ( h.callParserFunction( '#varexists', name ) == '1' ) |
return ( h.callParserFunction( '#varexists', name ) == '1' ) |
||
Line 46: | Line 46: | ||
function p.var( name, default ) |
function p.var( name, default ) |
||
− | + | checkType( 'var', 1, name, 'string' ) |
|
local value = h.callParserFunction( '#var', name ) |
local value = h.callParserFunction( '#var', name ) |
Revision as of 07:43, 6 August 2018
See also: Extension:Variables
This module provides a Lua interface for the parser functions provided by Extension:Variables.
Usage
Function names are identical to the parser function names.
Variables.vardefineecho
Returns the value as it was stored (as a string). Should only be used to verify the stored value, not for inline declarations.
Differences
- Returns
nil
instead of empty strings (usep.varexists()
to check for variables holding an empty string). varexists()
returns a boolean instead of1
ornil
.
Tests
All tests passed.
Name | Expected | Actual | |
---|---|---|---|
test_var_defined | |||
test_var_defined_empty | |||
test_var_defined_empty_default | |||
test_var_not_defined | |||
test_var_not_defined_default | |||
test_vardefine_boolean | |||
test_vardefine_function_throws | |||
test_vardefine_nil | |||
test_vardefine_number | |||
test_vardefine_string | |||
test_vardefine_table_throws | |||
test_vardefineecho_boolean | |||
test_vardefineecho_function_throws | |||
test_vardefineecho_nil | |||
test_vardefineecho_number | |||
test_vardefineecho_returns | |||
test_vardefineecho_string | |||
test_vardefineecho_table_throws | |||
test_varexists_false | |||
test_varexists_true |
--------------------------------------------------------------------------------
-- Imports
--------------------------------------------------------------------------------
local util = require( 'libraryUtil' )
local checkType = util.checkType
local checkTypeMulti = util.checkTypeMulti
--------------------------------------------------------------------------------
-- Helper functions
--------------------------------------------------------------------------------
local h = {}
function h.callParserFunction( func, args )
return mw.getCurrentFrame():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 '' )
h.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 h.callParserFunction( '#vardefineecho', { name, value } )
end
function p.varexists( name )
checkType( 'varexists', 1, name, 'string' )
return ( h.callParserFunction( '#varexists', name ) == '1' )
end
function p.var( name, default )
checkType( 'var', 1, name, 'string' )
local value = h.callParserFunction( '#var', name )
if value ~= '' then
return value
elseif default then
return default
else
return nil
end
end
--------------------------------------------------------------------------------
-- Return
--------------------------------------------------------------------------------
return p