Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created November 20, 2020 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/3b4d45f7f7bcb0823cdeeec9de7f2eb5 to your computer and use it in GitHub Desktop.
Save bennadel/3b4d45f7f7bcb0823cdeeec9de7f2eb5 to your computer and use it in GitHub Desktop.
Lists - The Unsung Heroes Of ColdFusion And Lucee CFML
<cfscript>
include "./utils.cfm";
authorization = "Basic YW5uYTppY2FuaGF6Y2hlZXNlYnVyZ2Vy";
// The Authorization header is really just a space-delimited list.
echoLine( authorization.listFirst( " " ) );
echoLine( authorization.listRest( " " ) );
echoLine();
// The Basic authentication, the payload is a Base64-encoded string that contains
// the username and password a colon-delimited list.
base64token = authorization.listRest( " " );
binaryToken = binaryDecode( base64token, "base64" );
token = charsetEncode( binaryToken, "utf-8" );
echoLine( token.listFirst( ":" ) );
echoLine( token.listRest( ":" ) );
</cfscript>
<cfscript>
include "./utils.cfm";
email = "ben@bennadel.com";
// Treating email as an @-delimited list makes plucking the local-part / recipient
// and the domain super easy!
echoLine( email.listFirst( "@" ) );
echoLine( email.listRest( "@" ) );
</cfscript>
<cfscript>
include "./utils.cfm";
clientFilename = "lucy-the-goose.gif";
// If you need to extract the file-extension from a file-name, then you can think of
// the file as being a dot-delimited list.
echoLine( clientFilename.listLast( "." ) );
</cfscript>
<cfscript>
include "./utils.cfm";
// When your application is sitting behind several layers of ingress / load-
// balancers, the user's original IP is usually passed-through as an HTTP header
// (often "X-Forwarded-For"), which is really just a comma-delimited list of address,
// the first of which is the user's actual IP address.
ip = request[ "X-Forwarded-For" ] = "1.1.1.1,2.2.2.2,3.3.3.3,4.4.4.4";
echoLine( ip.listFirst() );
echoLine( ip.listRest() );
</cfscript>
<cfscript>
include "./utils.cfm";
name = "Ben Nadel";
// Treating names as a space-delimited list is a super naive way to handle names.
// Because, names are quite complicated - and if a user hasn't explicitly told you
// which is first and which is last, you'll likely get it wrong. But, IN A PINCH,
// this can be helpful. Such as when having to convert a single-field within your app
// into multiple fields required by an external systems (such as a payment processor).
echoLine( name.listFirst( " " ) );
echoLine( name.listRest( " " ) );
echoLine();
name = "Madonna";
// The lovely thing about listRest() is that it will just return an EMPTY string if
// the given list only has one item in it.
echoLine( name.listFirst( " " ) );
echoLine( name.listRest( " " ) );
</cfscript>
<cfscript>
include "./utils.cfm";
queryString = "foo=bar&quote=soccer=life";
// To iterate over a query-string, we can use the list-based loop functionality
// because a query-string is nothing but an &-delimited list.
loop
value = "pair"
list = queryString
delimiters = "&"
{
// And, for each item within the query-string, we get a pair of values that are
// really just a =-delimited list.
echo( canonicalize( pair.listFirst( "=" ), true, true ) );
echo( " &rarr; " );
echoLine( canonicalize( pair.listRest( "=" ), true, true ) );
}
</cfscript>
<cfscript>
include "./utils.cfm";
domain = "app.bennadel.com";
// A domain can be seen as a .-delimited list of tokens.
echoLine( domain.listFirst( "." ) );
echoLine( domain.listRest( "." ) );
</cfscript>
<cfscript>
include "./utils.cfm";
siteUrl = "https://www.bennadel.com/index.cfm?site-photo=305##main-content";
// A URL can be seen as a list that has "?" and "#" delimiters.
serverPart = siteUrl.listFirst( "##" );
fragment = siteUrl.listRest( "##" );
resource = serverPart.listFirst( "?" );
queryString = serverPart.listRest( "?" );
echoLine( resource );
echoLine( queryString );
echoLine( fragment );
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment