Project

Profile

Help

Bug #4532 » map.xsl

Dependency of al3-dictionary - Jeremy Cronk, 2020-05-08 18:47

 
<?xml version="1.0"?>
<xsl:package name="com.ivans.connect.utils.map" package-version="0.1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:array-ext="http://www.ivansinsurance.com/connect/array" xmlns:map-ext="http://www.ivansinsurance.com/connect/map"
xmlns:array="http://www.w3.org/2005/xpath-functions/array" xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:map="http://www.w3.org/2005/xpath-functions/map" exclude-result-prefixes="#all" expand-text="true" version="3.0">
<xsl:use-package name="com.ivans.connect.utils.array" package-version="0.1.0"/>
<xsl:expose visibility="public" component="function" names="map-ext:*"/>
<xsl:function name="map-ext:interlace" as="map(*)" visibility="public">
<xsl:param name="first-seq"/>
<xsl:param name="second-seq"/>
<xsl:variable name="pairs" as="array(*)*" select="array-ext:interlace($first-seq, $second-seq)"/>
<xsl:sequence select="map-ext:entries($pairs)"/>
</xsl:function>
<xsl:function name="map-ext:put-pair" as="map(*)" visibility="public">
<xsl:param name="map" as="map(*)"/>
<xsl:param name="tuple" as="array(*)"/>
<xsl:sequence select="apply(map:put($map, ?, ?), $tuple)"/>
</xsl:function>
<xsl:function name="map-ext:put-in" as="map(*)" visibility="public">
<xsl:param name="nested-map" as="map(*)"/>
<xsl:param name="key-seq" as="item()*"/>
<xsl:param name="value"/>
<xsl:variable name="key" select="head($key-seq)"/>
<xsl:variable name="seq" select="tail($key-seq)"/>
<xsl:choose>
<xsl:when test="$seq => empty()">
<xsl:sequence select="map:put($nested-map, $key, $value)"/>
</xsl:when>
<xsl:otherwise>
<xsl:sequence
select="map:put($nested-map, $key, map-ext:put-in(if($key => $nested-map() => exists()) then $nested-map($key)
else map {}, tail($key-seq), $value))"
/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="map-ext:entries" as="map(*)">
<xsl:param name="pairs" as="array(*)*"/>
<xsl:sequence select="fold-left($pairs, map {}, map-ext:put-pair#2)"/>
</xsl:function>
<!-- Merge two maps so that the left (second) one overrides the right one -->
<xsl:function name="map-ext:merge-left" as="map(*)" visibility="public">
<xsl:param name="right-map"/>
<xsl:param name="left-map"/>
<xsl:sequence select="map:merge(($right-map, $left-map), map {'duplicates': 'use-last'})"/>
</xsl:function>
<!-- Merge a map where the rightmost (first) map overrides the one on the left -->
<xsl:function name="map-ext:merge-right" as="map(*)" visibility="public">
<xsl:param name="right-map"/>
<xsl:param name="left-map"/>
<xsl:sequence select="map:merge(($right-map, $left-map), map {'duplicates': 'use-first'})"/>
</xsl:function>
<xsl:function name="map-ext:map-with-default" as="function(*)" visibility="public">
<xsl:param name="map" as="map(*)"/>
<xsl:param name="default"/>
<xsl:sequence
select="
function ($key) {
if (map:contains($map, $key)) then
$map($key)
else
$default
}"
/>
</xsl:function>
</xsl:package>
(5-5/6)