Extension:ArrayFunctions

The ArrayFunctions extension creates an additional set of pure, Parsoid-compatible parser functions that operate on arrays. The extension works by using pure functions: instead of manipulating some previously defined array, a pure parser function takes some arguments, performs some calculation, and returns the result. Arrays, and other data types, are passed around as opaque data types.

af_bool
This parser function casts a string to a boolean. This is useful for creating an array containing a boolean.

Parameters

 * value : string or boolean
 * The value to cast to a boolean.

Return values
Returns the casted boolean.

af_count
This parser functions counts the number of values in an array.

Parameters

 * array : array
 * The array to count.


 * recursive : boolean, default=false
 * Whether to count items recursively. Note that elements containing a list are also counted (see examples below).

Return values
The number of items in the array.

af_exists
This parser function checks whether the given key or index in an array exists.

Parameters

 * array : array
 * The array to check.


 * key : string or int
 * The key to check.

Return values
Returns true if  contains , false otherwise.

af_float
This parser function casts a string to a float. This is useful for creating an array containing a float.

Parameters

 * value : string or float
 * The value to cast to a float.

Return values
Returns the casted float.

af_foreach
This parser function provides a way to iterate over arrays.

Parameters

 * array : array
 * The array over which to iterate.


 * key_name : string, default=null
 * The name to use for the key.


 * value_name : string, default=null
 * The name to use for the value.


 * body : string
 * The body to return for each iteration.

Return values
Returns the resulting wikitext.

af_get
This parser function is used to index in an array.

Parameters

 * array : array
 * The array in which to index.


 * * indices : string
 * The index. Multiple indices can be given to index nested arrays.

Return values
Returns the indexed value, or the empty string if the index does not exist.

af_if
This parser function selects one of two alternatives based on the given boolean.

Parameters

 * predicate : boolean
 * The predicate.


 * consequent : string
 * The value to return if the predicate holds (i.e. is true).


 * alternative : string, default=""
 * The value to return if the predicate does not hold (i.e. is false).

Return values
Returns the consequent if the predicate holds, or the alternative if it is given and the predicate does not hold.

af_int
This parser function casts a string to an integer. This is useful for creating an array containing an integer.

Parameters

 * value : string or int
 * The value to cast to an integer.

Return values
Returns the casted integer.

af_isarray
This parser function checks if the given value is an array.

Parameters

 * value : mixed
 * The value to check.

Return values
Returns true if  is an array, false otherwise.

af_join
This parser function recursively joins the items of an array together with a given separator.

Parameters

 * array : array
 * The array to join.


 * glue : string, default=""
 * The string used to join each item. This parameter recognises the following escape sequences:
 * for spaces
 * for newlines
 * for backslashes

Return values
Returns the joined array.

af_keysort
This parser function sorts a multidimensional array based on the values of a key.

Parameters

 * array : array
 * The array to sort.


 * key : string
 * The key of the values on which the sort should be based.


 * descending : boolean, default=false
 * Whether to sort in a descending order.

Return values
Returns the sorted array.

af_list
This parser function creates a new list from the given parameters.

Parameters

 * * values : mixed
 * The values for the list.

Return values
Returns the resulting list.

af_map
This parser function applies a callback to each element of a list.

Parameters

 * array : array
 * The array to run through the callback.


 * value_name : string
 * The name to give to the value in the callback.


 * callback : string
 * The callback to apply to each element of the array.

Return values
Returns the resulting mapped array.

af_object
This parser function creates a new object from the given parameters.

Parameters

 * ** values : mixed
 * The values for the object.

Return values
Returns the resulting object.

af_print
This parser function prints the given value in a human-readable format.

Parameters

 * * values : mixed
 * The values to print.


 * end : string, default=""
 * The string to append to the end of each printed value. This parameter recognises the following escape sequences:
 * for spaces
 * for newlines
 * for backslashes

Return values
Returns the value in human-readable form.

af_push
This parser function adds the given value to the end of the list.

Parameters

 * array : array
 * The array to append the value to.


 * value : mixed
 * The value to add.

Return values
Returns the array with the value appended.

af_set
This parser function sets the given value for the given index.

Parameters

 * value : mixed
 * The value to set the index to.


 * array : array
 * To array in which to set the index.


 * * indices : string
 * The index to set. Multiple indices can be given to index nested arrays.

Return values
Returns the array with the given index set to the given value.

af_slice
This parser function extracts a slice from the given array. Keys will be reset and reordered.

Parameters

 * array : array
 * The array to take a slice from.


 * offset : integer
 * The offset at which the slice starts. If non-negative, the slice will start at this given offset. If negative, the sequence will start that far from the end of the array.


 * length : integer, optional
 * The length of the slice. If the length is given and positive, then the slice will have that many elements in it. If the length is given and negative, then the slice will stop that many elements from the end of the array. If it is omitted, then the slice will have everything from offset up until the end of the array.

Return values
The slice.

af_sort
This parser function sorts the given list.

Parameters

 * array : array
 * The array to sort.


 * descending : boolean, default=false
 * Whether to sort the list in descending order.

Return values
Returns the sorted list.

af_template
This parser function will invoke the given template and pass the values in the given array as (named) parameters.

Parameters

 * name : string
 * The name of the template to invoke. If no namespace is given, it is assumed the page is in the template namespace, otherwise the given namespace is used. The page must exist, must be includable and must be readable by the user, otherwise an error is given.

Return values
The expanded template.

af_unique
This parser function removes duplicate values from the given array. This function does not reset keys.

Parameters

 * array : array
 * The array from which to remove duplicates.

Return values
Returns the array with duplicates removed.

af_unset
This parser function removes the value associated with the given index from the array and returns the result. Numeric keys are not reset after calling this function.

Parameters

 * array : array
 * The array from which to remove the given key.


 * * indices : string
 * The index to remove. Multiple indices can be given to index nested arrays.

Return values
Returns the array with the given index removed.

Magic words
The extension defines a number of magic words (variables).

AF_EMPTY
This magic word returns the empty array.

Return values
Returns the empty array.

Scribunto
This extension is particularly useful in combination with Lua, as it can be used to create the array containing data required for the presentation of the page. This array can be exported to work with ArrayFunctions:

This module may then be invoked like so: