Skip to content

Instantly share code, notes, and snippets.

View bradmarshall's full-sized avatar

Brad Marshall bradmarshall

View GitHub Profile
@bradmarshall
bradmarshall / formatDateAsYearMonthDay.js
Last active August 24, 2022 19:33
Format a date string or date object using DayJS
import dayjs from 'dayjs'
// @param date {string, dayjs object}
// @param separator {string} optional - separates year/month/day numbers. Empty string is valid.
function formatDateAsYearMonthDay(date, separator = '-') {
if (!(date instanceof dayjs || typeof date === 'string')) {
return false // Unsupported date type. `undefined` is unsupported.
}
@bradmarshall
bradmarshall / getSiteMapURLs.php
Created June 14, 2018 18:44
Read all URL's from XML sitemap file.
<?php
// Requires PHP DOM extension. Works with both local files and live ones on the web!
if(!isset($argv[1])) {
print("getSiteMapURLs.php error: This script takes one argument (the path of the site map to parse).".PHP_EOL);
die();
}
$urls = "";
@bradmarshall
bradmarshall / coldfution-closure-scope.cfm
Created December 1, 2016 16:00
Illustrates scoping of external variables in a Coldfusion Closure. If you create a variable in "local" scope outside of the closure function and wish to reference it from *inside*, you must leave the inside variable Unscoped in order to allow the engine to search through various scopes in order to find the external "local". The order the engine …
local.people = {
"jim": 100,
"bob": 500,
"sally": 1000
};
local.myStruct = {};
structEach(local.people, function(key, value) {
writeDump(key); // name
@bradmarshall
bradmarshall / sortArrayOfStructs.cfm
Last active November 11, 2016 16:08
Sorting an array of structs in Coldfusion using a "function expression". Function Expressions allow you to pass an anonymous argument to another function. Coldfusion 10 supports function expressions in: arrayEach(), arrayFilter(), arrayFind(), arrayFindAll(), arraySort(), listFilter(), structEach() and structFilter().
<cfscript>
// Testing sorting an array of structs
variables.myArray = [
{
"animal": "cat",
"averageAge": 37
},
{
"animal": "dog",
@bradmarshall
bradmarshall / Folder listing based on folders from two separate sites
Last active May 5, 2016 21:08
In Mura CMS, I needed to create a folder listing that contained items from two separate folders, each of which are on their own site. If the folders were on one site you could achieve this with a single feed easily but on two different sites it gets trickier. My solution was to create two separate feeds, extract the query result from both and ja…
<cfscript>
// Get main site's feed.
variables.feed = $.getBean("feed");
variables.feed.setSiteID("default");
variables.feed.setMaxItems(0);
variables.feed.setSortBy(esapiEncode('html_attr',variables.$.event('sortby')));
variables.feed.setSortDirection(esapiEncode('html_attr',variables.$.event('sortdirection')));
variables.feed.setContentID("57A975C9-155D-11DA-00EE3AC56A450048");
variables.qryMainSite = variables.feed.getQuery();
@bradmarshall
bradmarshall / getPosition.js
Last active September 25, 2016 15:29
A simple little function to get the position of an element with respect to the upper top left of the viewport. Uses getBoundingClientRect() for today's browsers and falls back to an older method for older browsers.
// Get position of element from top of viewport.
function getPosition(element) {
// Modern browsers.
if(element.getBoundingClientRect) {
var clientRect = element.getBoundingClientRect();
return {
x: Math.floor(clientRect.left),
y: Math.floor(clientRect.top)