Skip to content

Instantly share code, notes, and snippets.

View mrandrewmills's full-sized avatar

Andrew Mills mrandrewmills

View GitHub Profile
@mrandrewmills
mrandrewmills / arrangeArrayStructs.js
Created June 29, 2019 20:27
Ever needed to arrange an Array of Structs in a specific order which wasn't alphabetical or numerical? (e.g. Federal Holidays, or Beatles) This function can help you with that.
function arrangeArrayStructs(arrayToArrange, arrOrder, whichKey){
var results = [];
arrOrder.forEach( function(item){
results.push( arrayToArrange.filter( record => record[whichKey] == item ) );
});
return results;
}
@mrandrewmills
mrandrewmills / arrayEnhancements.js
Created January 6, 2019 14:23
initial commit, add method to Array to extract unique values
/*
* .unique() - filters out any duplicated values from an Array
* test = ["A", "B", "A", "C", "A", "C", "D"]; // given this
* test.unique(); // using this
* (4) ["A", "B", "C", "D"] // returns that
*/
if (typeof Array.prototype.unique === "undefined"){
Array.prototype.unique = function(){
@mrandrewmills
mrandrewmills / GAExceptions.js
Created December 7, 2018 18:50
Custom Code for Jeff Starr's Google Analytics WordPress plugin to log JS exceptions
<script>
// bind GA send to window.onerror event
window.onerror = function(message, source, lineno, colno, error){
// find out if Google Analytics is present
if ( (typeof window[window.GoogleAnalyticsObject] !== "undefined") && (window[window.GoogleAnalyticsObject].answer === 42) ){
// create a reference to the GoogleAnalyticsObject
tmpGA = window[window.GoogleAnalyticsObject];
@mrandrewmills
mrandrewmills / utmTags.js
Created October 24, 2018 12:40
quick & dirty JS object for managing utm tags, adding to existing URLs.
function UTM(source, medium, campaign, term, content){
try {
if (typeof(source) === "undefined")
throw("Source argument required.");
this.source = source;
this.medium = medium || "";
this.campaign = campaign || "";
this.term = term || "";
@mrandrewmills
mrandrewmills / compJSONStringify.js
Created June 28, 2018 00:03
Picked up a copy of Tom Marrs' JSON At Work last night, and realized JSON Stringify can be used as a way to compare objects in JS for equivalency.
/* quick and dirty way to compare two objects in JS for equivalency */
function compareObject(objFirst, objSecond){
return ( JSON.stringify(objFirst) === JSON.stringify(objSecond) );
}
@mrandrewmills
mrandrewmills / color.js
Last active February 28, 2018 03:32
quick and dirty RGB to HEX converter
function Color(r,g,b){
this.red = r;
this.green = g;
this.blue = b;
this.toHex = function (){
var redHex = this.red.toString(16);
var greenHex = this.green.toString(16);
@mrandrewmills
mrandrewmills / ThornyPuzzle.js
Created February 24, 2018 17:02
This puzzle features a JS object with a subtle thorn in it. Can you identify and explain the issue?
function Book(title, author, genre, ISBN){
this.title = title;
this.author = author;
this.genre = genre;
this.ISBN = ISBN;
}
function Library(){
this.books = [];
@mrandrewmills
mrandrewmills / areAnagrams.io
Created February 3, 2018 13:31
Io functions for testing if two strings are or are not anagrams
/**
* areAnagrams - compare two strings to see if they are anagrams
*
* param string1, first string to compare
* param string2, second string to compare
* return boolean, true = string are anagrams, false = strings are not
*/
areAnagrams := method(string1, string2,
return ( lettersScore(string1) == lettersScore(string2) )
)
@mrandrewmills
mrandrewmills / anagramTester.js
Last active February 18, 2018 03:42
functions which compare two strings to see if they are or are not anagrams.
/**
* areAnagrams - tests if two strings are anagrams (e.g. ear, are) or not
*
* @param {String} string1 first string to compare (e.g. ear)
* @param {String} string2 second string to compare (e.g. are)
* @return {Boolean} true = anagrams, false = not anagrams
*/
function areAnagrams(string1, string2){
"use strict";
@mrandrewmills
mrandrewmills / uniqueChars.io
Created January 30, 2018 13:57
filters out duplicated characters in a string
uniqueChars := method(myString,
result := ""
myString foreach (i,v,
(i==myString findSeq(v asCharacter)) ifTrue(result = result .. v asCharacter)
)
return result
)