Skip to content

Instantly share code, notes, and snippets.

View DavidWells's full-sized avatar
😃

David Wells DavidWells

😃
View GitHub Profile
@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'`
const fetch = async (...args) => {
console.log(...args)
return {
statusCode: 200,
data: {},
}
}
function httpRequest(url, method, data, opts, cb) {
const init = { method }
@DavidWells
DavidWells / fetch-via-js-proxy.js
Last active February 8, 2022 17:02
Fetch pages via JS proxy with await www
// Via https://twitter.com/RReverser/status/1490873967577640961
function wwwProxy() {
return new Proxy(new URL('https://www/'), {
get: function get(target, prop) {
let orig = Reflect.get(target, prop);
if (typeof orig === 'function') return orig.bind(target);
if (typeof prop !== 'string') return orig;
if (prop === 'then') return Promise.prototype.then.bind(fetch(target));
target = new URL(target);
target.hostname += `.${prop}`;
@DavidWells
DavidWells / alter-function-args-with-proxy.js
Created February 8, 2022 01:04
Alter function args via JS proxy
// via https://blog.sessionstack.com/how-javascript-works-proxy-and-reflect-11748452c695
const p = new Proxy(function() {}, {
apply: function(target, thisArg, argumentsList) {
console.log('called: ' + argumentsList.join(', '));
return argumentsList[0] + argumentsList[1] + argumentsList[2];
}
});
console.log(p(1, 2, 3)); // This will print called: 1, 2, 3 and 6
@DavidWells
DavidWells / set-private-field-via-proxy.js
Created February 8, 2022 01:00
Set private object fields via JS proxy
// https://betterprogramming.pub/everything-you-should-know-about-javascript-proxy-67576f2e069e
function setPrivateField(obj, prefix = "_"){
return new Proxy(obj, {
has: (obj, prop) => {
if(typeof prop === "string" && prop.startsWith(prefix)){
return false
}
return prop in obj
},
ownKeys: obj => {