Skip to content

Instantly share code, notes, and snippets.

Chris West westc

Block or report user

Report or block westc

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View step-example.js
// Allows for an array or an array like structure to be stepped through in the order specified
// after each iteration.
function step(array, callback, opt_stepValue, opt_startIndex) {
opt_stepValue = ~~opt_stepValue || 1;
var count = array.length;
var i = (opt_startIndex == undefined || isNaN(~~opt_startIndex))
? opt_stepValue < 0
? count - 1
: 0
: opt_startIndex;
@westc
westc / await-for-each.js
Last active Aug 29, 2019
Seeing the difference between using await with Array.prototype.forEach and using await with for...of.
View await-for-each.js
function testForEachReplies(maxSecs = 4) {
async function replyIn(secs) {
return await {
then(res, rej) {
setTimeout(
_ => res(`I replied after ${secs} second${secs - 1 ? 's' : ''}`),
secs * 1e3
);
}
};
@westc
westc / wrapText.js
Last active Aug 28, 2019
Wrap text by splitting the lines based on whitespace and a maximum number of characters.
View wrapText.js
function wrapText(str, opt_max) {
opt_max = Math.min(Math.max(1, ~~opt_max || 80), 200);
let rgx = new RegExp(`\\S{1,${opt_max}}|\\s{1,${opt_max}}`, 'g');
let lastPart, wasNotWS;
return (str.match(rgx) || []).reduce(function (lines, part, partIndex, parts) {
let isNotWS = /\S/.test(part);
if (isNotWS) {
let lineCount = lines.length;
let lastLine = lines[lineCount - 1];
let newLastLine;
@westc
westc / getGitContents.js
Last active Aug 23, 2019
Get the contents of a file or directory in Git using this function on any web page.
View getGitContents.js
/**
* Gets the GitHub contents of a file or directory.
* @param {string} user
* The GitHub user name.
* @param {string} repo
* The name of the repo. If you want to also specify the branch then you can
* append a colon to the repo name and then specify the branch name in this
* same field.
* @param {string} path
* The path to the file or directory whose contents should be retrieved.
@westc
westc / fromExcelDate.js
Created Aug 12, 2019
Function used to convert a numeric value (from Excel or Google Sheets) into a JavaScript Date object.
View fromExcelDate.js
/**
* Converts a numeric value (from Excel or Google Sheets) into a JavaScript Date object.
* @param {number} numDate - The numeric value that will be converted into a date.
* @param {?number=} opt_tzMinOffset - Optional. Defaults to the minute offset of the
* local timezone. If given this should indicate how many minutes to offset the date
* by to correctly represent the desired timezone.
* @returns {Date} - The date representation of the numeric value passed in.
*/
function fromExcelDate(num, opt_tzMinOffset) {
return new Date(
@westc
westc / GoogleSheetReader.js
Last active Aug 9, 2019
Provides an easy way to get Google Sheets data.
View GoogleSheetReader.js
var GoogleSheetReader = (function (document) {
var global = this;
function loadJSONP(url, paramName, callback) {
var fnName = ('_' + Math.random() + Math.random()).replace(/\./g, '');
var body = document.body;
var script = document.createElement('script');
global[fnName] = function (data) {
delete global[fnName];
@westc
westc / pardot.visitor_activity_types.sql
Created May 28, 2019
Create a simple table that indicates the different visitor activity types in Pardot.
View pardot.visitor_activity_types.sql
CREATE TABLE visitor_activity_type AS
SELECT id::INT, name::VARCHAR(100)
FROM (
SELECT 1 AS id, 'Click' AS name
UNION
SELECT 2 AS id, 'View' AS name
UNION
SELECT 3 AS id, 'Error' AS name
UNION
SELECT 4 AS id, 'Success' AS name
@westc
westc / get_url_params.sql
Last active May 21, 2019
Decodes simple URL parameters, getting all of the parameters or just the ones that you want.
View get_url_params.sql
CREATE OR REPLACE FUNCTION get_url_params (
in_url VARCHAR
)
RETURNS TABLE(
param_number INT,
raw_key VARCHAR,
raw_value VARCHAR,
"key" VARCHAR,
"value" VARCHAR
)
@westc
westc / getColNum.gs
Last active May 5, 2019
Gets the number of the column from the letter reference.
View getColNum.gs
/**
* Gets the number of the column from the letter reference
* (eg. A -> 1, B -> 2, G -> 7, AC -> 29, etc.).
* @param {string} letters
* The letters referring to column which will be converted to a number.
* @return {number}
* The column number represented by the given letters.
*/
function getColNum(letters) {
var num = 0;
@westc
westc / formatDate.gs
Last active May 5, 2019
Formats a JSON date string or a date object using the Google sheet's timezone or the timezone you specify.
View formatDate.gs
/**
* Formats a JSON date string or a date object using the spreadsheet's timezone
* or the timezone you specify.
* @param {Date|string} date
* The date object or the string that should be formatted as a date string.
* @param {string=} opt_format
* Optional. The format of the date as it should be returned. If not
* specified the format will be "yyyy-MM-dd'T'HH:mm:ss'Z'".
* @param {string=} opt_timeZone
* Optional. Timezone to use to format the date. Defaults to the
You can’t perform that action at this time.