Skip to content

Instantly share code, notes, and snippets.

View DavidWells's full-sized avatar
😃

David Wells DavidWells

😃
View GitHub Profile
@DavidWells
DavidWells / github-proxy-client.js
Last active March 3, 2025 17:47
Full Github REST api in 34 lines of code
/* Ultra lightweight Github REST Client */
// original inspiration via https://gist.github.com/v1vendi/75d5e5dad7a2d1ef3fcb48234e4528cb
const token = 'github-token-here'
const githubClient = generateAPI('https://api.github.com', {
headers: {
'User-Agent': 'xyz',
'Authorization': `bearer ${token}`
}
})
@DavidWells
DavidWells / thunkify.js
Created March 10, 2022 09:23
Defer execution of function and preserve arguments. Aka thunkify
// via https://www.oreilly.com/library/view/you-dont-know/9781491905197/ch04.html
function foo(x, y) {
return x + y
}
function thunkify(fn) {
var args = [].slice.call( arguments, 1 );
return function(cb) {
args.push( cb );
return fn.apply( null, args );
@DavidWells
DavidWells / attach-multiple-onerror-listeners.js
Created March 1, 2022 23:35
Attach multiple onError or onLoad listeners
// Fork of https://gist.github.com/alexreardon/8833460
function addWindowEvent(event, fn) {
const existing = window[event]
if (typeof existing !== 'function') return fn
return function () {
existing.apply(window, arguments)
fn.apply(window, arguments)
}
}
@DavidWells
DavidWells / regex-find-code-blocks.js
Last active April 8, 2024 03:46
Find all ``` code blocks in markdown
const fs = require('fs')
const path = require('path')
const content = fs.readFileSync(path.resolve(__dirname, 'zmd-with-code.md'), 'utf-8')
// https://regex101.com/r/nIlW1U/6
const PATTERN = /^([A-Za-z \t]*)```([A-Za-z]*)?\n([\s\S]*?)```([A-Za-z \t]*)*$/gm
function findCodeBlocks(block) {
let matches
let errors = []
@DavidWells
DavidWells / async-forEach-with-abort-and-concurrency.js
Created February 26, 2022 08:07
Async forEach with concurrency limit and Abort Controller
// via https://github.com/vatesfr/xen-orchestra/tree/a1c0d82889dbf40aebb87d6e486dc12fee49adbc/%40vates/async-each
const noop = Function.prototype
class AggregateError extends Error {
constructor(errors, message) {
super(message)
this.errors = errors
}
}
@DavidWells
DavidWells / proxy-factory.js
Created February 25, 2022 23:36
JS factory functions via a proxy
// https://github.com/Schniz/factoree
/**
* Creates a strict factory
*
* @param defaults the default values that would appear on all instances
*/
function factory(defaults) {
return attributes => {
const data = Object.assign(Object.assign({}, defaults), attributes);
const proxy = new Proxy(data, {
@DavidWells
DavidWells / trim-comments.js
Created February 24, 2022 04:23
Trim comments from a string
// via https://github.com/suchipi/without-comments/blob/main/index.js
function trimComments(content, commentToken = "#") {
const startRegex = new RegExp(`^${commentToken}`);
const endRegex = new RegExp(`${commentToken}.*$`, "g");
return content
.split("\n")
.map((line) => {
// remove comment lines
if (startRegex.test(line.trim())) return "";
@DavidWells
DavidWells / simple-object-search.js
Created February 22, 2022 04:37
Super simple array of object search without referencing keys
// https://github.com/RajikaKeminda/multi-search/blob/main/index.js
// https://multi-search.vercel.app/
// https://codesandbox.io/s/upbeat-goldberg-mzvzqz?from-embed=&file=/src/App.js
function singleKeyFilter(list, query, key) {
let querySanitizer = String(query).trim().toLowerCase();
return list.filter(
(i) => String(i[key]).toLowerCase().indexOf(querySanitizer) > -1
);
}
@DavidWells
DavidWells / super-simple-gantt-chart.html
Created February 19, 2022 18:04
Super simple Gantt chart setup using google charts
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
const DEFAULT_ACTIVITY_TIME_IN_DAYS = 14
google.charts.load('current', { 'packages': ['gantt'] })
google.charts.setOnLoadCallback(drawChart);
function daysToMilliseconds(days) {
@DavidWells
DavidWells / debug-netlify-cache-directory.js
Created February 19, 2022 17:51
Debug what is in your netlify cache dir
const path = require('path')
const getCacheInfo = require('whats-in-the-cache')
const NETLIFY_CACHE_DIR = '/opt/build/cache'
const MY_BUILD_DIR = path.resolve('build')
const CACHE_MANIFEST_PATH = path.join(MY_BUILD_DIR, 'cache-output.json')
getCacheInfo({
cacheDirectory: NETLIFY_CACHE_DIR,
outputPath: CACHE_MANIFEST_PATH,