Map functions
Different languages have different syntax for getting substrings and pushing element to array. Gutt provides solid common functions for working with strings, numbers and maps.
All this functions is just a declaration. In real life stringifiers and platforms may return different values. I hope there is nothing critical, but i have to warning you.
Especially it is sensible for such kind of structure as maps. Especially if you mix ordinary arrays with objects or associative arrays. Please, don't mix them.
If you use once key for declaration and your map is going to be object or associative array. The name depends of platform.
The syntax of maps is simple. It's a chain of values wrapped with square brackets and separated with comma. You may set key for value. It could be a string or a variable. There are some examples of defenition:
<variable name={$array} value={1, 2, 3, 4} />
<variable name={$array} value={'a', 'b', 'c', 'd'} />
<variable name={$array} value={1, 'b', 3, 'd'} />
Access to elements cound be in two ways. If name of key contains digitss or letters of latin alphabet then you may use dot. Another way you wrap key name in square brackets. If key name places in variable you write variable name wrapped in square brackets as well.
{$array.a}
{$simple-map['complicated key']}
{$another-map[$key-name]}
There are also two ways to create range arrays. It's an array of numbers. From first number to second. It could be opened or closed. Look at examples:
<variable name={$array} value={0..5} /><!-- 0, 1, 2, 3, 4, 5 -->
<variable name={$array} value={0...5} /><!-- 0, 1, 2, 3, 4 -->
<variable name={$array} value={9..5} /><!-- 9, 8, 7, 6, 5 -->
<variable name={$array} value={9...5} /><!-- 9, 8, 7, 6 -->
<variable name={$end} value={10} />
<variable name={$array} value={6..$end} /><!-- 6, 7, 8, 9, 10 -->
<variable name={$array} value={$end...5} /><!-- 10, 9, 8, 7, 6 -->
arr_join#
arr_join($map, $joiner)
Returns string of joined map's items
Parameters
$map
the map to use.
$joiner
the string to join with.
Examples
{arr_join([1, 2, 3, 4, 5], ', ')}
<!-- 1, 2, 3, 4, 5 -->
arr_keys#
arr_keys($map)
Returns map of keys
Parameters
$map
the map to use.
Examples
{arr_join(arr_keys([1, 2, 3, 'a': 5, 'b', [3]]), ', ')} />
<!-- 0, 1, 2, 3, 4, a -->
arr_values#
arr_values($map)
Returns map of values.
Parameters
$map
the map to use.
Examples
{arr_join(arr_values([1: 'a', 2: 'b', 3: 'c', 4: 'd']), ', ')} />
<!-- a, b, c, d -->
arr_contain#
arr_contain($map, $element)
Returns true if $map
contains $element
Parameters
$map
the map to use.
$element
the searching element.
Examples
<if test={arr_contain([1, 2, 3, 4, 5], 3)}/>contains</if>
<!-- contains -->
arr_len#
arr_len($map)
Returns array length.
Parameters
$map
the map to use.
Examples
{arr_len([1, 2, 3, 4, 5])}
<!-- 5 -->
arr_push#
arr_push($map, $element)
This doesn't return anything. It mutates origin one.
This function puts $lement
as last element to $map
.
Parameters
$map
the map to use.
$element
the element to push.
Examples
<variable name={$arr} value={[1, 2, 3, 4, 5]} />
{arr_push($arr, 6)}
{arr_join($arr, ', ')}
<!-- 1, 2, 3, 4, 5, 6 -->
arr_unshift#
arr_unshift($map, $element)
This doesn't return anything. It mutates origin one.
This function puts $lement
as first element to $map
.
Parameters
$map
the map to use.
$element
the element to push.
Examples
<variable name={$arr} value={[1, 2, 3, 4, 5]} />
{arr_unshift($arr, 0)}
{arr_join($arr, ', ')}
<!-- 0, 1, 2, 3, 4, 5 -->
arr_pop#
arr_pop($map)
Returns last element from $map
. This function mutates origin
$map
— last element will
be removed form origin $map
.
Parameters
$map
the map to use.
Examples
<variable name={$arr} value={[1, 2, 3, 4, 5]} />
{arr_pop($arr)}
{arr_join($arr, ', ')}
<!-- 5 -->
<!-- 1, 2, 3, 4 -->
arr_shift#
arr_shift($map)
Returns first element from $map
. This function mutates origin
$map
— first element will be removed form origin $map.
Parameters
$map
the map to use.
Examples
<variable name={$arr} value={[1, 2, 3, 4, 5]} />
{arr_shift($arr)}
{arr_join($arr, ', ')}
<!-- 1 -->
<!-- 2, 3, 4, 5 -->
arr_rand#
arr_rand($map, $element)
Returns random element from $map
. This function doesn't mutates origin
$map
.
Parameters
$map
the map to use.
Examples
<variable name={$arr} value={[1, 2, 3, 4, 5]} />
{arr_rand($arr)}
<!-- 4 -->
arr_slice#
arr_slice($map, $start[, $length])
Returns submap from $start
index with $length
. Without
$length
full rest will be returned.
Function doesn't mutate origin $map
. Be careful with this constructions.
Different platforms works very different.
Parameters
$map
the map to use.
$start
the start index.
$length
the length.
Examples
<variable name={$arr} value={[1, 2, 3, 4, 5]} />
{arr_join(arr_slice($arr, 1, 2), ', ')}
{arr_join($arr, ', ')}
<!-- 2, 3 -->
<!-- 1, 2, 3, 4, 5 -->
arr_splice#
arr_splice($map, $start[, $length])
Returns submap from $start
index with $length
. Without
$length
full rest will be returned. Function mutates origin $map
.
This elements will be removed from origin $map
. Be careful with this
constructions. Different platforms works very different.
Parameters
$map
the map to use.
$start
the start index.
$length
the length.
Examples
<variable name={$arr} value={[1, 2, 3, 4, 5]} />
{arr_join(arr_splice($arr, 1, 2), ', ')}
{arr_join($arr, ', ')}
<!-- 2, 3 -->
<!-- 1, 4, 5 -->
arr_splice#
arr_splice($map, $start[, $length])
Returns submap from $start
index with $length
. Without
$length
full rest will be returned. Function mutates origin $map
.
This elements will be removed from origin $map
. Be careful with this
constructions. Different platforms works very different.
Parameters
$map
the map to use.
$start
the start index.
$length
the length.
Examples
<variable name={$arr} value={[1, 2, 3, 4, 5]} />
{arr_join(arr_splice($arr, 1, 2), ', ')}
{arr_join($arr, ', ')}
<!-- 2, 3 -->
<!-- 1, 4, 5 -->
arr_pad#
arr_pad($map, $length, $element)
Returns padded $map
at the end or begin of $map
for
$length
with $element
. If $length
positive it is
going to be end. If you user negative then it is going to be begin. Function doesn't
mutate origin $map
.
Parameters
$map
the map to use.
$length
the length.
$element
the element to pad with.
Examples
<variable name={$arr} value={[1, 2, 3, 4, 5]} />
{arr_join(arr_pad($arr, 7, 0), ', ')}
{arr_join(arr_pad($arr, -7, 0), ', ')}
<!-- 1, 2, 3, 4, 5, 0, 0 -->
<!-- 0, 0, 1, 2, 3, 4, 5 -->
arr_sort#
arr_sort($map)
Returns sorted $map
.
Parameters
$map
the map to use.
Examples
<variable name={$arr} value={[2, 5, 4, 1, 3]} />
{arr_join(arr_sort($arr), ', ')}
<!-- 1, 2, 3, 4, 5 -->
arr_reverse#
arr_reverse($map)
Returns reversed $map
.
Parameters
$map
the map to use.
Examples
<variable name={$arr} value={[2, 5, 4, 1, 3]} />
{arr_join(arr_reverse($arr), ', ')}
<!-- 3, 1, 4, 5, 2 -->
arr_sort_reverse#
arr_sort_reverse($map)
Returns reversed $map
.
Parameters
$map
the map to use.
Examples
<variable name={$arr} value={[2, 5, 4, 1, 3]} />
{arr_join(arr_sort_reverse($arr), ', ')}
<!-- 5, 4, 3, 2, 1 -->
arr_unique#
arr_unique($map)
Returns map with unique elements from $map
. Be careful with difficult
structured elements.
Parameters
$map
the map to use.
Examples
<variable name={$arr} value={[2, 1, 3, 1, 5, 4, 3, 3, 5]} />
{arr_join(arr_unique($arr), ', ')}
<!-- 2, 1, 3, 5, 4 -->
arr_key#
arr_key($map, $element)
Returns first finded key from $map
with searching $element
Parameters
$map
the map to use.
$element
element for search
Examples
<variable name={$arr} value={[2, 1, 3, 1, 5, 4, 3, 3, 5]} />
{arr_key($arr, 3)}
<!-- 2 -->