Skip to content

Instantly share code, notes, and snippets.

View tonyjunkes's full-sized avatar
🍺
Cheers

Tony Junkes tonyjunkes

🍺
Cheers
View GitHub Profile
@tonyjunkes
tonyjunkes / queryColumnSort.cfm
Created February 8, 2024 18:25
Example sorting alphanumeric column names for a query object created out of order (for example: cfspreadsheet).
<cfscript>
// Column name example: col_1, col_10, col_6, etc.
var qryColumnOrder = myQuery.columnList
.listToArray()
.sort((a, b) => {
// Extract the numeric part from the string
var numA = numberFormat(a.reReplace("\D", "", "all"), "0000");
var numB = numberFormat(b.reReplace("\D", "", "all"), "0000");
// Extract the text part from the string
@tonyjunkes
tonyjunkes / createSpreadSheet.cfm
Created May 15, 2022 06:35
General function that takes an array of structs representing data to go on the file along with column/row/cell styling.
/*
Example argument structure
worksheets = [
{
name: [sheet_name],
data: [query],
includeColumnNames: [true|false],
columnFormat: [struct],
columnCellStyle: [array_of_structs]
}
@tonyjunkes
tonyjunkes / binaryPDFToImage.cfm
Last active April 4, 2022 01:42
Converts pages of a binary PDF document into binary JPG images (concurrently). Requires PDFBox 2.x, CF 2021/Lucee 5.x
<cfscript>
/**
* @hint Converts pages of a binary PDF document into binary JPG images.
* @pdfFile.hint A binary representation of the file to be converted.
*/
public array function pdfToImage(binary pdfFile) {
var result = [];
// Collection for async processes
var futures = [];
@tonyjunkes
tonyjunkes / roundedDollarFormat.cfm
Created April 15, 2021 15:00
Choose to round a decimal value that is 3 or more places and return the dollar formatted value.
<cfscript>
// Because of https://tracker.adobe.com/#/view/CF-4199995,
// I've come across a few scenarios where dollarFormat() and numberFormat() will not round up when expected/desired.
// Looking at how Java handles BigDecimals and rounding them, it seems my expectations are debatable.
// But I need to round up from 3+ decimal places so I expect "62.275" to become "6.28".
// The above example works in dollarFormat() but not numberFormat(). The ticket has other failing examples.
public string function roundedDollarFormat(
required numeric amount,
@tonyjunkes
tonyjunkes / hexColorSpread.cfm
Created March 19, 2021 20:35
Returns an array of hex based colors starting from a provided color and shifting from dark to light or the reverse.
<cfscript>
array function shadeColors(string color, numeric percent, numeric iteration) {
var result = [];
var currentcolor = arguments.color;
for (var i = 0; i < arguments.iteration; i++) {
var R = inputBaseN(currentcolor.mid(1, 2), 16);
var G = inputBaseN(currentcolor.mid(3, 2), 16);
var B = inputBaseN(currentcolor.mid(5, 2), 16);
R = formatBaseN(R * (100 + percent) / 100, 10);
@tonyjunkes
tonyjunkes / randomAlphaNumericString.cfm
Last active April 27, 2022 05:33
Generate a random alpha/numeric string based on a given length using Java
<cfscript>
string function randomAlphaNumericString(required numeric length) {
var alphaNum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var SecureRandom = new java("java.security.SecureRandom"); // Note: CF2018 syntax
var rng = SecureRandom.getInstanceStrong();
var result = "";
for (var i = 0; i < arguments.length; i++) {
result &= alphaNum.charAt( rng.nextInt(alphaNum.len()) );
@tonyjunkes
tonyjunkes / arrayRemoveDuplicates.cfm
Last active August 12, 2020 15:21
Remove duplicates from an array in CFML using Java
<cfscript>
example = ["a", "b", "c", "a", 1, 2, 1];
HashSet = createObject("java", "java.util.HashSet");
Arrays = createObject("java", "java.util.Arrays");
result = HashSet.init(Arrays.asList(example)).toArray();
writeDump(result);
print( ['a', 'at', 'cat', 'scat', 'catch'].find { it ==~ '.+at' } )
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="false">
<Context path="" docBase="C:\websites\coldfusion2016\wwwroot" workDir="C:\ColdFusion2016\cfusion\runtime\conf\Catalina\localhost\tmp">
<Resources>
<PreResources className="org.apache.catalina.webresources.DirResourceSet" base="C:\ColdFusion2016\cfusion\wwwroot\CFIDE" webAppMount="/CFIDE" />
<PreResources className="org.apache.catalina.webresources.DirResourceSet" base="C:\ColdFusion2016\cfusion\wwwroot\WEB-INF" webAppMount="/WEB-INF" />
</Resources>
</Context>
</Host>
<cfscript>
array function nextIds(required array ids, required numeric id, numeric count = 0) {
var idPos = arguments.ids.find(arguments.id);
return arguments.ids.filter(function(item, index) {
return idPos && index != idPos && index <= idPos + count;
});
}
writeDump( nextIds([7141, 6881, 5821, 8001, 7904, 6601, 7961, 6021, 4721], 7141, 3) );