Wikipedia:Lua/Modul/Expr/en

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
Vorlagenprogrammierung Diskussionen Lua Test Unterseiten
Modul Deutsch English

Modul: Dokumentation

Expr – Module with functions for any kind of expression in addition to the functionality offered by #expr parser functions.

The results of those Lua functions are supposed to be used within other computations or conditions. Therefore they need to be numbers rather than text and leave no way to communicate any error message nor triggering a maintenance category.

Functions for templates

[Quelltext bearbeiten]

The standard functions are supposed to be used in #invoke and retrieve those parameters. Whitespace ahead and after any content is ignored.

average
Calculate average
  • 1 – First number
  • 2 – Second number
  • parse(optional) number format in transclusions
  • 3(optional) Further number
  • 4 usw. – (optional) Further numbers, as many as desired
The average is always at least 0 and invalid values will be ignored.
base62
Coding und decoding according to Base62.
  • 1 – figure to be converted
  • 2(optional) D2B for decimal-into-base62; default: base62-into-decimal
crossTotal
Calculate sum of digits
  • 1 – positive number, digits only (minus sign and decimal fraction would be ignored)
Yields at least 0.
decimal2minsec
Coordinate value conversion from decimal to degrees / minutes / seconds
  • 1 – decimal value
  • 2(optional) number of fractional digits after separator in seconds (default: none)
  • parse(optional) number format of decimal value
Yields non-breaking <span> element with degrees / minutes / seconds, else 0
figure
Test figure for validity and convert if possible
Yields nothing if invalid.
max
Retrieve maximum of a set of figures.
All unnamed parameters are used for extremum calculation.
  • Parameter values may be empty or can contain expressions, which can be evaluated by #expr parser function.
  • Both ASCII minus U+002D and U+2212 &minus; are accepted.
  • Options are available as named parameters:
    • minus – minus sign coding in result; see below
    • zeroBlank=1 – present zero as empty value
The return value is an empty string (nothing), if no parameter at all or only empty values were provided. An error message for invalid #expr might occur as well. Otherwise that is the determined number in the specified formatting.
min
Retrieve minimum in the same way as max.
minsec2decimal
Coordinate value conversion from degrees / minutes / seconds to decimal number
  • 1 – degrees
  • 2(optional) minutes
  • 3(optional) seconds
  • 4(optional) lettter for compass direction
  • parse(optional) number format in transclusions
modulo
Modulo, remainder
  • 1 – current value (dividend)
  • 2 – unit value (base, divisor) non-zero
  • parse(optional) number format in transclusions
percent
Retrieve percentage
  • 1 – part
  • 2 – base value (related unit, 100 %) non-zero
  • 3(optional) number of fractional digits after separator (default: none)
  • 4(optional) keep trailing zeros in fractional digits
  • parse(optional) number format in transclusions
  • low(optional) result format, 1 for “computer interface” (default: local Wiki).
The result ist terminated by % and therfore the number is no longer subject to formatting.
Ramanujan
random
(Pseudo-) random number (integer)
  • 1(optional) base, standardization, number of values (default: 100), integer >1.
Results in integer numbers beginning at (including) zero, therefore always less than number of values.
Each transclusion in page yields an independent and mostly different result.
seed numbers for ensuring different values in page are not necessary.
sum
Calculate sum
  • 1 – First number
  • 2 – Second number
  • parse(optional) number format in transclusions
  • 3(optional) Further number
  • 4 usw. – (optional) Further numbers, as many as desired
The sum is always at least 0 and invalid values will be ignored.
failsafe
Version management

The Failsafe interface is heading for version management of globally distributed Lua modules. It enables modules equipped with this interface to

  • ensure, that a library module required by a template or another module available as local copy does support certain functionality, or complain if not.
  • administrate global updating and linking of module codes via Wikidata.

The Failsafe interface is present both at template level and for direct Lua access.

The functions in detail are (not all supported yet completely by every library):

Parameter
Value Result current
nothing
false
local version ID »2022-09-12«
Minimal version version ID required at least
date in ISO format

It will be compared whether the current local implementation matches this version or later.

  • empty, if minimal version not achieved
  • 2001-01-01 → »2022-09-12«
  • 2099-01-01 → »«
wikidata version ID of global upstream
  • version ID at Wikidata
  • local, if not found there
»2022-09-12«
item ID of the Wikidata item
  • empty if not defined
Q54991461
~ Corresponding version ID locally and registered at Wikidata
  • empty, if up to date
  • version ID at Wikidata, if not equal
»«
@ Is the current (module) page linked correctly with Wikidata item?
  • empty, if linked to the item which is supposed
  • Iitem ID, if not
The return value is in template programming empty or under Lua false, otherwis a non-empty string as described.

Direct transclusion in templates

[Quelltext bearbeiten]
TemplateAverage
Like average, but use all parameters of the template around.
TemplateBooland
Yields 1, if all parameters are “something”, else “nothing”.
Use parameters of the template around.
TemplateBoolor
Yields 1, if at least one parameter is “something”, else “nothing”.
Use parameters of the template around.
TemplateMax
Like max, but specific to Template:Max – use all parameters of the template around.
TemplateMin
Like min, but specific to Template:Min – use all parameters of the template around.
TemplateSum
Like sum, but use all parameters of the template around.

Unicode minus

[Quelltext bearbeiten]

The minus sign is treated as follows:

  • On input values they may be used at will.
  • If any input value contains a Unicode minus U+2212, this encoding will be used in the result.
  • Optional minus=1
    Use Unicode minus only for resulting string.
  • Optional minus=-
    Use ASCII hyphen only for resulting string.

A result with Unicode minus is by nature always a string.

Number format in transclusions

[Quelltext bearbeiten]

An optional parameter permits variable interpretation of input values.

  • The default is . with the meaning “computer calculation format”.
  • The meaning of “computer calculation format” is: . as decimal separator, minus sign in ASCII, no grouping (thousands). Alternatively the E exponential representation may be used.

The supported input format, if provided, consists of one til three codes mgd with the components:

  • d – mandatory if used: decimal separator. Permitted values: . or , (only at last position).
  • m – minus sign may be used as “typographic” (Unicode U+2212) when - (only at first position).
  • g – number grouping (thousands), even following decimal separator, permitted according to subsequent table(optional).
Codes for number grouping
g Meaning
, comma, used in angloamerican formatting
. period; default formatting in German Wikipedia
' ASCII apostrophe; common in Switzerland
', ASCII apostrophe or comma
'. ASCII apostrophe or period
U+20 ASCII space

Not recommended as number format, but may be used for analysis.

U+A0 non-breaking space
U+202F narrow non-breaking space
%s ASCII or (even narrow) non-breaking space
.%s period or ASCII space or (even narrow) non-breaking space
'%s ASCII apostrophe or ASCII space or (even narrow) non-breaking space
'.%s period or ASCII apostrophe or ASCII space or (even narrow) non-breaking space

On input HTML entities may be used alternatively for number grouping.

  • “Scientific” or exponential representation does require always “computer calculation format”: e oder E, integer exponent, + may precede mantissa and/or exponent.

Examples:

  • ,. – classical angloamerican format (ASCII)
  • -'.%s, – broadest acceptance of German formats, typographic minus possible

Examples (test page)

[Quelltext bearbeiten]

A test page illustrates practical use.

Functions for Lua modules (API)

[Quelltext bearbeiten]

All functions documented above may be used via require() in other Modules:

local lucky, Expr = pcall( require, "Module:Expr" )
if type( Expr ) == "table" then
    Expr = Expr()
else
    -- Error; Expr contains error message
    return "<span class='error'>" .. Expr .. "</span>"
end

Then there is available:

Expr.average( array, ask )
  • array – sequence table, with string or number items
  • asknumber format of input
returns: number, at least 0
Expr.base62( adjust )
  • adjuststring or number
    • number – code in base62 (string)
    • string – dekcode in number
Expr.crossTotal( amount )
  • amountstring or number
returns: number, sum of digits in integer number, at least 0
Expr.decimal2minsec( amount, align, ask )
  • amount – decimal coordinate, string or number
  • align – number of decimal digits of seconds, string or number (default: 0)
  • asknumber format of decimal number
returns: mw.html object if success, else string 0
Expr.figure( amount, ask )
returns: number, or false if invalid
Expr.minsec2decimal( aDeg, aMin, aSec, alter, ask )
  • aDeg – degree, string or number
  • aMin – minutes, string or number or nothing
  • aSec – seconds, string or number or nothing
  • alter – letter for compass direction, like S or W, or nothing
  • asknumber format of input
returns: number, 0 if invalid
Expr.modulo( amount, adjust, ask )
  • amount – figure, string or number
  • adjust – base, string or number
  • asknumber format of input
returns: number, 0 if invalid
Expr.percent( amount, all, align, after, ask, allow, frame )
  • amount – part, string or number
  • adjust – base, string or number
  • align – number of fractional digits after separator, string or number (default: none)
  • aftertrue, to keep trailing zeros in fractional digits
  • asknumber format of input
  • allow – result format, true, for „computational format“ (default: local Wiki).
  • frame – object, if available
The result is terminated by % and the number itself is not accessible for formatting.
  • number 0 if invalid
Expr.sum( array, ask )
  • array – sequence table, with string or number items
  • asknumber format of input
Two results:
  1. number, with sum, at least 0
  2. number, of valid summands, at least 0
Expr.failsafe( atleast )
(like template)

General library; no limitations.

Dependencies

[Quelltext bearbeiten]

None.