Created
August 10, 2020 11:21
-
-
Save bennadel/0f7eb8f0e6619fab032403874606f672 to your computer and use it in GitHub Desktop.
Mapping Arrays-To-Structs And Structs-To-Arrays Using Mapping Functions In Lucee CFML 5.3.6.61
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
friends = [ "Sarah", "Danny", "Todd", "Vera", "Seema" ]; | |
lookup = mapArrayToStruct( | |
friends, | |
( mapTo, friend, index ) => { | |
// The mapTo() function setups the (key, value) pair in the resultant Struct. | |
mapTo( | |
friend.lcase(), | |
{ | |
initials: friend.left( 1 ).lcase(), | |
name: friend | |
} | |
); | |
} | |
); | |
dump( lookup ); | |
// ------------------------------------------------------------------------------- // | |
// ------------------------------------------------------------------------------- // | |
/** | |
* I map the given Array onto a Struct using a mapTo() operator which is passed to the | |
* given callback for each item in the array. The mapTo() function takes two arguments | |
* which are consumed as the (key,value) of the resultant struct. If the mapTo() | |
* function is NOT called for a given Array item, that Array item is excluded from the | |
* resultant mapping. The callback function is called with the following arguments: | |
* | |
* operator( mapTo, item, index, collection ) | |
* | |
* @collection I am the collection being mapped. | |
* @operator I am the operator being invoked on each collection item. | |
*/ | |
public struct function mapArrayToStruct( | |
required array collection, | |
required function operator | |
) { | |
var results = {}; | |
// This mapping closure will be passed to the operator for EACH ITEM in the given | |
// collection. It's how the items are mapped to the key/value Struct entries. | |
var mapTo = ( key, value ) => { | |
results[ key ] = ( value ?: nullValue() ); | |
}; | |
// In Lucee CFML, the CFLoop tag for Arrays can expose BOTH the INDEX and the | |
// VALUE at the same time, which allows us to iterate over the collection faster | |
// and with less complexity (when compared to a typical FOR-Loop). | |
loop | |
index = "local.index" | |
item = "local.item" | |
array = collection | |
{ | |
operator( mapTo, item, index, collection ); | |
} | |
return( results ); | |
} | |
</cfscript> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
function arrayToStructOperator( item, index ) { | |
return( [ index, item ] ); | |
} | |
</cfscript> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
function arrayToStructOperator( mapTo, item, index ) { | |
mapTo( index, item ); | |
} | |
</cfscript> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfscript> | |
lookup = { | |
sarah: { initials: "S", name: "Sarah" }, | |
danny: { initials: "D", name: "Danny" }, | |
todd: { initials: "T", name: "Todd" }, | |
vera: { initials: "V", name: "Vera" }, | |
seema: { initials: "S", name: "Seema" } | |
}; | |
friends = mapStructToArray( | |
lookup, | |
( mapTo, key, value ) => { | |
// The mapTo() function setups the item in the resultant Array. | |
mapTo( value.name ); | |
} | |
); | |
dump( friends ); | |
// ------------------------------------------------------------------------------- // | |
// ------------------------------------------------------------------------------- // | |
/** | |
* I map the given Struct onto an Array using a mapTo() operator which is passed to | |
* the given callback for each entry in the Struct. The mapTo() function takes one | |
* argument which is consumed as the (item) of the resultant Array. If the mapTo() | |
* function is NOT called for a given Struct entry, that Array item is excluded from | |
* the resultant mapping. The callback function is called with the following | |
* arguments: | |
* | |
* operator( mapTo, key, value, collection ) | |
* | |
* @collection I am the collection being mapped. | |
* @operator I am the operator being invoked on each collection entry. | |
*/ | |
public array function mapStructToArray( | |
required struct collection, | |
required function operator | |
) { | |
var results = []; | |
// This mapping closure will be passed to the operator for EACH ENTRY in the | |
// given collection. It's how the items are mapped to the Array items. | |
var mapTo = ( value ) => { | |
results.append( value ); | |
}; | |
// In Lucee CFML, the CFLoop tag for Structs can expose BOTH the KEY and the | |
// VALUE at the same time, which allows us to iterate over the collection faster | |
// and with less complexity (when compared to a typical FOR-Loop). | |
loop | |
index = "local.index" | |
item = "local.item" | |
collection = collection | |
{ | |
operator( mapTo, index, item, collection ); | |
} | |
return( results ); | |
} | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment