• Find duplicate IDs in your DOM

    With a dynamic application with lots of shared templates it can sometimes be easy to use the same ID for two different elements on the same page. Sometimes this can manifest itself by your JS not working quite right but it isn’t apparent what the problem is. Here is a quick script you can paste into your console to report any duplicates.

    var allElements = document.getElementsByTagName("*"), allIds = {}, dupIDs = [];
    for (var i = 0, n = allElements.length; i < n; ++i) {
      var el = allElements[i];
      if (el.id) {
      	if (allIds[el.id] !== undefined) dupIDs.push(el.id);
      	allIds[el.id] = el.name || el.id;
        }
      }
    if (dupIDs.length) { console.error("Duplicate ID's:", dupIDs);} else { console.log("No Duplicates Detected"); }

    Math Busche covered the same topic last year, head over there to see how he tackled the same thing.

    comments

  • CFML Value Array

    Occasionally in the CFML Slack channel (and the IRC room before it) someone asks about a valueArray() function - what they mean is that they want something like the terribly named valueList() that you can provide a query and a column and get a column slice which will return an array instead of a CFML list. valueArray() exists on Lucee but for ACF you’re stuck implementing it for yourself. Here is a way to do that properly (it’s easy to do it naively wrong).

    Read more

    comments

  • kth Percentile in CFML

    I recently needed to analyze some data in CFML where there was a wide range of values but some real outliers - latency numbers for example. So I ported this kthPercentile() function. You pass in the percentile and an array of numbers - kthPercentile(99, [...]) will give you the 99th percentile for example - kthPercentile(50, [...]) is the same as the median.

    <cfscript>
    	function kthPercentile (k, data) {
    		if (k > 1) { //this allows users to pass in .99 or 99
    			k = k / 100;
    		}
    		arraySort(data, "numeric");
    		var kth = k * arrayLen(data);
    		if (kth == ceiling(kth)) { //its a whole number
    			return data[kth];
    		} else {
    			return (data[int(kth)] + data[ceiling(kth)]) / 2;
    		}
    	}
    </cfscript>

    comments

  • CFML Fuzzy Time

    A friend asked recently how I would go about writing a function to do fuzzy time - something that formats time in a casual way, the way you would talk about it in conversation - “just now”, “a few minutes ago”, “2 days ago”, etc. He wanted it to be customizable, where they could define the groupings used. This is what we came up with:

    Read more

    comments

  • Today I learned

    I have been considering starting a blog (technically again) for a while now but I couldn’t convince myself that I had something worth saying. But after being inspired by a few different people that even just documenting what you have learned can make useful and worthwhile content. I don’t expect a large audience — but this is more about me and my journey than anything.

    The subjects for the posts here will be varied; Certainly lots of technical posts about what I am learning at my new job but I would expect some scientific, social and possibly even the occasional political thought to come by as well.

    I have attempted to do this in the past without much luck — I hope to stick with it much longer this time and if you are reading this I hope you help encourage me too. Hopefully something I learn today can be something you can learn with me.

    comments