Skip to content

Instantly share code, notes, and snippets.

Avatar

James Moberg JamoCA

View GitHub Profile
@JamoCA
JamoCA / pretty-print-xml.cfm
Last active January 24, 2023 19:39
Adobe ColdFusion UDF to convert valid XML (string or object) to a pretty-print XML string. (Doesn't work with Lucee CFML.)
View pretty-print-xml.cfm
<cfscript>
// borrowed from https://gist.github.com/aviflax/725761/5887c83a695cd0d7a9be70c9c41c08e59c6611a6
// 2023-01-24 Rewritten in cfscript; Updated to use isxml/isxmldoc; Works w/CF10+, but not Lucee CFML.
// Tweet: https://twitter.com/gamesover/status/1617969485855752192
string function prettyXml(required any xml) hint="I convert valid XML (string or object) to a pretty-print XML string" {
if (isxml(arguments.xml)){
local.xmlstring = createobject("java", "java.io.StringReader").init(javacast("string", arguments.xml));
local.document = createobject("java", "org.jdom.input.SAXBuilder").init().build(local.xmlstring);
} else if (isxmldoc(arguments.xml)){
local.document = createobject("java", "org.jdom.input.DOMBuilder").init().build(arguments.xml);
@JamoCA
JamoCA / Instrumenter.cfc
Created January 18, 2023 20:07
ColdFusion 2016 compatible version of Ben Nadel's Instrumenter.CFC
View Instrumenter.cfc
component
output = false
hint = "I instrument a given ColdFusion component."
{
/*
2023-01-18 ColdFusion 2016 compatible version of Ben Nadel's Instrumenter.CFC
https://www.bennadel.com/blog/4390-dynamically-instrumenting-coldfusion-component-methods-with-gettickcount-to-locate-performance-bottlenecks.htm
https://gist.github.com/bennadel/2dac819075dfce0d9b993f8ea343707c#file-instrumenter-cfc
@JamoCA
JamoCA / getPercentageGraph.cfm
Created January 17, 2023 00:15
CFML version of reddit r/ProgrammerHumor code
View getPercentageGraph.cfm
<cfscript>
/* 2023-01-16 https://twitter.com/PR0GRAMMERHUM0R/status/1615061348928339968 https://www.reddit.com/r/programmerhumor/comments/10dh6x1
/* It's real code: https://github.com/MinBZK/woo-besluit-broncode-digid-app/blob/ad2737c4a039d5ca76633b81e9d4f3f9370549e4/Source/DigiD.iOS/Services/NFCService.cs#L182 *
/* Updated to accept either decimal or integer values. Try it at https://www.trycf.com/ */
string function progressBar(required numeric percentage=0, fill="&##9608;", empty="&##9618;"){
if (arguments.percentage lt 0 || arguments.percentage gt 100){
local.p = 0;
} else if ( arguments.percentage gt 1 && arguments.percentage lt 100){
local.p = int(arguments.percentage / 10);
} else {
@JamoCA
JamoCA / cuid-java-vs-cf-cuid2-benchmark.cfm
Last active January 15, 2023 18:39
CUID for Java vs CF-CUID2 Benchmarks
View cuid-java-vs-cf-cuid2-benchmark.cfm
<cfscript>
// 2023-01-15 CUID for Java vs CF-CUID2 Benchmarks
// CUID1: 435,595 ops/sec (clcxncf7wvwy56062pd6o10xs)
// CUID2: 130,682 ops/sec (q2qimk6q6cifa0oyq3ygz316)
// CUID.isValid: 1,898,025 ops/sec (YES)
// CF-CUID2: 5,001 ops/sec (nxvr6js43lh8rtwuuv259dvi)
cuid = createobject("java", "io.github.thibaultmeyer.cuid.CUID");
// Dependency: https://github.com/thibaultmeyer/cuid-java
// JAR: https://search.maven.org/artifact/io.github.thibaultmeyer/cuid
@JamoCA
JamoCA / mergeQbSqlBindingsUDF.cfm
Last active December 30, 2022 20:23
mergeQbSqlBindings - ColdFusion/CFML UDF to be used with QB parameterized SQL string & binding array to generate reusable SQL
View mergeQbSqlBindingsUDF.cfm
<cfscript>
/* 20221222 mergeQbSqlBindings UDF
by James Moberg / SunStar Media.
Tested w/MSSQL. To be used with QB parameterized SQL string & binding array to generate reusable SQL.
NOTE: Using Query Builder (QB) does not required ColdBox or CommandBox.
GIST: https://gist.github.com/JamoCA/bb681afd2eb1a0d6d380f3b714ccc138
TWEET: https://twitter.com/gamesover/status/1606008360976781312
GITHUB: https://github.com/coldbox-modules/qb
DOCS: https://qb.ortusbooks.com/query-builder/building-queries/parameters-and-bindings
USAGE:
@JamoCA
JamoCA / Testing-CFML-Replace-Regex-Limits.cfm
Created December 22, 2022 19:18
Testing ColdFusion/CFML Replace Regex Limits
View Testing-CFML-Replace-Regex-Limits.cfm
<!--- 20221222 Testing CFML Replace Regex Limits
Blog: https://dev.to/gamesover/use-java-replaceall-instead-of-coldfusion-rereplacenocase-ee4
Bug: https://tracker.adobe.com/#/view/CF-4165797
Bug: https://tracker.adobe.com/#/view/CF-3928688
YMMV based on CF version, JVM and regex used, but using native replaceAll() seems to be more reliable & faster
when performing lots of replacements.
When using ACF2016, we encountered intermittent, unpreventable/uncatchable hard ColdFusion errors. (Using CFTRY/CATCH
wouldn't do anything and we had to refer to log files to identify the errors.)
CF2016/18 seems to have a maximum 1587 character limit with this regex. CF2021 is higher.
@JamoCA
JamoCA / cache-udf-reponse-to-request-scope.cfm
Last active December 9, 2022 19:16
Caching repetitive UDF access to request scope using argument stringified+hashed key - #ColdFusion #cfml
View cache-udf-reponse-to-request-scope.cfm
<!--- 20221208 Caching repetitive UDF access to request scope using argument stringified+hashed key #ColdFusion #cfml
By James Moberg / SunStar Media
GIST: https://gist.github.com/JamoCA/62f8535cfcf5dacd4e7f5ea678038330
TWEET: https://twitter.com/gamesover/status/1600908166426136576
BLOG: https://dev.to/jamoca/caching-repetitive-udf-access-to-request-scope-using-argument-stringifiedhashed-key-3lln
--->
<cfscript>
string function slowFunction(required a, b="100", c="abc", d=[], e={}) hint="I perform a repetitive function with a cacheable result" {
// generate a unique key based on stringified+hashed arguments passed to UDF
View getIsoTimeString.cfm
<cfscript>
/**
* Converts your date into a time string the aheres to the ISO 8601 standard (for use with some API calls).
* v1.0 by Ben Nadel
* https://cflib.org/udf/getIsoTimeString
* @param datetime A date/time object (Required)
* @param convertToUTC Whether to convert to UTC before formatting (default true) (Optional)
* @return A date string as per ISO format
* @author Ben Nadel (ben@bennadel.com)
* @version 1.0, August 1, 2013
@JamoCA
JamoCA / function-boolean-cfml.cfm
Last active December 2, 2022 20:05
ColdFusion functions with return type "boolean" returns Double, Integer, String or CFBoolean, but not "boolean" unless you explicitly & manually cast it.
View function-boolean-cfml.cfm
<!--- 20221202 A ColdFusion function w/return type of "boolean" won't return boolean unless you explicitly & manually cast it.
- James Moberg - SunStar Media
Gist: https://gist.github.com/JamoCA/56a6e1f32a353b23c13e71b028c7551f
Tweet: https://twitter.com/gamesover/status/1598770171057168384
If not explicitly cast, Lucee & Adobe may return a "Double", "Integer", or "String" datatype.
Adobe 2018-2021 sometimes returns a "CFBoolean" type & CF2016 only returns "String".
I'm suprised that Adobe doesn't auto-trim values before evaluating. (The isValid("email") BIF auto-trims before validating.)
Probably "not a bug", but I'd prefer that functions with a return type of "boolean" return an typed boolean value and not
some value that can be coerced-to-boolean.
@JamoCA
JamoCA / isvalid-vs-refind-vs-java-pattern.cfm
Last active December 2, 2022 18:09
Comparing Performance of ColdFusion isValid("regex") vs reFind() vs Cached Java Regex Pattern #cfml
View isvalid-vs-refind-vs-java-pattern.cfm
<!--- 20221202 Comparing Performance of ColdFusion isValid("regex") vs reFind() vs Cached Java Pattern #cfml
By James Moberg - SunStar Media
Gist: https://gist.github.com/JamoCA/e8c2ab5815bb26db58ff4024a86c533c
Tweet: https://twitter.com/gamesover/status/1598741050608480256
Blog: https://dev.to/gamesover/coldfusion-isvalidregex-vs-refind-vs-java-regex-pattern-2ee1
--->
<cfscript>
request.tldRegexList = "aaa|aarp|abarth|abb|abbott|abbvie|abc|able|abogado|abudhabi|ac|academy|accenture|accountant|accountants|aco|actor|ad|ads|adult|ae|aeg|aero|aetna|af|afl|africa|ag|agakhan|agency|ai|aig|airbus|airforce|airtel|akdn|al|alfaromeo|alibaba|alipay|allfinanz|allstate|ally|alsace|alstom|am|amazon|americanexpress|americanfamily|amex|amfam|amica|amsterdam|analytics|android|anquan|anz|ao|aol|apartments|app|apple|aq|aquarelle|ar|arab|aramco|archi|army|arpa|art|arte|as|asda|asia|associates|at|athleta|attorney|au|auction|audi|audible|audio|auspost|author|auto|autos|avianca|aw|aws|ax|axa|az|azure|ba|baby|baidu