Skip to content

Instantly share code, notes, and snippets.

View DavidWells's full-sized avatar
😃

David Wells DavidWells

😃
View GitHub Profile
@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,
@DavidWells
DavidWells / parse-time-string.js
Created February 18, 2022 22:54
Parse time out of random string. Handy
// https://github.com/substack/parse-messy-time/blob/master/index.js
var months = [
'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'
];
var days = [
'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
];
@DavidWells
DavidWells / import-flavors.js
Created February 14, 2022 22:43
“infamous triplet” set up to support TS, faux esm, esm and commonjs
// https://github.com/fastify/fastify/blob/224dc104260ad26f9baa0e46962f917963d41fe5/fastify.js#L711
// https://twitter.com/matteocollina/status/1493198259212406784
/**
* These export configurations enable JS and TS developers
* to consumer fastify in whatever way best suits their needs.
* Some examples of supported import syntax includes:
* - `const fastify = require('fastify')`
* - `const { fastify } = require('fastify')`
* - `import * as Fastify from 'fastify'`
* - `import { fastify, TSC_definition } from 'fastify'`