Skip to content

Instantly share code, notes, and snippets.

View jameswomack's full-sized avatar
📈

James J. Womack jameswomack

📈
View GitHub Profile
@jameswomack
jameswomack / palindromes.js
Last active December 14, 2021 21:43
Determining if a string is a palindrome—or becomes a palindrome by dropping one of its characters
function isArrayPalindrome (array) {
const forward = [...array].join();
const reversed = [...array].reverse().join();
if (reversed == forward) {
return true;
}
return false;
}
const getArraySansIndex = (a, _index) => a.filter((item, index) => index !== _index);
@jameswomack
jameswomack / find-and-replace.txt
Created September 2, 2021 18:02
Replacing named function expressions w/ arrow body style
Find:
function ([a-zA-Z]+)(\([a-zA-Z, \{\}]+\)) (\{)
Replace:
const $1 = $2 => $3
@jameswomack
jameswomack / _jewel.js
Last active July 15, 2021 00:30
Configurably generating a "jewel" board in which no 3 consecutive jewels are allowed in a given column or row
/* eslint-disable no-console, no-param-reassign */
/**
* Returns a filled in board such that there are no 3 consecutive
* jewels (vertically/horizontally) of the same type.
*
* @param widthOfBoard int which provides width of board
* @param heightOfBoard int which provides height of board
* @param jewels array providing valid jewels for board
*/
@jameswomack
jameswomack / scope.js
Created July 14, 2021 16:34
Looking at the difference between function & block scope in JavaScript
const console = require("console");
(function () {
console.log(foo); // undefined
{
var foo = 'foo';
}
})();
(function () {
@jameswomack
jameswomack / scopev.js
Last active July 7, 2021 23:28
A CLI script for syncing versions of all a scope's dependencies within a monorepo. Supports CLI args & environment variables
#!/usr/bin/env node
const argv = require('yargs-parser')(process.argv.slice(2));
function getEnvVarForScopeVConfigKey(keyName) {
const {env} = require('process');
return env[`MONOREPO_${keyName.toUpperCase()}`];
}
const ScopeVConfiguration = Object.create({
@jameswomack
jameswomack / index.js
Last active July 6, 2021 19:38
Does an array contain a particular sum? Performant edition.
/* Begin cache-related code */
const sumCache = new Map();
function getCachedSumResult (array, sum) {
return sumCache.get(`${array}{${sum}`);
}
function cacheSumResult (array, sum, result) {
sumCache.set(`${array}{${sum}`, result);
return result;
@jameswomack
jameswomack / javascript_background_thread.js
Created September 11, 2012 06:41
Extend function prototype to run a function as a WebWorker
Function.prototype.runOnBackgroundThread = function (aCallback) {
var _blob = new Blob(['onmessage = '+this.toString()],{"type":"text/javascript"});
var _worker = new Worker((webkitURL.createObjectURL || URL.createObjectURL)(_blob));
_worker.onmessage = aCallback;
_worker.postMessage();
}
var _test = function () {
postMessage((1+1).toString());
}
@jameswomack
jameswomack / managebac-class-students-api-usage.js
Created March 1, 2021 03:32
Using the ManageBac API to retrieve a set of students across multiple classes
const fetch = require('node-fetch');
function classFetch (classId) {
return fetch(`https://api.managebac.com/v2/classes/${classId}/students`, {
headers: {
'auth-token': '{{}}'
}
})
.then(response =>
response.json()
@jameswomack
jameswomack / knutshell.sh
Created February 26, 2021 00:58
Function composition in a Knut shell
> foo = (...s) => `foo${s.join()}`
[Function: foo]
> bar = (...s) => `${s.join()}bar`
[Function: bar]
> qux = s => Math.ceil(Number(s))
[Function: qux]
> compose(foo, bar, qux)(1.27890)
'foo2bar'
> fooBarQux = compose(foo, bar, qux)
undefined